Previous_Year_HLC

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

//|                                            Previous_Year_HLC.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 description "Previous Year High,Low,Close indicator"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   3

//--- plot High

#property indicator_label1  "Last Year High"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Low

#property indicator_label2  "Last Year Low"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Close

#property indicator_label3  "Last Year Close"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrDodgerBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- enums

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,       // Yes

   INPUT_NO    =  0        // No

  };

//--- input parameters

input uchar             InpYearNumber  =  1;          // Year number (0 - current, 1 - previous, etc)

input ENUM_INPUT_YES_NO InpShowPrice   =  INPUT_NO;   // Display labels with prices for price lines

input uint              InpWidthHigh   =  1;          // Label size for High line

input uint              InpWidthLow    =  1;          // Label size for Low line

input uint              InpWidthClose  =  1;          // Label size for Close line

//--- indicator buffers

double         BufferHigh[];

double         BufferLow[];

double         BufferClose[];

//--- global variables

string         prefix;

int            num_year;

color          color_high;

color          color_low;

color          color_close;

datetime       prev_year_time;

datetime       array_time[];

double         array_high[];

double         array_low[];

double         array_close[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- timer

   EventSetTimer(90);

//--- set global variables

   prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_";

   color_high=(color)PlotIndexGetInteger(0,PLOT_LINE_COLOR);

   color_low=(color)PlotIndexGetInteger(1,PLOT_LINE_COLOR);

   color_close=(color)PlotIndexGetInteger(2,PLOT_LINE_COLOR);

   num_year=InpYearNumber;

   prev_year_time=YearStartTime(num_year);

   if(!prev_year_time)

     {

      Print("Could not get the start time of the year ",(string)InpYearNumber," years ago.\nThe data for the previous year will be displayed.");

      num_year=1;

      prev_year_time=YearStartTime(1);

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferHigh,INDICATOR_DATA);

   SetIndexBuffer(1,BufferLow,INDICATOR_DATA);

   SetIndexBuffer(2,BufferClose,INDICATOR_DATA);

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

   PlotIndexSetInteger(0,PLOT_ARROW,158);

   PlotIndexSetInteger(1,PLOT_ARROW,158);

   PlotIndexSetInteger(2,PLOT_ARROW,158);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Previous Year HLC");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferHigh,true);

   ArraySetAsSeries(BufferLow,true);

   ArraySetAsSeries(BufferClose,true);

   ArraySetAsSeries(array_high,true);

//--- get timeframe

   Time(NULL,PERIOD_MN1,1);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//--- timer

   EventKillTimer();

   ObjectsDeleteAll(0,prefix,0);

   ChartRedraw();

//---

  }

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

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

  {

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

   ArraySetAsSeries(time,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<2) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-1;

      ArrayInitialize(BufferHigh,EMPTY_VALUE);

      ArrayInitialize(BufferLow,EMPTY_VALUE);

      ArrayInitialize(BufferClose,EMPTY_VALUE);

     }

//--- >43>B>2:0 40==KE

   BufferClose[0]=BufferHigh[0]=BufferLow[0]=EMPTY_VALUE;

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

   static datetime time_last_year=0;

   datetime time_curr_year=YearStartTime(TimeCurrent());

   if(time_last_year!=time_curr_year)

     {

      datetime time_prev_year=YearStartTime(num_year);

      datetime last_month_time=YearMonthStartTime(time_prev_year,12);

      int x=int(num_year>0 ? (last_month_time-time_prev_year)/PeriodSeconds(PERIOD_MN1)+1 : Month(TimeCurrent()));

      int copied_time=CopyTime(NULL,PERIOD_MN1,time_prev_year,1,array_time);

      int copied_close=CopyClose(NULL,PERIOD_MN1,last_month_time,1,array_close);

      int copied_high=CopyHigh(NULL,PERIOD_MN1,last_month_time,x,array_high);

      int copied_low=CopyLow(NULL,PERIOD_MN1,last_month_time,x,array_low);

      if(copied_time!=1 || copied_close!=1 || copied_high!=x || copied_low!=x)

         return 0;

      ArraySort(array_high);

      ArraySort(array_low);

      double max=array_high[0];

      double min=array_low[0];

      double cls=array_close[0];

      datetime tm=array_time[0];

      string year="Year: "+TimeToString(time_prev_year,TIME_DATE);

      if(InpShowPrice)

        {

         DrawArrowRightPrice(prefix+"YearHigh",max,time[0],color_high,InpWidthHigh,"",year+", High: "+DoubleToString(max,Digits()));

         DrawArrowRightPrice(prefix+"YearLow",min,time[0],color_low,InpWidthLow,"",year+", Low: "+DoubleToString(min,Digits()));

         DrawArrowRightPrice(prefix+"YearClose",cls,time[0],color_close,InpWidthClose,"",year+", Close: "+DoubleToString(cls,Digits()));

        }

      MqlDateTime stm;

      if(TimeToStruct(time_prev_year,stm))

        {

         string yt=(string)stm.year;

         PlotIndexSetString(0,PLOT_LABEL,"High "+yt+" year");

         PlotIndexSetString(1,PLOT_LABEL,"Low "+yt+" year");

         PlotIndexSetString(2,PLOT_LABEL,"Close "+yt+" year");

        }

      time_last_year=time_curr_year;

     }

   int total=BarShift(NULL,PERIOD_CURRENT,array_time[0]);

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

     {

      BufferHigh[i]=array_high[0];

      BufferLow[i]=array_low[0];

      BufferClose[i]=array_close[0]; 

     }

     

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

   return(rates_total);

  }

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

