Pivot Points 2

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

//|                                               Pivot Points 2.mq5 |

//|                         Copyright © 2020-2021, Vladimir Karputov |

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

#property copyright "Copyright © 2022-2021, Vladimir Karputov"

#property version   "2.002"

#property description "There are no rectangles or trend lines. Everything is done on indicator buffers"

#property indicator_chart_window

#property indicator_buffers      5

#property indicator_plots        5

//--- plot R2

#property indicator_label1  "R2"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrYellowGreen

#property indicator_style1  STYLE_DOT

#property indicator_width1  1

//--- plot R1

#property indicator_label2  "R1"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellowGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot P

#property indicator_label3  "P"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- plot S1

#property indicator_label4  "S1"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrDodgerBlue

#property indicator_style4  STYLE_SOLID

#property indicator_width4  2

//--- plot S2

#property indicator_label5  "S2"

#property indicator_type5   DRAW_LINE

#property indicator_color5  clrDodgerBlue

#property indicator_style5  STYLE_DOT

#property indicator_width5  1

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

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

input bool              InpR2Use    = true;              // Use 'R2'

input bool              InpR1Use    = true;              // Use 'R1'

input bool              InpPUse     = true;              // Use 'P'

input bool              InpS1Use    = true;              // Use 'S1'

input bool              InpS2Use    = true;              // Use 'S2'

//--- indicator buffers

double   R2[];

double   R1[];

double   P[];

double   S1[];

double   S2[];

//---

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;

//---

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

int      m_count_errors = 0;

string   m_error="";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,R2,INDICATOR_DATA);

   SetIndexBuffer(1,R1,INDICATOR_DATA);

   SetIndexBuffer(2,P,INDICATOR_DATA);

   SetIndexBuffer(3,S1,INDICATOR_DATA);

   SetIndexBuffer(4,S2,INDICATOR_DATA);

//--- the 0.0 (empty) value will mot participate in drawing

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0);

//---

   MqlRates rates_d1[];

   ArraySetAsSeries(rates_d1,true);

   int start_pos=0;

   int count=2;

   CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_d1);

//---

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

  {

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      if(!CalculatePivot(time[i]))

        {

         R2[i]=0.0;

         R1[i]=0.0;

         P[i]=0.0;

         S1[i]=0.0;

         S2[i]=0.0;

         m_count_errors++;

        }

      else

        {

         if(InpR2Use)

            R2[i]=m_r2_price;

         if(InpR1Use)

            R1[i]=m_r1_price;

         if(InpPUse)

            P[i]=m_p_price;

         if(InpS1Use)

            S1[i]=m_s1_price;

         if(InpS2Use)

            S2[i]=m_s2_price;

        }

      if(i>0)

        {

         if(R2[i]!=R2[i-1])

            R2[i-1]=0.0;

         if(R1[i]!=R1[i-1])

            R1[i-1]=0.0;

         if(P[i]!=P[i-1])

            P[i-1]=0.0;

         if(S1[i]!=S1[i-1])

            S1[i-1]=0.0;

         if(S2[i]!=S2[i-1])

            S2[i-1]=0.0;

        }

     }

   if(m_count_errors>rates_total/2)

     {

      Print(m_error);

      m_count_errors=0;

      return(0);

     }

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

   return(rates_total);

  }

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

//| Calculate Pivot                                                  |

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

bool CalculatePivot(datetime open_time)

  {

//---

   MqlRates rates_d1[];

   ArraySetAsSeries(rates_d1,true);

   int count=2;

   ResetLastError();

   if(CopyRates(Symbol(),PERIOD_D1,open_time,count,rates_d1)!=count)

     {

      m_error="CopyRates("+

              Symbol()+","+

              "PERIOD_D1"+","+

              TimeToString(open_time,TIME_DATE|TIME_MINUTES)+","+

              IntegerToString(count)+","+

              "rates_d1)!="+

              IntegerToString(count)+", ERROR: "+IntegerToString(GetLastError());

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

//---

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