Pivot Points

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Pivot Points
ÿþ//+------------------------------------------------------------------+

//|                                                 Pivot Points.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

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

//| Enum Pivot Point                                                 |

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

enum ENUM_PIVOT_POINT

  {

   yesterday      = 0,  // Yesterday's prices only

   yesterday_today= 1,  // Yesterday and today's prices

  };

//--- input parameters

input ENUM_PIVOT_POINT  InpPivot    = yesterday_today;   // Pivot Point:

//---

datetime m_left_time    = 0,        m_right_time= 0;

double   m_r2_price     = 0.0,      m_r1_price  = 0.0,   m_s1_price  = 0.0,   m_s2_price  = 0.0,   m_p_price   = 0.0;

string   m_prefix       = "PPOffset ";

string   m_r2_name      = m_prefix+"R2";

string   m_r1_name      = m_prefix+"R1";

string   m_s1_name      = m_prefix+"S1";

string   m_s2_name      = m_prefix+"S2";

string   m_p_name       = m_prefix+"P";

//---

string   m_rec_yesterday_name             = m_prefix+"Yesterday";

datetime m_rec_yesterday_left_up_time     = 0;

datetime m_rec_yesterday_right_down_time  = 0;

double   m_rec_yesterday_left_up_price    = 0.0;

double   m_rec_yesterday_right_down_price = 0.0;

//---

string   m_rec_today_name                 = m_prefix+"Today";

datetime m_rec_today_left_up_time         = 0;

datetime m_rec_today_right_down_time      = 0;

double   m_rec_today_left_up_price        = 0.0;

double   m_rec_today_right_down_price     = 0.0;

//---

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create a timer with a 1 second period

   EventSetTimer(3);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//--- destroy the timer after completing the work

   EventKillTimer();

//--- removes all objects of the specified type using prefix in object names

   ObjectsDeleteAll(0,m_prefix,0,OBJ_TREND);

   ObjectsDeleteAll(0,m_prefix,0,OBJ_RECTANGLE);

  }

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

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

     {

      OnTimer();

      if(!CalculatePivot())

         return(0);

      TrendPointChange(0,m_r2_name,1,m_left_time,m_r2_price);

      TrendPointChange(0,m_r2_name,0,m_right_time,m_r2_price);

      //---

      TrendPointChange(0,m_r1_name,1,m_left_time,m_r1_price);

      TrendPointChange(0,m_r1_name,0,m_right_time,m_r1_price);

      //---

      TrendPointChange(0,m_s1_name,1,m_left_time,m_s1_price);

      TrendPointChange(0,m_s1_name,0,m_right_time,m_s1_price);

      //---

      TrendPointChange(0,m_s2_name,1,m_left_time,m_s2_price);

      TrendPointChange(0,m_s2_name,0,m_right_time,m_s2_price);

      //---

      TrendPointChange(0,m_p_name,1,m_left_time,m_p_price);

      TrendPointChange(0,m_p_name,0,m_right_time,m_p_price);

      //---

      RectanglePointChange(0,m_rec_yesterday_name,1,m_rec_yesterday_left_up_time,m_rec_yesterday_left_up_price);

      RectanglePointChange(0,m_rec_yesterday_name,0,m_rec_yesterday_right_down_time,m_rec_yesterday_right_down_price);

      //---

      RectanglePointChange(0,m_rec_today_name,1,m_rec_today_left_up_time,m_rec_today_left_up_price);

      RectanglePointChange(0,m_rec_today_name,0,m_rec_today_right_down_time,m_rec_today_right_down_price);

      //---

      return(rates_total);

     }

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

   datetime time_m1[1];

   if(CopyTime(Symbol(),PERIOD_M1,0,1,time_m1)<1)

      return(0);

   if(time_m1[0]==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_m1[0];

//---

   if(!CalculatePivot())

      return(0);

   TrendPointChange(0,m_r2_name,0,m_left_time,m_r2_price);

   TrendPointChange(0,m_r2_name,1,m_right_time,m_r2_price);

//---

   TrendPointChange(0,m_r1_name,0,m_left_time,m_r1_price);

   TrendPointChange(0,m_r1_name,1,m_right_time,m_r1_price);

//---

   TrendPointChange(0,m_s1_name,0,m_left_time,m_s1_price);

   TrendPointChange(0,m_s1_name,1,m_right_time,m_s1_price);

//---

   TrendPointChange(0,m_s2_name,0,m_left_time,m_s2_price);

   TrendPointChange(0,m_s2_name,1,m_right_time,m_s2_price);

//---

   TrendPointChange(0,m_p_name,0,m_left_time,m_p_price);

   TrendPointChange(0,m_p_name,1,m_right_time,m_p_price);

//---

   RectanglePointChange(0,m_rec_yesterday_name,1,m_rec_yesterday_left_up_time,m_rec_yesterday_left_up_price);

   RectanglePointChange(0,m_rec_yesterday_name,0,m_rec_yesterday_right_down_time,m_rec_yesterday_right_down_price);

//---

   RectanglePointChange(0,m_rec_today_name,1,m_rec_today_left_up_time,m_rec_today_left_up_price);

   RectanglePointChange(0,m_rec_today_name,0,m_rec_today_right_down_time,m_rec_today_right_down_price);

//---

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   if(ObjectFind(0,m_r2_name)<0)

      TrendCreate(0,m_r2_name,clrYellowGreen,STYLE_DASH,2);

   if(ObjectFind(0,m_r1_name)<0)

      TrendCreate(0,m_r1_name,clrYellowGreen,STYLE_DASH,2);

   if(ObjectFind(0,m_s1_name)<0)

      TrendCreate(0,m_s1_name,clrDodgerBlue,STYLE_DASH,2);

   if(ObjectFind(0,m_s2_name)<0)

      TrendCreate(0,m_s2_name,clrDodgerBlue,STYLE_DASH,2);

   if(ObjectFind(0,m_p_name)<0)

      TrendCreate(0,m_p_name,clrMagenta);

   if(ObjectFind(0,m_rec_yesterday_name)<0)

      RectangleCreate(0,m_rec_yesterday_name,clrBlack,STYLE_DASH,2);

   if(ObjectFind(0,m_rec_today_name)<0)

      RectangleCreate(0,m_rec_today_name,clrBlack,STYLE_DASH,2);

  }

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

