Pivots 5 types

Author: Copyright 2022, Yury
Price Data Components
2 Views
0 Downloads
0 Favorites
Pivots 5 types
ÿþ//+------------------------------------------------------------------+

//|                                               Pivots 5 types.mq5 |

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

#property copyright "Copyright 2022, Yury"

#property link      "https://www.mql5.com/ru/users/yur"

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_plots   9

#property indicator_label1  "P"

#property indicator_label2  "S1"

#property indicator_label3  "S2"

#property indicator_label4  "S3"

#property indicator_label5  "S4"

#property indicator_label6  "R1"

#property indicator_label7  "R2"

#property indicator_label8  "R3"

#property indicator_label9  "R4"





enum pivot

{

   piv1 = 1, // Classic

   piv2 = 2, // Fibonacci

   piv3 = 3, // Camarilla

   piv4 = 4, // Woodie

   piv5 = 5, // DeMark

};

enum timeframes

{

   tf0 = 0,      // Current period

   tf1 = 1,      // 1 minute

   tf2 = 2,      // 2 minutes

   tf3 = 3,      // 3 minutes

   tf4 = 4,      // 4 minutes

   tf5 = 5,      // 5 minutes

   tf6 = 6,      // 6 minutes

   tf7 = 10,     // 10 minutes

   tf8 = 12,     // 12 minutes

   tf9 = 15,     // 15 minutes

   tf10 = 20,    // 20 minutes

   tf11 = 30,    // 30 minutes

   tf12 = 60,    // 1 hour

   tf13 = 120,   // 2 hours

   tf14 = 180,   // 3 hours

   tf15 = 240,   // 4 hours

   tf16 = 360,   // 6 hours

   tf17 = 480,   // 8 hours

   tf18 = 720,   // 12 hours

   tf19 = 1440,  // 1 day

   tf20 = 10080, // 1 week

   tf21 = 43200, // 1 month

   tf22 = 518400,// 1 year

};

enum e_width

{

   width1 = 1,      // 1

   width2 = 2,      // 2

   width3 = 3,      // 3

   width4 = 4,      // 4

   width5 = 5,      // 5

};





input pivot PivType=piv1;              // Pivot

input timeframes TF=tf19;              // Timeframe

input string s1="-----COLOR-----";

input color color_r4=clrGreen;         // R4

input color color_r3=clrGreen;         // R3

input color color_r2=clrGreen;         // R2

input color color_r1=clrGreen;         // R1

input color color_p=clrBlue;           // P

input color color_s1=clrRed;           // S1

input color color_s2=clrRed;           // S2

input color color_s3=clrRed;           // S3

input color color_s4=clrRed;           // S4

input string s2="-----STYLE-----";

input ENUM_LINE_STYLE style_r4=STYLE_SOLID; // R4

input ENUM_LINE_STYLE style_r3=STYLE_SOLID; // R3

input ENUM_LINE_STYLE style_r2=STYLE_SOLID; // R2

input ENUM_LINE_STYLE style_r1=STYLE_SOLID; // R1

input ENUM_LINE_STYLE style_p=STYLE_SOLID;  // P

input ENUM_LINE_STYLE style_s1=STYLE_SOLID; // S1

input ENUM_LINE_STYLE style_s2=STYLE_SOLID; // S2

input ENUM_LINE_STYLE style_s3=STYLE_SOLID; // S3

input ENUM_LINE_STYLE style_s4=STYLE_SOLID; // S4

input string s3="-----WIDTH-----";

input e_width width_r4=width1;              // R4  

input e_width width_r3=width1;              // R3

input e_width width_r2=width1;              // R2

input e_width width_r1=width1;              // R1

input e_width width_p=width1;               // P

input e_width width_s1=width1;              // S1

input e_width width_s2=width1;              // S2

input e_width width_s3=width1;              // S3

input e_width width_s4=width1;              // S4



double LineBuf1[],LineBuf2[],LineBuf3[],LineBuf4[],LineBuf5[],LineBuf6[],LineBuf7[],LineBuf8[],LineBuf9[];

