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

//|                                                    Daily Bar.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   "1.003"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   1

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

//--- input parameters

input int   InpShift = 5;  // Shift

//--- indicator buffers

double   DailyOpenBuffer[];

double   DailyHighBuffer[];

double   DailyLowBuffer[];

double   DailyCloseBuffer[];

double   DailyColors[];

//---

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

double   m_open=0.0,m_high=DBL_MIN,m_low=DBL_MAX,m_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);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

//--- line shifts when drawing

   PlotIndexSetInteger(0,PLOT_SHIFT,InpShift);

//--- set the display of the symbol

   PlotIndexSetString(0,PLOT_LABEL,"Daily Open;"+"Daily High;"+"Daily Low;"+"Daily 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;

        }

      m_open=0.0;

      m_high=DBL_MIN;

      m_low=DBL_MAX;

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

           }

         m_open=0.0;

         m_high=DBL_MIN;

         m_low=DBL_MAX;

         m_close=0.0;

        }

//---

   MqlDateTime STime_curr;

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

   m_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_open=open[i+1];

      if(STime_prev.day!=STime_curr.day)

         break;

      if(high[i]>m_high)

         m_high=high[i];

      if(low[i]<m_low)

         m_low=low[i];

     }

//---

   DailyOpenBuffer[rates_total-1]=m_open;

   DailyHighBuffer[rates_total-1]=m_high;

   DailyLowBuffer[rates_total-1]=m_low;

   DailyCloseBuffer[rates_total-1]=m_close;

   DailyColors[rates_total-1]=(m_open<m_close)?0.0:1.0;

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

   return(rates_total);

  }

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

Comments