Pivots 5 types v9

Author: Copyright 2022, Yury
1 Views
0 Downloads
0 Favorites
Pivots 5 types v9
ÿþ//+------------------------------------------------------------------+

//|                                               Pivots 5 types.mq4 |

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

#property copyright "Copyright 2022, Yury"

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

#property strict

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_label1  "R4"

#property indicator_label2  "R3"

#property indicator_label3  "R2"

#property indicator_label4  "R1"

#property indicator_label5  "P"

#property indicator_label6  "S1"

#property indicator_label7  "S2"

#property indicator_label8  "S3"

#property indicator_label9  "S4"

#property indicator_type1   DRAW_LINE

#property indicator_type2   DRAW_LINE

#property indicator_type3   DRAW_LINE

#property indicator_type4   DRAW_LINE

#property indicator_type5   DRAW_LINE

#property indicator_type6   DRAW_LINE

#property indicator_type7   DRAW_LINE

#property indicator_type8   DRAW_LINE

#property indicator_type9   DRAW_LINE

#property indicator_color1  clrGreen

#property indicator_color2  clrGreen

#property indicator_color3  clrGreen

#property indicator_color4  clrGreen

#property indicator_color5  clrBlue

#property indicator_color6  clrRed

#property indicator_color7  clrRed

#property indicator_color8  clrRed

#property indicator_color9  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_style2  STYLE_SOLID

#property indicator_style3  STYLE_SOLID

#property indicator_style4  STYLE_SOLID

#property indicator_style5  STYLE_SOLID

#property indicator_style6  STYLE_SOLID

#property indicator_style7  STYLE_SOLID

#property indicator_style8  STYLE_SOLID

#property indicator_style9  STYLE_SOLID

#property indicator_width1  1

#property indicator_width2  1

#property indicator_width3  1

#property indicator_width4  1

#property indicator_width5  1

#property indicator_width6  1

#property indicator_width7  1

#property indicator_width8  1

#property indicator_width9  1



enum pivot

{

   piv1 = 1, // Classic

   piv2 = 2, // Fibonacci

   piv3 = 3, // Camarilla

   piv4 = 4, // Woodie

   piv5 = 5, // DeMark

};

enum timeframes

{

   tf1 = 0,      // Current period

   tf2 = 1,      // 1 minute

   tf3 = 5,      // 5 minutes

   tf4 = 15,     // 15 minutes

   tf5 = 30,     // 30 minutes

   tf6 = 60,     // 1 hour

   tf7 = 240,    // 4 hours

   tf8 = 1440,   // 1 day

   tf9 = 10080,  // 1 week

   tf10 = 43200, // 1 month

   tf11 = 518400,// 1 year

};



input pivot PivType=piv1;  // Pivot

input timeframes TF=tf8;   // Timeframe



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

int TF2;

string pref_glob;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   IndicatorBuffers(9);

   SetIndexBuffer(0, LineBuf1);

   SetIndexBuffer(1, LineBuf2);

   SetIndexBuffer(2, LineBuf3);

   SetIndexBuffer(3, LineBuf4);

   SetIndexBuffer(4, LineBuf5);

   SetIndexBuffer(5, LineBuf6);

   SetIndexBuffer(6, LineBuf7);

   SetIndexBuffer(7, LineBuf8);

   SetIndexBuffer(8, LineBuf9);

   TF2=TF;

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

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,pref_glob,0,OBJ_ARROW);

  }

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

//| 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(TF2==tf11) TF2=tf10;

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

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



   ArrowRightPriceCreate(pref_glob+"RightPriceR4",time[0],LineBuf1[0],clrGreen);

   ArrowRightPriceCreate(pref_glob+"RightPriceR3",time[0],LineBuf2[0],clrGreen);

   ArrowRightPriceCreate(pref_glob+"RightPriceR2",time[0],LineBuf3[0],clrGreen);

   ArrowRightPriceCreate(pref_glob+"RightPriceR1",time[0],LineBuf4[0],clrGreen);

   ArrowRightPriceCreate(pref_glob+"RightPriceP", time[0],LineBuf5[0],clrBlue);

   ArrowRightPriceCreate(pref_glob+"RightPriceS1",time[0],LineBuf6[0],clrRed);

   ArrowRightPriceCreate(pref_glob+"RightPriceS2",time[0],LineBuf7[0],clrRed);

   ArrowRightPriceCreate(pref_glob+"RightPriceS3",time[0],LineBuf8[0],clrRed);

   ArrowRightPriceCreate(pref_glob+"RightPriceS4",time[0],LineBuf9[0],clrRed);



   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]=R4;

      LineBuf2[i2]=R3;

      LineBuf3[i2]=R2;

      LineBuf4[i2]=R1;

      LineBuf5[i2]=P;

      LineBuf6[i2]=S1;

      LineBuf7[i2]=S2;

      LineBuf8[i2]=S3;

      LineBuf9[i2]=S4;

      return;

     }

   idx = i;

   

   double high=iHigh(_Symbol,TF2,i);

   double low=iLow(_Symbol,TF2,i);

   double close=iClose(_Symbol,TF2,i);

   double open=iOpen(_Symbol,TF2,i);

   if(TF == tf11)

   {

      int yearcur=TimeYear(iTime(_Symbol,TF2,i-1));

      int bars=iBars(_Symbol,TF2)-13;

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

      {

         if(yearcur != TimeYear(iTime(_Symbol,TF2,j)))

         {

            close=iClose(_Symbol,TF2,j);

            open=iOpen(_Symbol,TF2,j+11);

            double highest=0,lowest=9999999;

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

            {

               if(iHigh(_Symbol,TF2,k) > highest) highest=iHigh(_Symbol,TF2,k);

               if(iLow(_Symbol,TF2,k) < lowest) lowest=iLow(_Symbol,TF2,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]=R4;

   LineBuf2[i2]=R3;

   LineBuf3[i2]=R2;

   LineBuf4[i2]=R1;

   LineBuf5[i2]=P;

   LineBuf6[i2]=S1;

   LineBuf7[i2]=S2;

   LineBuf8[i2]=S3;

   LineBuf9[i2]=S4;

  }

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

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

  }

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

Comments