High and Low Magnet

Author: Copyright © 2019, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
High and Low Magnet
ÿþ//+------------------------------------------------------------------+

//|                                          High and Low Magnet.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//---

bool     need_move= false;

datetime time1    = 0;

datetime time2    = 0;

string   name     = "";

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//---  

   need_move= false;

   time1    = 0;

   time2    = 0;

   name     = "";

//---

   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[])

  {

//---

   if(prev_calculated==0)

      Print(__FUNCTION__," prev_calculated == 0");

   if(need_move)

     {

      double max=DBL_MIN;

      double min=DBL_MAX;

      for(int i=rates_total-1;i>=0;i--)

        {

         if(time[i]<time1)

            break;

         if(time[i]>=time1 && time[i]<=time2)

           {

            if(high[i]>max)

               max=high[i];

            if(low[i]<min)

               min=low[i];

           }

        }

      need_move=false;

      if(max!=DBL_MIN && min!=DBL_MAX)

        {

         //--- move the anchor point 

         if(!ObjectMove(0,name,0,time1,max))

           {

            Print(__FUNCTION__,

                  ": failed to move the anchor point! Error code = ",GetLastError());

            need_move=false;

            return(rates_total);

           }

         //--- move the anchor point 

         if(!ObjectMove(0,name,1,time2,min))

           {

            Print(__FUNCTION__,

                  ": failed to move the anchor point! Error code = ",GetLastError());

            need_move=false;

            return(rates_total);

           }

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| ChartEvent function                                              |

//+------------------------------------------------------------------+

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_OBJECT_DRAG)

     {

      if(ObjectGetInteger(0,sparam,OBJPROP_TYPE,0)==OBJ_RECTANGLE)

        {

         string comm="<O ?5@5<5I5==>3> 3@0D8G5A:>3> >1J5:B0 ";

         time1=(datetime)(ObjectGetInteger(0,sparam,OBJPROP_TIME,0));

         time2=(datetime)(ObjectGetInteger(0,sparam,OBJPROP_TIME,1));

         name=sparam;

         if((long)time1==0 || (long)time2==0)

            return;

         need_move=true;

        }

      Print(__FUNCTION__,"; CHARTEVENT_OBJECT_DRAG");

     }

  }

//+------------------------------------------------------------------+

Comments