#property     indicator_chart_window
#property     indicator_buffers                                    2
#property     description                                         "© True Cross HTF"
#property     copyright		                                       ""
#property     strict
#property     link				                                    ""

extern string ————————————————————————1———————————————————————— = "————————|  Settings for  Count Bars :";
input         int                 CountBars                     =  100;                //  Count Bars:

extern string ————————————————————————2———————————————————————— = "————————|  Settings for  TIME-Frame :";
input         ENUM_TIMEFRAMES     TimeFrame                     =  PERIOD_CURRENT;      //  Time Frame:

extern string ————————————————————————3———————————————————————— = "————————|  Settings for  Indicator Stochastic :";
input         int                 InpKPeriod                    =  8;                  //  %K Period
input         int                 InpDPeriod                    =  3;                   //  %D Period
input         int                 InpSlowing                    =  3;                   //  Slowing
input         string —————————————————————————————————————————— =  "";
input         int                 UpperLevel                    =  50;                  //  Overbought level:
input         int                 LowerLevel                    =  50;                  //  Oversold level:

extern string ————————————————————————4———————————————————————— = "————————|  Settings for  Arrows :";
input         int                 BullsTypes                    =  159;                 //  Upper Arrows type:
input         int                 BearsTypes                    =  159;                 //  Lower Arrows type:
input         color               BullArrows                    =  clrLime;             //  Bull Arrows color:
input         color               BearArrows                    =  clrRed;              //  Bear Arrows color:
input         int                 SizeArrows                    =  2;                   //  Arrows size:
input         int                 ShiftArrow                    =  5;                   //  Shifting Arrows from Extremes:

long          times;
int           heigh;
int           limit;
int           period;

double        ArrowBULL[];
double        ArrowBEAR[];
double        SignlBULL[];
double        SignlBEAR[];
double        MainsBuff[];
double        SignlBuff[];

bool          upSignal=false;
bool          dnSignal=false;

//————————————————————————————————————|

void OnChartEvent(const int     id,
                  const long   &lparam,
                  const double &dparam,
                  const string &sparam)
{
//————————————————————————————————————|

   if(id == CHARTEVENT_CLICK
   || id == CHARTEVENT_CHART_CHANGE)
   {
      ChartSetSymbolPeriod(0,NULL,0);
   }
}

//——————————————————————————————————|

int OnInit()
{
   IndicatorBuffers(6);

   SetIndexBuffer(0,ArrowBULL);
   SetIndexBuffer(1,ArrowBEAR);
   SetIndexBuffer(2,MainsBuff);
   SetIndexBuffer(3,SignlBuff);
   SetIndexBuffer(4,SignlBULL);
   SetIndexBuffer(5,SignlBEAR);

   SetIndexArrow (0,BullsTypes);
   SetIndexArrow (1,BearsTypes);
   SetIndexStyle (0,DRAW_ARROW,EMPTY,SizeArrows,BullArrows);
   SetIndexStyle (1,DRAW_ARROW,EMPTY,SizeArrows,BearArrows);

   return(INIT_SUCCEEDED);
}

//—————————————————————————————————————————————————————————|

int OnCalculate(const int       rates_total,
                const int       prev_calculated,
                const datetime &time[],
                const double   &open[],
                const double   &high[],
                const double   &low[],
                const double   &close[],
                const long     &tick_volume[],
                const long     &volume[],
                const int      &spread[])
{
//——————————————————————————————————————————————|

   if(_Period < TimeFrame) period=TimeFrame;
   else                    period=PERIOD_CURRENT;

//———————————————————————————————————————————————————————————————|

          heigh =(int)ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0);
   double WPMax = WindowPriceMax();
   double WPMin = WindowPriceMin();
   double distn = WPMax - WPMin;

//——————————————————————————————————————————————————————————|

   if(CountBars >= rates_total) limit=rates_total-InpKPeriod;
   else                         limit=CountBars;

//—————————————————————————————————————————————|

   for(int i = limit; i >= 0; i--)
   {
      SignlBULL[i]= 0;
      SignlBEAR[i]= 0;

      MainsBuff[i]= iStochastic(NULL,period,InpKPeriod,InpDPeriod,InpSlowing,MODE_SMA,0,MODE_MAIN,i);
      SignlBuff[i]= iStochastic(NULL,period,InpKPeriod,InpDPeriod,InpSlowing,MODE_SMA,0,MODE_SIGNAL,i);

//————————————————————————————————————————————————————————————————————————————————————————————————————|

      if(MainsBuff[i]< LowerLevel && MainsBuff[i]>= SignlBuff[i]&& MainsBuff[i+1]<= SignlBuff[i+1]){SignlBULL[i]= iLow(NULL,period,i)-(heigh!=0?(distn /(heigh /(10+ShiftArrow))):0);}
      if(MainsBuff[i]> UpperLevel && MainsBuff[i]<= SignlBuff[i]&& MainsBuff[i+1]>= SignlBuff[i+1]){SignlBEAR[i]= iHigh(NULL,period,i)+(heigh!=0?(distn /(heigh /(10+ShiftArrow))):0);}
   }

//————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————|

       int y=0;
   for(int i=limit; i>=0; i--)
   {
      ArrowBULL[i]= 0;
      ArrowBEAR[i]= 0;

      if(period != PERIOD_CURRENT)
      {
         y = iBarShift(NULL,TimeFrame,Time[i]);

         if(SignlBULL[y]!= 0 && upSignal==false){upSignal=true; ArrowBULL[i]= SignlBULL[y];}
         if(SignlBULL[y]== 0 && upSignal==true){upSignal=false; ArrowBULL[i]= 0;}

         if(SignlBEAR[y]!= 0 && dnSignal==false){dnSignal=true; ArrowBEAR[i]= SignlBEAR[y];}
         if(SignlBEAR[y]== 0 && dnSignal==true){dnSignal=false; ArrowBEAR[i]= 0;}
      }
      else
      {
         if(SignlBULL[i]!= 0) ArrowBULL[i]= low[i]-(heigh!=0?(distn /(heigh /(10+ShiftArrow))):0);
         if(SignlBEAR[i]!= 0) ArrowBEAR[i]= high[i]+(heigh!=0?(distn /(heigh /(10+ShiftArrow))):0);
      }
   }

   return(rates_total);
}

//———————————————————————————————————| end of the algorithm |—————————————————————————————————————|