Daily Bar 2

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Daily Bar 2
ÿþ//+------------------------------------------------------------------+

//|                                                  Daily Bar 2.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43161 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43161"

#property version   "2.001"

#property description "Two bars: D1 and one to choose from"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   2

//--- plot Daily

#property indicator_label1  "Daily"

#property indicator_type1   DRAW_COLOR_CANDLES

#property indicator_color1  clrLavender,clrLimeGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Second timeframe

#property indicator_label2  "Second timeframe"

#property indicator_type2   DRAW_COLOR_CANDLES

#property indicator_color2  clrLavender,clrLimeGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width12 1

//--- input parameters

input int               InpShift    = 3;           // Shift

input ENUM_TIMEFRAMES   Inp_period  = PERIOD_H4;   // Second timeframe

//--- indicator buffers

double   DailyOpenBuffer[];

double   DailyHighBuffer[];

double   DailyLowBuffer[];

double   DailyCloseBuffer[];

double   DailyColors[];

//---

double   SecondOpenBuffer[];

double   SecondHighBuffer[];

double   SecondLowBuffer[];

double   SecondCloseBuffer[];

double   SecondColors[];

//---

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

double   m_daily_open   = 0.0,   m_daily_high   = DBL_MIN,  m_daily_low = DBL_MAX,  m_daily_close  = 0.0;

double   m_second_open  = 0.0,   m_second_high  = DBL_MIN,  m_second_low= DBL_MAX,  m_second_close = 0.0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Period()>PERIOD_D1)

      return(INIT_PARAMETERS_INCORRECT);

//--- indicator buffers mapping

   SetIndexBuffer(0,DailyOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,DailyHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,DailyLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,DailyCloseBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,DailyColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(5,SecondOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(6,SecondHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(7,SecondLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(8,SecondCloseBuffer,INDICATOR_DATA);

   SetIndexBuffer(9,SecondColors,INDICATOR_COLOR_INDEX);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//--- line shifts when drawing

   PlotIndexSetInteger(0,PLOT_SHIFT,InpShift);

   PlotIndexSetInteger(1,PLOT_SHIFT,InpShift-1);

//--- set the display of the symbol

   PlotIndexSetString(0,PLOT_LABEL,"D1 Open;"+"D1 High;"+"D1 Low;"+"D1 Close");

   string text=StringSubstr(EnumToString(Inp_period),7,-1);

   PlotIndexSetString(1,PLOT_LABEL,text+" Open;"+text+" High;"+text+" Low;"+text+" Close");

//---

   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=rates_total;

//--- first calculation or number of bars was changed

   if(prev_calculated==0)

     {

      //printf(" rates_total (%d) > prev_calculated (%d)",rates_total,prev_calculated);

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

        {

         DailyOpenBuffer[i]=0.0;

         DailyHighBuffer[i]=0.0;

         DailyLowBuffer[i]=0.0;

         DailyCloseBuffer[i]=0.0;

         DailyColors[i]=0.0;

         //---

         SecondOpenBuffer[i]=0.0;

         SecondHighBuffer[i]=0.0;

         SecondLowBuffer[i]=0.0;

         SecondCloseBuffer[i]=0.0;

         SecondColors[i]=0.0;

        }

      m_daily_open=0.0;

      m_daily_high=DBL_MIN;

      m_daily_low=DBL_MAX;

      m_daily_close=0.0;

      //---

      m_second_open=0.0;

      m_second_high=DBL_MIN;

      m_second_low=DBL_MAX;

      m_second_close=0.0;

     }

//--- new bar

   else

      if(rates_total>prev_calculated && prev_calculated!=0)

        {

         //printf(" rates_total (%d) > prev_calculated (%d)",rates_total,prev_calculated);

         for(int i=prev_calculated-1; i<rates_total; i++)

           {

            DailyOpenBuffer[i]=0.0;

            DailyHighBuffer[i]=0.0;

            DailyLowBuffer[i]=0.0;

            DailyCloseBuffer[i]=0.0;

            DailyColors[i]=0.0;

            //---

            SecondOpenBuffer[i]=0.0;

            SecondHighBuffer[i]=0.0;

            SecondLowBuffer[i]=0.0;

            SecondCloseBuffer[i]=0.0;

            SecondColors[i]=0.0;

           }

         m_daily_open=0.0;

         m_daily_high=DBL_MIN;

         m_daily_low=DBL_MAX;

         m_daily_close=0.0;

         //---

         m_second_open=0.0;

         m_second_high=DBL_MIN;

         m_second_low=DBL_MAX;

         m_second_close=0.0;

        }

//---

   MqlDateTime STime_curr;

   TimeToStruct(time[rates_total-1],STime_curr);

   m_daily_close=close[rates_total-1];

   m_second_close=close[rates_total-1];

   for(int i=rates_total-1; i>0; i--)

     {

      MqlDateTime STime_prev;

      TimeToStruct(time[i],STime_prev);

      if(i<rates_total-1)

         m_daily_open=open[i+1];

      if(STime_prev.day!=STime_curr.day)

         break;

      if(high[i]>m_daily_high)

         m_daily_high=high[i];

      if(low[i]<m_daily_low)

         m_daily_low=low[i];

      //---

      MqlRates rates_second[];

      int start_pos=0,count=1;

      if(CopyRates(Symbol(),Inp_period,start_pos,count,rates_second)!=count)

         return(0);

      m_second_open=rates_second[0].open;

      m_second_high=rates_second[0].high;

      m_second_low=rates_second[0].low;

     }

//---

   DailyOpenBuffer[rates_total-1]=m_daily_open;

   DailyHighBuffer[rates_total-1]=m_daily_high;

   DailyLowBuffer[rates_total-1]=m_daily_low;

   DailyCloseBuffer[rates_total-1]=m_daily_close;

   DailyColors[rates_total-1]=(m_daily_open<m_daily_close)?0.0:1.0;

//---

   SecondOpenBuffer[rates_total-1]=m_second_open;

   SecondHighBuffer[rates_total-1]=m_second_high;

   SecondLowBuffer[rates_total-1]=m_second_low;

   SecondCloseBuffer[rates_total-1]=m_second_close;

   SecondColors[rates_total-1]=(m_second_open<m_second_close)?0.0:1.0;

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

   return(rates_total);

  }

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

Comments