Two Timeframes at Close prices

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Series array that contains close prices for each bar
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Two Timeframes at Close prices
ÿþ//+------------------------------------------------------------------+

//|                               Two Timeframes at Close prices.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.002"

#property indicator_chart_window

#property indicator_buffers 3

#property indicator_plots   3

//--- plot ClosePriceTF_0

#property indicator_label1  "ClosePriceTF_0"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDarkOliveGreen

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2

//--- plot ClosePriceTF_1

#property indicator_label2  "ClosePriceTF_1"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrYellowGreen

#property indicator_style2  STYLE_SOLID

#property indicator_width2  2

//--- plot MedianPrice

#property indicator_label3  "MedianPrice"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrLightSkyBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

//--- input parameters

input ENUM_TIMEFRAMES      Inp_TF_0    = PERIOD_H1;   // TimeFrame 0

input ENUM_TIMEFRAMES      Inp_TF_1    = PERIOD_D1;   // TimeFrame 1

//--- indicator buffers

double   ClosePriceTF0Buffer[];

double   ClosePriceTF1Buffer[];

double   MedianPriceBuffer[];

//---

bool     m_init_error=false;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(Inp_TF_0>=Inp_TF_1)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "\"TimeFrame 0\" ("+EnumToString(Inp_TF_0)+") 1>;LH5 \"TimeFrame 1\" ("+EnumToString(Inp_TF_1)+")!":

                      "\"TimeFrame 0\" ("+EnumToString(Inp_TF_0)+") more \"TimeFrame 1\" ("+EnumToString(Inp_TF_1)+")!";

      Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      m_init_error=true;

     }



   ENUM_TIMEFRAMES curr_tf=Period();

   if(curr_tf>=Inp_TF_0 || curr_tf>=Inp_TF_1)

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      ""5:CI89 B09<D@59< ("+EnumToString(curr_tf)+") 1>;LH5 \"TimeFrame 0\" ("+EnumToString(Inp_TF_0)+") 8;8 1>;LH5 \"TimeFrame 1\" ("+EnumToString(Inp_TF_1)+")!":

                      "The current timeframe ("+EnumToString(curr_tf)+") more than \"TimeFrame 0\"("+EnumToString(Inp_TF_0) + ") or more than \"TimeFrame 1\"("+EnumToString(Inp_TF_1)+")!";

      Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      m_init_error=true;

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,ClosePriceTF0Buffer,INDICATOR_DATA);

   SetIndexBuffer(1,ClosePriceTF1Buffer,INDICATOR_DATA);

   SetIndexBuffer(2,MedianPriceBuffer,INDICATOR_DATA);

//---

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

  {

   if(m_init_error)

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=1;



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

     {

      int index_tf_0=iBarShift(Symbol(),Inp_TF_0,time[i],false);

      if(index_tf_0<0)

         return(0);

      double price_tf_0=iClose(Symbol(),Inp_TF_0,index_tf_0+1);

      ClosePriceTF0Buffer[i]=price_tf_0;



      int index_tf_1=iBarShift(Symbol(),Inp_TF_1,time[i],false);

      if(index_tf_1<0)

         return(0);

      double price_tf_1=iClose(Symbol(),Inp_TF_1,index_tf_1+1);

      ClosePriceTF1Buffer[i]=price_tf_1;



      MedianPriceBuffer[i]=(price_tf_0+price_tf_1)/2.0;

     }

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

   return(rates_total);

  }

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

Comments