//| Calculate Pivot                                                  |

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

bool CalculatePivot(void)

  {

//---

   MqlRates rates_d1[],rates_h1[],rates_m1[];

   ArraySetAsSeries(rates_d1,true);

   ArraySetAsSeries(rates_h1,true);

   ArraySetAsSeries(rates_m1,true);

   int start_pos=0;

   int count=6;

   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_d1)<count)

      return(false);

   /*

      R2=P+(H-L)=P+(R1-S1)

      R1=(P*2)-L

      S1=(P*2)-H

      S2=P-(H-L)=P-(R1-S1)

      P=(H+L+C)/3 OR P=(Today's O+Yesterday's (H+L+C))/4



      m_r2_price - 1.17233

      |

      m_r1_price - 1.16515

      |

      m_s1_price - 1.15460

      |

      m_s2_price - 1.15123

   */

   if(InpPivot==yesterday)

      m_p_price                        = (rates_d1[1].high+rates_d1[1].low+rates_d1[1].close)/3.0;

   else

      m_p_price=(rates_d1[0].open+(rates_d1[1].high+rates_d1[1].low+rates_d1[1].close))/4.0;

//---

   m_rec_yesterday_left_up_time     = rates_d1[1].time;

   m_rec_yesterday_right_down_time  = rates_d1[0].time;

   m_rec_yesterday_left_up_price    = rates_d1[1].high;

   m_rec_yesterday_right_down_price = rates_d1[1].low;

//---

   m_rec_today_left_up_time         = rates_d1[0].time;

   m_rec_today_right_down_time      = TimeCurrent();

   m_rec_today_left_up_price        = rates_d1[0].high;

   m_rec_today_right_down_price     = rates_d1[0].low;

//---

   m_s2_price  = m_p_price-(rates_d1[1].high-rates_d1[1].low);

   m_s1_price  = (m_p_price*2.0)-rates_d1[1].high;

   m_r1_price  = (m_p_price*2.0)-rates_d1[1].low;

   m_r2_price  = m_p_price+(rates_d1[1].high-rates_d1[1].low);

//---

   m_left_time=(InpPivot==yesterday)?rates_d1[0].time:rates_d1[1].time;

   m_right_time=TimeCurrent();

//---

   return(true);

  }

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

//| Create a trend line by the given coordinates                     |

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

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

                 const string          name="TrendLine",  // line name

                 const color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 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 bool            back=false,        // in the background

                 const bool            selection=false,   // highlight to move

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

                 const bool            ray_right=true,    // line'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

   ChangeTrendEmptyPoints(time1,price1,time2,price2);

//--- reset the error value

   ResetLastError();

//--- create a trend line by the given coordinates

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

     {

      Print(__FUNCTION__,

            ": failed to create a trend line! Error code = ",GetLastError());

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set line display 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 moving the line by mouse

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

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move trend line anchor point                                     |

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

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

                      const string name="TrendLine", // line 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 trend line's 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);

  }

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

//| Check the values of trend line's anchor points and set default   |

//| values for empty ones                                            |

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

void ChangeTrendEmptyPoints(datetime &time1,double &price1,

                            datetime &time2,double &price2)

  {

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

   if(!time1)

      time1=TimeCurrent();

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

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

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

   if(!time2)

     {

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

      datetime temp[10];

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

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

      time2=temp[0];

     }

//--- if the second point's price is not set, it is equal to the first point's one

   if(!price2)

      price2=price1;

  }

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

//| Create rectangle by the given coordinates                        |

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

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

                     const string          name="Rectangle",  // rectangle name

                     const color           clr=clrRed,        // rectangle color

                     const ENUM_LINE_STYLE style=STYLE_SOLID, // style of rectangle lines

                     const int             width=1,           // width of rectangle lines

                     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 bool            fill=false,        // filling rectangle with color

                     const bool            back=false,        // in the background

                     const bool            selection=false,   // highlight to move

                     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

   ChangeRectangleEmptyPoints(time1,price1,time2,price2);

//--- reset the error value

   ResetLastError();

//--- create a rectangle by the given coordinates

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

     {

      Print(__FUNCTION__,

            ": failed to create a rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- set rectangle color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the style of rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the rectangle

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

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

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

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

//--- successful execution

   return(true);

  }

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

//| Move the rectangle anchor point                                  |

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

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

                          const string name="Rectangle", // rectangle 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);

  }

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

//| Check the values of rectangle's anchor points and set default    |

//| values for empty ones                                            |

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

void ChangeRectangleEmptyPoints(datetime &time1,double &price1,

                                datetime &time2,double &price2)

  {

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

   if(!time1)

      time1=TimeCurrent();

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

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

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

   if(!time2)

     {

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

      datetime temp[10];

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

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

      time2=temp[0];

     }

//--- if the second point's price is not set, move it 300 points lower than the first one

   if(!price2)

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

  }

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

Comments