Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains open time of each barSeries array that contains open prices of each bar
0 Views
0 Downloads
0 Favorites
DayTime
ÿþ//+------------------------------------------------------------------+

//|                                                      DayTime.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   3

//--- plot UP

#property indicator_label1  "UP"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot DN

#property indicator_label2  "Down"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Open

#property indicator_label3  "Open"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- input parameters

input ENUM_DAY_OF_WEEK  InpDay   =  MONDAY;  // Day of week

input uint              InpHour  =  12;      // Hour of day

//--- indicator buffers

double         BufferUP[];

double         BufferDN[];

double         BufferOP[];

//--- global variables

int            hour;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   hour=int(InpHour>23 ? 23 : InpHour);

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferUP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferDN,INDICATOR_DATA);

   SetIndexBuffer(2,BufferOP,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,233);

   PlotIndexSetInteger(1,PLOT_ARROW,234);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"DayTime("+EnumToString(InpDay)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferUP,true);

   ArraySetAsSeries(BufferDN,true);

   ArraySetAsSeries(BufferOP,true);

//---

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

  {

//--- @>25@:0 =0 <8=8<0;L=>5 :>;85AB2> 10@>2 4;O @0AGQB0

   if(rates_total<3) return 0;

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferUP,EMPTY_VALUE);

      ArrayInitialize(BufferDN,EMPTY_VALUE);

     }

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      int day_of_week=TimeDayOfWeek(time[i]);

      int hour_of_day=TimeHour(time[i]);

      int prev_hour_of_day=TimeHour(time[i+1]);

      if(day_of_week==WRONG_VALUE || hour_of_day==WRONG_VALUE || prev_hour_of_day==WRONG_VALUE) return 0;

      if(day_of_week==InpDay && hour_of_day==hour && prev_hour_of_day!=InpHour)

        {

         int index=BarShift(PERIOD_D1,time[i]);

         double PrevDayOpen=Open(PERIOD_D1,index+1);

         datetime TimePrevDayOpen=Time(PERIOD_D1,index+1);

         int IndexPrevDayOpen=BarShift(PERIOD_CURRENT,TimePrevDayOpen);

         for(int j=IndexPrevDayOpen; j>=close[i+1]; j--)

            BufferOP[j]=PrevDayOpen;

         if(close[i+1]<PrevDayOpen){

            BufferUP[i]=low[i];

            }

         else if(close[i+1]>PrevDayOpen){

            BufferDN[i]=high[i];

            }

        }

      else

        {

         BufferDN[i]=BufferUP[i]=BufferOP[i]=EMPTY_VALUE;

        }

     }



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

   return(rates_total);

  }

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

//| >72@0I05B 45=L =545;8 C:070==>9 40BK                            |

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

int TimeDayOfWeek(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.day_of_week;

  }

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

//| >72@0I05B G0A C:070==>3> 2@5<5=8                                |

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

int TimeHour(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.hour;

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

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

int BarShift(const ENUM_TIMEFRAMES timeframe,const datetime time)

  {

   int res=WRONG_VALUE;

   datetime last_bar=0;

   if(SeriesInfoInteger(Symbol(),timeframe,SERIES_LASTBAR_DATE,last_bar))

     {

      if(time>last_bar) res=0;

      else

        {

         const int shift=Bars(Symbol(),timeframe,time,last_bar);

         if(shift>0) res=shift-1;

        }

     }

   return(res);

  }

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

//| >72@0I05B Open C:070==>3> 10@0                                  |

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

double Open(const ENUM_TIMEFRAMES timeframe,const int index)

  {

   double array[];

   ArraySetAsSeries(array,true);

   if(CopyOpen(Symbol(),timeframe,index,1,array)==1) return array[0];

   return 0;

  }

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

//| >72@0I05B Time C:070==>3> 10@0                                  |

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

datetime Time(const ENUM_TIMEFRAMES timeframe,const int index)

  {

   datetime array[];

   ArraySetAsSeries(array,true);

   if(CopyTime(Symbol(),timeframe,index,1,array)==1) return array[0];

   return 0;

  }

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

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