double Close[],Open[],High[],Low[];

datetime Time[];

string pref_glob;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   SetIndexBuffer(0, LineBuf1, INDICATOR_DATA);

   SetIndexBuffer(1, LineBuf2, INDICATOR_DATA);

   SetIndexBuffer(2, LineBuf3, INDICATOR_DATA);

   SetIndexBuffer(3, LineBuf4, INDICATOR_DATA);

   SetIndexBuffer(4, LineBuf5, INDICATOR_DATA);

   SetIndexBuffer(5, LineBuf6, INDICATOR_DATA);

   SetIndexBuffer(6, LineBuf7, INDICATOR_DATA);

   SetIndexBuffer(7, LineBuf8, INDICATOR_DATA);

   SetIndexBuffer(8, LineBuf9, INDICATOR_DATA);

   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(4, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(5, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(6, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(7, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(8, PLOT_DRAW_TYPE, DRAW_LINE);

   PlotIndexSetInteger(0, PLOT_LINE_STYLE, style_p);

   PlotIndexSetInteger(1, PLOT_LINE_STYLE, style_s1);

   PlotIndexSetInteger(2, PLOT_LINE_STYLE, style_s2);

   PlotIndexSetInteger(3, PLOT_LINE_STYLE, style_s3);

   PlotIndexSetInteger(4, PLOT_LINE_STYLE, style_s4);

   PlotIndexSetInteger(5, PLOT_LINE_STYLE, style_r1);

   PlotIndexSetInteger(6, PLOT_LINE_STYLE, style_r2);

   PlotIndexSetInteger(7, PLOT_LINE_STYLE, style_r3);

   PlotIndexSetInteger(8, PLOT_LINE_STYLE, style_r4);

   PlotIndexSetInteger(0, PLOT_LINE_WIDTH, width_p);

   PlotIndexSetInteger(1, PLOT_LINE_WIDTH, width_s1);

   PlotIndexSetInteger(2, PLOT_LINE_WIDTH, width_s2);

   PlotIndexSetInteger(3, PLOT_LINE_WIDTH, width_s3);

   PlotIndexSetInteger(4, PLOT_LINE_WIDTH, width_s4);

   PlotIndexSetInteger(5, PLOT_LINE_WIDTH, width_r1);

   PlotIndexSetInteger(6, PLOT_LINE_WIDTH, width_r2);

   PlotIndexSetInteger(7, PLOT_LINE_WIDTH, width_r3);

   PlotIndexSetInteger(8, PLOT_LINE_WIDTH, width_r4);

   PlotIndexSetInteger(0, PLOT_LINE_COLOR, color_p);

   PlotIndexSetInteger(1, PLOT_LINE_COLOR, color_s1);

   PlotIndexSetInteger(2, PLOT_LINE_COLOR, color_s2);

   PlotIndexSetInteger(3, PLOT_LINE_COLOR, color_s3);

   PlotIndexSetInteger(4, PLOT_LINE_COLOR, color_s4);

   PlotIndexSetInteger(5, PLOT_LINE_COLOR, color_r1);

   PlotIndexSetInteger(6, PLOT_LINE_COLOR, color_r2);

   PlotIndexSetInteger(7, PLOT_LINE_COLOR, color_r3);

   PlotIndexSetInteger(8, PLOT_LINE_COLOR, color_r4);

   

   ArraySetAsSeries(Close, true);

   ArraySetAsSeries(Open, true);

   ArraySetAsSeries(High, true);

   ArraySetAsSeries(Low, true);

   ArraySetAsSeries(Time, true);

   ArraySetAsSeries(LineBuf1, true);

   ArraySetAsSeries(LineBuf2, true);

   ArraySetAsSeries(LineBuf3, true);

   ArraySetAsSeries(LineBuf4, true);

   ArraySetAsSeries(LineBuf5, true);

   ArraySetAsSeries(LineBuf6, true);

   ArraySetAsSeries(LineBuf7, true);

   ArraySetAsSeries(LineBuf8, true);

   ArraySetAsSeries(LineBuf9, true);

   pref_glob="S"+IntegerToString(MathRand())+"_";

   

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,pref_glob,0,OBJ_ARROW_RIGHT_PRICE);

  }

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

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

  {

   ArraySetAsSeries(time, true);

   CopyClose(_Symbol, getTimeframe(TF), 0, Bars(_Symbol, getTimeframe(TF)), Close);

   CopyOpen(_Symbol, getTimeframe(TF), 0, Bars(_Symbol, getTimeframe(TF)), Open);

   CopyHigh(_Symbol, getTimeframe(TF), 0, Bars(_Symbol, getTimeframe(TF)), High);

   CopyLow(_Symbol, getTimeframe(TF), 0, Bars(_Symbol, getTimeframe(TF)), Low);

   CopyTime(_Symbol, getTimeframe(TF), 0, Bars(_Symbol, getTimeframe(TF)), Time);

   

   for(int i=(prev_calculated==0? rates_total>10000?10000:rates_total-2:2); i>=0 && !_StopFlag; i--)

      ShowPivots(iBarShift(_Symbol,getTimeframe(TF),time[i])+1, i);



   ArrowRightPriceCreate(pref_glob+"RightPriceP",time[0],LineBuf1[0],color_p);

   ArrowRightPriceCreate(pref_glob+"RightPriceS1",time[0],LineBuf2[0],color_s1);

   ArrowRightPriceCreate(pref_glob+"RightPriceS2",time[0],LineBuf3[0],color_s2);

   ArrowRightPriceCreate(pref_glob+"RightPriceS3",time[0],LineBuf4[0],color_s3);

   ArrowRightPriceCreate(pref_glob+"RightPriceS4",time[0],LineBuf5[0],color_s4);

   ArrowRightPriceCreate(pref_glob+"RightPriceR1",time[0],LineBuf6[0],color_r1);

   ArrowRightPriceCreate(pref_glob+"RightPriceR2",time[0],LineBuf7[0],color_r2);

   ArrowRightPriceCreate(pref_glob+"RightPriceR3",time[0],LineBuf8[0],color_r3);

   ArrowRightPriceCreate(pref_glob+"RightPriceR4",time[0],LineBuf9[0],color_r4);



   return(rates_total);

  }

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

//| draw pivots                                                      |

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

void ShowPivots(int i, int i2)

  {

   static double P,S1,S2,S3,S4,R1,R2,R3,R4;

   static int idx;

   if(idx == i)

     {

      LineBuf1[i2]=P;

      LineBuf2[i2]=S1;

      LineBuf3[i2]=S2;

      LineBuf4[i2]=S3;

      LineBuf5[i2]=S4;

      LineBuf6[i2]=R1;

      LineBuf7[i2]=R2;

      LineBuf8[i2]=R3;

      LineBuf9[i2]=R4;

      return;

     }

   idx = i;

   

   if(i<0 || i>=Bars(_Symbol, getTimeframe(TF))) return;

   

   double high=High[i];

   double low=Low[i];

   double close=Close[i];

   double open=Open[i];

   if(TF == tf22)

   {

      MqlDateTime tm,tm2;

      TimeToStruct(Time[i-1], tm);

      int bars=iBars(_Symbol,getTimeframe(TF))-13;

      for(int j=i; j<bars; j++)

      {

         TimeToStruct(Time[j], tm2);

         if(tm.year != tm2.year)

         {

            close=Close[j];

            open=Open[j+11];

            double highest=0,lowest=9999999;

            for(int k=j; k<j+12; k++)

            {

               if(High[k] > highest) highest=High[k];

               if(Low[k] < lowest) lowest=Low[k];

            }

            high=highest;

            low=lowest;

            break;

         }

      }

   } 

   

// pivots Classic

   if(PivType==piv1)

     {

      P=(high+low+close)/3;

      S1=(2*P)-high;

      S2=P-high+low;

      S3=low-2*(high-P);

      S4=low-3*(high-P);

      R1=(2*P)-low;

      R2=P+high-low;

      R3=high+2*(P-low);

      R4=high+3*(P-low);

     }

// pivots Fibonacci

   if(PivType==piv2)

     {

      P=(high+low+close)/3;

      S1=P-0.382*(high-low);

      S2=P-0.618*(high-low);

      S3=P-(high-low);

      S4=P-1.618*(high-low);

      R1=P+0.382*(high-low);

      R2=P+0.618*(high-low);

      R3=P+(high-low);

      R4=P+1.618*(high-low);

     }

// pivots Camarilla

   if(PivType==piv3)

     {

      P=(high+low+close)/3;

      S1=close-(high-low)*1.1/12;

      S2=close-(high-low)*1.1/6;

      S3=close-(high-low)*1.1/4;

      S4=close-(high-low)*1.1/2;

      R1=(high-low)*1.1/12+close;

      R2=(high-low)*1.1/6+close;

      R3=(high-low)*1.1/4+close;

      R4=(high-low)*1.1/2+close;

     }

// pivots Woodie

   if(PivType==piv4)

     {

      P=(high+low+2*close)/4;

      S1=2*P-high;

      S2=P-high+low;

      S3=S2-(P-S1);

      S4=S3-(P-S2);

      R1=2*P-low;

      R2=P+(high-low);

      R3=R2+(R1-P);

      R4=R3+(R2-P);

     }

// pivots DeMark

   if(PivType==piv5)

     {

      if(close < open)

         P=high+2*low+close;

      if(close > open)

         P=2*high+low+close;

      else

         P=high+low+2*close;

      S1=P/2-high;

      R1=P/2-low;

      P/=4;

     }



   LineBuf1[i2]=P;

   LineBuf2[i2]=S1;

   LineBuf3[i2]=S2;

   LineBuf4[i2]=S3;

   LineBuf5[i2]=S4;

   LineBuf6[i2]=R1;

   LineBuf7[i2]=R2;

   LineBuf8[i2]=R3;

   LineBuf9[i2]=R4;

  }

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

//| Create right-price arrow                                         |

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

bool ArrowRightPriceCreate(const string          name="RightPrice",

                           datetime              time=0,

                           double                price=0,

                           const color           clr=clrRed,

                           const long            chart_ID=0,

                           const int             sub_window=0,

                           const ENUM_LINE_STYLE style=STYLE_SOLID,

                           const int             width=1,

                           const bool            back=false,

                           const bool            selection=false,

                           const bool            hidden=true,

                           const long            z_order=0)

  {

   if(!ObjectCreate(chart_ID,name,OBJ_ARROW_RIGHT_PRICE,sub_window,time,price))

     {

      ObjectMove(chart_ID,name,0,time,price);

      return(false);

     }

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

   return(true);

  }

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

//| Get timeframe from int                                           |

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

ENUM_TIMEFRAMES  getTimeframe(int tf)

{

	switch (tf)

	{

		case 1: return(PERIOD_M1);

		case 2: return(PERIOD_M2);

		case 3: return(PERIOD_M3);

		case 4: return(PERIOD_M4);      

		case 5: return(PERIOD_M5);

		case 6: return(PERIOD_M6);

		case 10: return(PERIOD_M10);

		case 12: return(PERIOD_M12);

		case 15: return(PERIOD_M15);

		case 20: return(PERIOD_M20);

		case 30: return(PERIOD_M30);

		case 60: return(PERIOD_H1);

		case 120: return(PERIOD_H2);

		case 180: return(PERIOD_H3);

		case 240: return(PERIOD_H4);

		case 360: return(PERIOD_H6);

		case 480: return(PERIOD_H8);

		case 720: return(PERIOD_H12);

		case 1440: return(PERIOD_D1);

		case 10080: return(PERIOD_W1);

		case 43200: return(PERIOD_MN1);     

		case 518400: return(PERIOD_MN1);        

		default: return(PERIOD_CURRENT);

	}

}

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

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