Fibo Previous Day

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

//|                                            Fibo Previous Day.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.000"

#property description "Fibo at the prices of the previous day"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

//---

bool     glabal_error   = false;

string   m_fibo_name    = "FPD Fibo ";

datetime m_prev_bars    = 0;           // "0" -> D'1970.01.01 00:00';

MqlRates m_rates_D1[];                 // rates D1

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

//| 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);

     }

   ArraySetAsSeries(m_rates_D1,true);

//---

   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);

//---

   int start_pos=0,count=3;

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

      return(rates_total);

//--- we work only at the time of the birth of new bar

   datetime time_0=m_rates_D1[0].time;

   if(time_0==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_0;

//---

   MqlRates m_rates_M1[];

   ArraySetAsSeries(m_rates_M1,true);

   int count_M1=CopyRates(Symbol(),PERIOD_M1,m_rates_D1[1].time,m_rates_D1[0].time,m_rates_M1);

   if(count_M1<2)

     {

      time_0=0;

      return(rates_total);

     }

   datetime time_high=0, time_low=0;

   double price_high=DBL_MIN, price_low=DBL_MAX;

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

     {

      if(m_rates_M1[i].high>price_high)

        {

         time_high=m_rates_M1[i].time;

         price_high=m_rates_M1[i].high;

        }

      if(m_rates_M1[i].low<price_low)

        {

         time_low=m_rates_M1[i].time;

         price_low=m_rates_M1[i].low;

        }

     }

//---

   double   fibo_price_start  = (time_high<time_low)?price_high:price_low;

   double   fibo_price_end    = (time_high<time_low)?price_low:price_high;

   datetime fibo_time_start   = (time_high<time_low)?time_high:time_low;

   datetime fibo_time_end     = (time_high<time_low)?time_low:time_high;

//---

   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=true,    // 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---