PivotsSupportsResistances_D1_W1_MN1

Author: Copyright © 2021, x572intraday
0 Views
0 Downloads
0 Favorites
PivotsSupportsResistances_D1_W1_MN1
//+------------------------------------------------------------------+
//|                          PivotsSupportsResistances_D1_W1_MN1.mq5 |
//|                                   Copyright © 2021, x572intraday |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, x572intraday"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

struct TBuffer
  {
   color ClrArray[3];
  };

TBuffer ArrayOfClrArrays[3];

double LastHigh=0;
double Lastlow=0;
double Lastclose=0;
double P=0;
double R1, R2, R3;
double S1, S2, S3;
string line_types[7]={"P", "R1", "R2", "R3", "S1", "S2", "S3"};
MqlRates rates_period[];

ENUM_TIMEFRAMES PArray[3]={PERIOD_D1, PERIOD_W1, PERIOD_MN1};

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//   if(!GlobalVariableGet(_Symbol+": PSR_SingleActuation"))
   {
      R1=0; R2=0; R3=0;
      S1=0; S2=0; S3=0;

      ArrayOfClrArrays[0].ClrArray[0]=clrDarkSlateBlue;
      ArrayOfClrArrays[0].ClrArray[1]=clrDarkGreen;
      ArrayOfClrArrays[0].ClrArray[2]=clrMaroon;
      ArrayOfClrArrays[1].ClrArray[0]=clrBlue;
      ArrayOfClrArrays[1].ClrArray[1]=clrSeaGreen;
      ArrayOfClrArrays[1].ClrArray[2]=clrCrimson;
      ArrayOfClrArrays[2].ClrArray[0]=clrRoyalBlue;
      ArrayOfClrArrays[2].ClrArray[1]=clrYellowGreen;
      ArrayOfClrArrays[2].ClrArray[2]=clrTomato;
   }

   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 calculated=iBars(_Symbol,PArray[0]);

   //if(GlobalVariableGet(_Symbol+": PSR_SingleActuation") && calculated==GlobalVariableGet(_Symbol+": PSR_bars_calculated"))
   //{
      //GlobalVariableSet(_Symbol+": PSR_SingleActuation",1);
      //GlobalVariableSet(_Symbol+": PSR_bars_calculated",calculated);
      //return(0);
   //}

   if(calculated<=0)
      return(0);

   if(!GlobalVariableGet(_Symbol+": PSR_bars_calculated") || calculated!=GlobalVariableGet(_Symbol+": PSR_bars_calculated") ||
      calculated>GlobalVariableGet(_Symbol+": PSR_bars_calculated")+1)
   {
      if(!GlobalVariableGet(_Symbol+": PSR_SingleActuation") || (GlobalVariableGet(_Symbol+": PSR_SingleActuation") &&
         calculated!=GlobalVariableGet(_Symbol+": PSR_bars_calculated")))
      {
         for(int p=0; p<ArraySize(PArray); p++)
         {
            CopyRates(Symbol(),PArray[p],0,2+40,rates_period);

            ArraySetAsSeries(rates_period,true);

            Lastclose = rates_period[1].close;
            LastHigh = rates_period[1].high;
            Lastlow = rates_period[1].low;

            //---- Calculate Pivots
            P = (LastHigh + Lastlow + Lastclose)/3;
            R1 = 2*P-Lastlow;
            S1 = 2*P-LastHigh;
            R2 = P+(R1 - S1);
            S2 = P-(R1 - S1);
            R3 = P+(R2 - S2);
            S3 = P-(R2 - S2);

            double line_prices[7]={P, R1, R2, R3, S1, S2, S3};
            color clr;
            int visibility=OBJ_ALL_PERIODS; // 2097151
            ENUM_LINE_STYLE style=STYLE_SOLID;

            if(p==0)
            {
               style=STYLE_DOT;
               visibility=524287;
            }
            else
               if(p==1)
               {
                  style=STYLE_DASH;
                  visibility=1048575;
               }

            for(int lt=0; lt<ArraySize(line_types); lt++)
            {
               if(line_types[lt]=="P")
                  clr=ArrayOfClrArrays[p].ClrArray[0];
               else
                  if(StringSubstr(line_types[lt],0,1)=="R")
                     clr=ArrayOfClrArrays[p].ClrArray[1];
                  else
                     clr=ArrayOfClrArrays[p].ClrArray[2];

               DrawTheLine(line_types[lt], StringSubstr(EnumToString(PArray[p]),7), line_prices[lt], 8+p*2, p, style, clr, visibility);
            }
         }
      }
   }

   GlobalVariableSet(_Symbol+": PSR_SingleActuation",1);
   GlobalVariableSet(_Symbol+": PSR_bars_calculated",calculated);

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Line drawing and moving function                                 |
//+------------------------------------------------------------------+
  void DrawTheLine(string LineType, string tf, double price, int fontsize, int width, ENUM_LINE_STYLE style, color clr, int visibility)
  {
// при PSR_SingleActuation по понятным причинам не срабатывают перемещения лейблов при переходе по ТФам,
// поэтому заменяем обратно на rates_period[0].time
   if(ObjectFind(0,tf+" "+LineType+" label")<0)
   {
      //ObjectCreate(0,tf+" "+LineType+" label",OBJ_TEXT,0,TimeTradeServer()+PeriodSeconds(_Period)*15,price);
      ObjectCreate(0,tf+" "+LineType+" label",OBJ_TEXT,0,rates_period[0].time,price);
      ObjectSetString(0,tf+" "+LineType+" label",OBJPROP_TEXT,"{"+tf+"}   "+LineType);
      ObjectSetString(0,tf+" "+LineType+" label",OBJPROP_FONT,"Arial");
      ObjectSetInteger(0,tf+" "+LineType+" label",OBJPROP_FONTSIZE,fontsize);
      ObjectSetInteger(0,tf+" "+LineType+" label",OBJPROP_COLOR,clr);
      ObjectSetInteger(0,tf+" "+LineType+" label",OBJPROP_TIMEFRAMES,visibility);
   }
   else
      //ObjectMove(0,tf+" "+LineType+" label",0,TimeTradeServer()+PeriodSeconds(_Period)*15,price);
      ObjectMove(0,tf+" "+LineType+" label",0,rates_period[0].time,price);

   if(ObjectFind(0,tf+" "+LineType+" line")<0)
   {
      ObjectCreate(0,tf+" "+LineType+" line",OBJ_HLINE,0,rates_period[0].time,price);
      ObjectSetInteger(0,tf+" "+LineType+" line",OBJPROP_WIDTH,width);
      ObjectSetInteger(0,tf+" "+LineType+" line",OBJPROP_STYLE,style);
      ObjectSetInteger(0,tf+" "+LineType+" line",OBJPROP_COLOR,clr);
      ObjectSetInteger(0,tf+" "+LineType+" line",OBJPROP_TIMEFRAMES,visibility);
   }
   else
      ObjectMove(0,tf+" "+LineType+" line",0,rates_period[0].time,price);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(_UninitReason==REASON_REMOVE)
   {
      for(int p=0; p<ArraySize(PArray); p++)
         for(int lt=0; lt<ArraySize(line_types); lt++)
            ObjectsDeleteAll(0,StringSubstr(EnumToString(PArray[p]),7)+" "+line_types[lt]+" l",0,-1);
      GlobalVariablesDeleteAll(_Symbol+": PSR_",0);
   }
  }

Comments