//+------------------------------------------------------------------+
//|                                                TwoFingerLine.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
input int FastPeriod=5;
input int SlowPeriod=22;
input int Box=14;

color Color=clrLightSeaGreen;
      double hh=-1, ll=-1;
      datetime tt=-1;
      int SBar=-1;
      int Direct0=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
      DelObj();
  }

int OnInit()
  {
//--- indicator buffers mapping
      DelObj();
//---
   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[])
  {
//---
   
//--- return value of prev_calculated for next call

   Color=(color)ChartGetInteger(0,CHART_COLOR_VOLUME);
   int i=1; int b1=-1; Direct0=0;
   int bf=-1;
      double maFast=iMA(_Symbol,1440,FastPeriod,0,MODE_EMA,PRICE_CLOSE,1);
      double maSlow=iMA(_Symbol,1440,SlowPeriod,0,MODE_EMA,PRICE_CLOSE,1);
      if (maFast>maSlow) Direct0=1;
      if (maFast<maSlow) Direct0=-1;
      
      hh=-1; ll=-1;
      tt=-1;
      SBar=-1;

      if (Direct0!=0)
      {
         while (b1<0 && tt<=0 && ll<0 && i<Bars-1)
         {
            int Direct=0;
            maFast=iMA(_Symbol,1440,FastPeriod,0,MODE_EMA,PRICE_CLOSE,i);
            maSlow=iMA(_Symbol,1440,SlowPeriod,0,MODE_EMA,PRICE_CLOSE,i);
            if (maFast>maSlow) Direct=1;
            if (maFast<maSlow) Direct=-1;
            if (Direct0!=Direct) {b1=i; break;}
            double frUp=0, frDn=0;
            frUp=iFractals(_Symbol,1440,MODE_UPPER,i);
            frDn=iFractals(_Symbol,1440,MODE_LOWER,i);
            if ((Direct0>0 && frUp>0) ||
               (Direct0<0 && frDn>0)) 
            {hh=iHigh (_Symbol,1440,i-2);
            ll=iLow(_Symbol,1440,i-2);
            tt=iTime(_Symbol,1440,i-2);
            }
            
            i++;
         }
      }
      SBar=i-1;
//      Comment(i,"  ",SBar);
//            DelObj();

      if (hh>0 && ll>0) DravLines();
   return(rates_total);
  }
//==========================================================================

