//+------------------------------------------------------------------+
//|                                             TDSEQUENTA_v2016.mq5 |
//|                                              Copyright 2016, Tor |
//|                                         https://www.einvestor.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Tor"
#property link      "https://www.einvestor.ru"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3

#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_type2   DRAW_ARROW
#property indicator_color2  clrBlue
#property indicator_type3   DRAW_ARROW
#property indicator_color3  clrGreen

input int set=5;
input int up=8;

double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];
double Buffer7[];
int b;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorSetString(INDICATOR_SHORTNAME,"TD SEQUENTA");
   SetIndexBuffer(0,Buffer3,INDICATOR_DATA);// Assign an array to a buffer
   SetIndexBuffer(1,Buffer4,INDICATOR_DATA);// Assign an array to a buffer 
   SetIndexBuffer(2,Buffer5,INDICATOR_DATA);// Assign an array to a buffer
   SetIndexBuffer(3,Buffer6,INDICATOR_DATA);// Assign an array to a buffer

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,1);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,1);
  // PlotIndexSetInteger(3,PLOT_DRAW_BEGIN,1);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);
  // PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   PlotIndexSetInteger(2,PLOT_ARROW,159);
 //  PlotIndexSetInteger(,PLOT_ARROW,159);

   ArraySetAsSeries(Buffer3,true);
   ArraySetAsSeries(Buffer4,true);
   ArraySetAsSeries(Buffer5,true);
   ArraySetAsSeries(Buffer6,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(time,true);
   int i,limit;

   if(rates_total<=1)
      return(0);
//--- last counted bar will be recounted
   if(prev_calculated>rates_total || prev_calculated<=0)// checking for the first start of calculation of an indicator
     {
      limit=rates_total-5; // starting index for the calculation of all bars
        }else{
      limit=rates_total-prev_calculated; // starting index for the calculation of new bars
     }

   bool perUP,perDWN,enterup,enterdwn;
   if(b!=rates_total)
     {
      b=rates_total;

      for(i=limit-1; i>=1; i--)
        {

         Buffer3[i]=EMPTY_VALUE;
         Buffer4[i]=EMPTY_VALUE;
         Buffer5[i]=EMPTY_VALUE;

         if((setupUP(i+1,set,up)>setupUP(i,set,up)) && (high[i+1]>=low[i+3] || high[i+2]>=low[i+4])) 
           {
           Buffer6[i]=1;
            perUP=true;
            perDWN=false;
            enterup=false;
            enterdwn=false;
            Buffer4[i]=low[i];
           }
         if((setupDOWN(i+1,set,up)>setupDOWN(i,set,up)) && (low[i+1]<=high[i+3] || low[i+2]<=high[i+4]))
           {
           Buffer6[i]=-1;
            perDWN=true;
            perUP=false;
            enterdwn=false;
            enterup=false;
            Buffer3[i]=high[i];
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
int setupUP(int i,int k,int k1)
  {
   int num=0,z;

   for(z=i+k1; z>=i; z--)
     {
      if(iClose(_Symbol,_Period,z)<iClose(_Symbol,_Period,z+k)) 
        {
         num=num+1;

         if(num>k1) 
           {
            Buffer5[i]=iLow(_Symbol,_Period,i);
            return (1);
           }

           }else{
         num=0;
        }
     }

   return (0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int setupDOWN(int i,int k,int k1)
  {
   int num=0,z;

   for(z=i+k1; z>=i; z--)
     {
      if((iClose(_Symbol,_Period,z)>iClose(_Symbol,_Period,z+k))) 
        {
         num=num+1;

         if(num>k1) 
           {
            Buffer5[z]=iHigh(_Symbol,_Period,i);

            return (1);
           }

           }else{
         num=0;
        }
     }

   return (0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iClose(string symbol,int tf,int index)
  {
   if(index < 0) return(-1);
   double Arr[];
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   if(CopyClose(symbol,timeframe,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iHigh(string symbol,int tf,int index)
  {
   if(index < 0) return(-1);
   double Arr[];
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   if(CopyHigh(symbol,timeframe,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double iLow(string symbol,int tf,int index)
  {
   if(index < 0) return(-1);
   double Arr[];
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   if(CopyLow(symbol,timeframe,index,1,Arr)>0)
      return(Arr[0]);
   else return(-1);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
ENUM_TIMEFRAMES TFMigrate(int tf)
  {
   switch(tf)
     {
      case 0: return(PERIOD_CURRENT);
      case 1: return(PERIOD_M1);
      case 5: return(PERIOD_M5);
      case 15: return(PERIOD_M15);
      case 30: return(PERIOD_M30);
      case 60: return(PERIOD_H1);
      case 240: return(PERIOD_H4);
      case 1440: return(PERIOD_D1);
      case 10080: return(PERIOD_W1);
      case 43200: return(PERIOD_MN1);

      case 2: return(PERIOD_M2);
      case 3: return(PERIOD_M3);
      case 4: return(PERIOD_M4);
      case 6: return(PERIOD_M6);
      case 10: return(PERIOD_M10);
      case 12: return(PERIOD_M12);
      case 16385: return(PERIOD_H1);
      case 16386: return(PERIOD_H2);
      case 16387: return(PERIOD_H3);
      case 16388: return(PERIOD_H4);
      case 16390: return(PERIOD_H6);
      case 16392: return(PERIOD_H8);
      case 16396: return(PERIOD_H12);
      case 16408: return(PERIOD_D1);
      case 32769: return(PERIOD_W1);
      case 49153: return(PERIOD_MN1);
      default: return(PERIOD_CURRENT);
     }
  }
//+------------------------------------------------------------------+