//| Custom indicator timer function                                  |

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

void OnTimer()

  {

   Time(NULL,PERIOD_MN1,1);

  }  

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

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

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

//| >72@0I05B Time                                                  |

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

datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)

  {

   datetime array[];

   return(CopyTime(symbol_name,timeframe,shift,1,array)==1 ? array[0] : 0);

  }

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

//| >72@0I05B 2@5<O =0G0;0 3>40 ?> 2@5<5=8                          |

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

datetime YearStartTime(const datetime time) {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm))

      return 0;

   tm.mon=1;

   tm.day=1;

   tm.hour=0;

   tm.min=0;

   tm.sec=0;

   return(StructToTime(tm));

}

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

//| >72@0I05B 2@5<O =0G0;0 3>40 ?> A<5I5=8N >B =0G0;0 B5:CI53> 3>40 |

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

datetime YearStartTime(int shift)

  {

   MqlDateTime tm;

   datetime time=YearStartTime(TimeCurrent());

   if(time==0)

      return 0;

   if(!TimeToStruct(time,tm))

      return 0;

   if(shift>tm.year-1970)

      shift=tm.year-1970;

   tm.year-=shift;

   tm.mon=1;

   tm.day=1;

   tm.hour=0;

   tm.min=0;

   tm.sec=0;

   return(StructToTime(tm));

  }

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

//| >72@0I05B 2@5<O =0G0;0 <5AOF0 7040==>3> 3>40                    |

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

datetime YearMonthStartTime(const datetime time_year,uchar month)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time_year,tm))

      return 0;

   tm.mon=int(month<1 ? 1 : month>12 ? 12 : month);

   tm.day=1;

   tm.hour=0;

   tm.min=0;

   tm.sec=0;

   return(StructToTime(tm));

  }

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

//| >72@0I05B <5AOF 2@5<5=8                                         |

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

int Month(datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm))

      return 0;

   return tm.mon;

  }

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

//| K2>48B B@5=4>2CN ;8=8N                                          |

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

void DrawLine(const string name,const double price1,const double price2,const datetime time1,const datetime time2,const color line_color,const int line_width,const ENUM_LINE_STYLE line_style,const string text="\n",const string tooltip="\n")

  {

   if(ObjectFind(0,name)<0)

     {

      ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

      ObjectSetInteger(0,name,OBJPROP_RAY_LEFT,false);

      ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,false);

     }

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time1);

   ObjectSetInteger(0,name,OBJPROP_TIME,1,time2);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price1);

   ObjectSetDouble(0,name,OBJPROP_PRICE,1,price2);

//---

   ObjectSetInteger(0,name,OBJPROP_WIDTH,line_width);

   ObjectSetInteger(0,name,OBJPROP_COLOR,line_color);

   ObjectSetInteger(0,name,OBJPROP_STYLE,line_style);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);

  }

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

//| K2>48B ?@02CN F5=>2CN <5B:C                                     |

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

void DrawArrowRightPrice(const string name,const double price,const datetime time,const color arrow_color,const int arrow_width,const string text="\n",const string tooltip="\n")

  {

   if(ObjectFind(0,name)<0)

     {

      ObjectCreate(0,name,OBJ_ARROW_RIGHT_PRICE,0,time,price);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

     }

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price);

//---

   ObjectSetInteger(0,name,OBJPROP_WIDTH,arrow_width);

   ObjectSetInteger(0,name,OBJPROP_COLOR,arrow_color);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);

  }

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

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