void DravLines()
{
      DelObj();
      {
         int bb=iBarShift(_Symbol,1440,tt);
         datetime ttt=0;
         if (bb>=Box-1) ttt=iTime(_Symbol,1440,bb-Box+1)+1440*60-_Period;
         else
         ttt=iTime(_Symbol,1440,0)+1440*60*(Box-bb)-_Period;
         
         ObjectCreate("TwoFingerLineH",OBJ_TREND,0,tt,hh,ttt,hh);
         ObjectCreate("TwoFingerLineL",OBJ_TREND,0,tt,ll,ttt,ll);
         ObjectCreate("TwoFingerLineRR",OBJ_TREND,0,ttt,ll,ttt,hh);
         //ObjectCreate("TwoFingerLineLL",OBJ_TREND,0,tt,ll,tt,hh);

         ObjectSet("TwoFingerLineH",OBJPROP_WIDTH,0);
         ObjectSet("TwoFingerLineL",OBJPROP_WIDTH,0);
         ObjectSet("TwoFingerLineRR",OBJPROP_WIDTH,0);
         //ObjectSet("TwoFingerLineLL",OBJPROP_WIDTH,0);
         //ObjectSet("TwoFingerLineH",OBJPROP_STYLE,STYLE_DASH);
         //ObjectSet("TwoFingerLineL",OBJPROP_STYLE,STYLE_DASH);
         //ObjectSet("TwoFingerLineRR",OBJPROP_STYLE,STYLE_DASH);
         //ObjectSet("TwoFingerLineLL",OBJPROP_STYLE,STYLE_DASH);
         ObjectSet("TwoFingerLineH",OBJPROP_RAY,false);
         ObjectSet("TwoFingerLineL",OBJPROP_RAY,false);
         ObjectSet("TwoFingerLineRR",OBJPROP_RAY,false);
         ObjectSet("TwoFingerLineLL",OBJPROP_RAY,false);
         if (Direct0>0) ObjectSet("TwoFingerLineH",OBJPROP_COLOR,Blue);
         if (Direct0<0) ObjectSet("TwoFingerLineH",OBJPROP_COLOR,Red);
         if (Direct0>0) ObjectSet("TwoFingerLineL",OBJPROP_COLOR,Blue);
         if (Direct0<0) ObjectSet("TwoFingerLineL",OBJPROP_COLOR,Red);
         if (Direct0>0) ObjectSet("TwoFingerLineRR",OBJPROP_COLOR,Blue);
         if (Direct0<0) ObjectSet("TwoFingerLineRR",OBJPROP_COLOR,Red);
         //if (Direct0>0) ObjectSet("TwoFingerLineLL",OBJPROP_COLOR,Blue);
         //if (Direct0<0) ObjectSet("TwoFingerLineLL",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerLineH",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerLineL",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerLineRR",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerLineLL",OBJPROP_COLOR,Red);
         
         ObjectSet("TwoFingerLineH",OBJPROP_TIMEFRAMES,OBJ_PERIOD_H4);
         ObjectSet("TwoFingerLineL",OBJPROP_TIMEFRAMES,OBJ_PERIOD_H4);
         ObjectSet("TwoFingerLineRR",OBJPROP_TIMEFRAMES,OBJ_PERIOD_H4);
      }

      {
         int bh=iHighest(_Symbol,1440,MODE_HIGH,5,SBar-2);
         int bl=iLowest(_Symbol,1440,MODE_LOW,5,SBar-2);
         
         double h=iHigh(_Symbol,1440,bh);
         double l=iLow(_Symbol,1440,bl);
         datetime t1=iTime(_Symbol,1440,SBar+2);
         datetime t2=t1+1440*60*7-_Period*60;
//         Comment (t2);
         
         ObjectCreate("TwoFingerBox",OBJ_RECTANGLE,0,t1,h,t2,l);
         ObjectSet("TwoFingerBox",OBJPROP_WIDTH,1);
         ObjectSet("TwoFingerBox",OBJPROP_BACK,false);
         ObjectSet("TwoFingerBox",OBJPROP_COLOR,Red);
         ObjectSet("TwoFingerBox",OBJPROP_TIMEFRAMES,OBJ_PERIOD_H4);
         
         tt=iTime(_Symbol,1440,SBar)+240*60*3;
         if (Direct0>0) ObjectCreate("TwoFingerArr",OBJ_ARROW_UP,0,tt,(h+l)/2);
         if (Direct0<0) ObjectCreate("TwoFingerArr",OBJ_ARROW_DOWN,0,tt,h);//(h+l)/2);
         if (Direct0>0) ObjectSet("TwoFingerBox",OBJPROP_COLOR,Blue);
         if (Direct0<0) ObjectSet("TwoFingerBox",OBJPROP_COLOR,Red);
         if (Direct0>0) ObjectSet("TwoFingerArr",OBJPROP_COLOR,Blue);
         if (Direct0<0) ObjectSet("TwoFingerArr",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerArr",OBJPROP_COLOR,Red);
         //ObjectSet("TwoFingerArr",OBJPROP_WIDTH,1);
         //ObjectSet("TwoFingerArr",OBJPROP_TIMEFRAMES,OBJ_PERIOD_H4|OBJ_PERIOD_D1);
//      Comment (bh,"   ",bl);
      }

}

//+------------------------------------------------------------------+
void DelObj()
{
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string name=ObjectName(i);
      if (StringSubstr(name,0,9)=="TwoFinger")
      ObjectDelete(name);
   }
}
//====================================================================
