Fibo Open High Day

Author: Copyright © 2020, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Fibo Open High Day
ÿþ//+------------------------------------------------------------------+

//|                                           Fibo Open High Day.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input int      Input1=9;

bool glabal_error=false;

string m_fibo_name="FHLD Fibo";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Period()>=PERIOD_D1)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "5@8>4 3@0D8:0 =5 <>65B 1KBL @025= 8;8 1>;LH5 'D1'!":

                      "The chart period cannot be equal to or greater than 'D1'!";

      Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      glabal_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   ObjectDelete(0,m_fibo_name);

  }

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

//| 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(glabal_error)

      return(0);

   if(rates_total<10)

      return(0);

//---

   MqlRates rates_D1[];

   ArraySetAsSeries(rates_D1,true);

   int start_pos=0,count=3;

   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_D1)!=count)

      return(rates_total);

//---

   double   fibo_price_start  = rates_D1[0].open;

   double   fibo_price_end    = rates_D1[0].high;

   datetime fibo_time_start   = rates_D1[0].time;

   datetime fibo_time_end     = TimeCurrent();

//---

   if(ObjectFind(0,m_fibo_name)<0)

      FiboLevelsCreate(0,m_fibo_name,0,fibo_time_start,fibo_price_start,fibo_time_end,fibo_price_end);

   else

     {

      FiboLevelsPointChange(0,m_fibo_name,0,fibo_time_start,fibo_price_start);

      FiboLevelsPointChange(0,m_fibo_name,1,fibo_time_end,fibo_price_end);

     }

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

   return(rates_total);

  }

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

//| Create Fibonacci Retracement by the given coordinates            |

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

bool FiboLevelsCreate(const long            chart_ID=0,        // chart's ID

                      const string          name="FiboLevels", // object name

                      const int             sub_window=0,      // subwindow index

                      datetime              time1=0,           // first point time

                      double                price1=0,          // first point price

                      datetime              time2=0,           // second point time

                      double                price2=0,          // second point price

                      const color           clr=clrDarkViolet, // object color

                      const ENUM_LINE_STYLE style=STYLE_SOLID, // object line style

                      const int             width=1,           // object line width

                      const bool            back=false,        // in the background

                      const bool            selection=false,   // highlight to move

                      const bool            ray_left=false,    // object's continuation to the left

                      const bool            ray_right=false,   // object's continuation to the right

                      const bool            hidden=true,       // hidden in the object list

                      const long            z_order=0)         // priority for mouse click

  {

//--- set anchor points' coordinates if they are not set

   ChangeFiboLevelsEmptyPoints(time1,price1,time2,price2);

//--- reset the error value

   ResetLastError();

//--- Create Fibonacci Retracement by the given coordinates

   if(!ObjectCreate(chart_ID,name,OBJ_FIBO,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create \"Fibonacci Retracement\"! Error code = ",GetLastError());

      return(false);

     }

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

   long levels=ObjectGetInteger(chart_ID,name,OBJPROP_LEVELS);

   for(int i=0; i<levels; i++)

      ObjectSetInteger(chart_ID,name,OBJPROP_LEVELCOLOR,i,clr);

//--- set line style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the channel for moving

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- enable (true) or disable (false) the mode of continuation of the object's display to the left

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_LEFT,ray_left);

//--- enable (true) or disable (false) the mode of continuation of the object's display to the right

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

   return(true);

  }

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

//| Check the values of Fibonacci Retracement anchor points and set  |

//| default values for empty ones                                    |

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

void ChangeFiboLevelsEmptyPoints(datetime &time1,double &price1,

                                 datetime &time2,double &price2)

  {

//--- if the second point's time is not set, it will be on the current bar

   if(!time2)

      time2=TimeCurrent();

//--- if the second point's price is not set, it will have Bid value

   if(!price2)

      price2=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- if the first point's time is not set, it is located 9 bars left from the second one

   if(!time1)

     {

      //--- array for receiving the open time of the last 10 bars

      datetime temp[10];

      CopyTime(Symbol(),Period(),time2,10,temp);

      //--- set the first point 9 bars left from the second one

      time1=temp[0];

     }

//--- if the first point's price is not set, move it 200 points below the second one

   if(!price1)

      price1=price2-200*SymbolInfoDouble(Symbol(),SYMBOL_POINT);

  }

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

//| Move Fibonacci Retracement anchor point                          |

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

bool FiboLevelsPointChange(const long   chart_ID=0,        // chart's ID

                           const string name="FiboLevels", // object name

                           const int    point_index=0,     // anchor point index

                           datetime     time=0,            // anchor point time coordinate

                           double       price=0)           // anchor point price coordinate

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move the anchor point

   if(!ObjectMove(chart_ID,name,point_index,time,price))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- successful execution

   return(true);

  }

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

Comments