Higher Time-frame Moving Average on a lower Time-frame Chart

 

Hello,

I am trying to create an indicator that Plot for example the 4H EMA on a 1H TM chart, but it's not working here is the code :

PS : I tried to print out HTF_BUFFER[0] But the values are not correct.

Chart Screenshot:

 

Code :

#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

#property  indicator_buffers 1
#property  indicator_plots   1

#property  indicator_type1 DRAW_LINE
#property  indicator_label1 "MT_EMA"
#property  indicator_color1 clrGreen
#property  indicator_width1 4

input int PERIOD = 200;                               //Moving Average Period
input ENUM_MA_METHOD METHOD = MODE_EMA;               //Moving Average Method
input ENUM_APPLIED_PRICE PRICE = PRICE_CLOSE;         //Moving Average Applied Price                        
input ENUM_TIMEFRAMES TIMEFRAME = PERIOD_H4;          //Moving Average TimeFrame

double MABuffer[];

int OnInit()
  {
      SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
      
      PlotIndexGetInteger(0,PLOT_DRAW_BEGIN,PERIOD);
      
      return(INIT_SUCCEEDED);
  }

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[])
  {
  
      ArraySetAsSeries(time,true);
      ArraySetAsSeries(MABuffer,true);
      
      if(IsStopped()) return(0);        
      
      if(rates_total < PERIOD) return(0);  


      int Limit = rates_total-prev_calculated;
      if(prev_calculated>0) Limit++; 
      
      for(int i = Limit-1; i >= 0; i--){
         int mtfbar = iBarShift(Symbol(),TIMEFRAME,time[i],false);
         int MTFHandle    =  iMA(Symbol(), TIMEFRAME, PERIOD, mtfbar, METHOD, PRICE);
         double HTF_BUFFER[1];
         CopyBuffer(MTFHandle,0,0,1,HTF_BUFFER);
         MABuffer[i] = HTF_BUFFER[0];
      }
    

   return(rates_total);
  }
 
         int MTFHandle    =  iMA(Symbol(), TIMEFRAME, PERIOD, mtfbar, METHOD, PRICE);
         double HTF_BUFFER[1];
         CopyBuffer(MTFHandle,0,0,1,HTF_BUFFER);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

Thank you so much William for the help, it worked i had to put the handle on the init function then call it in OnCalculate 

int OnInit()
  {
      SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
      
      MAHandle = iMA(Symbol(), TIMEFRAME, PERIOD, 0, METHOD, PRICE);
      if(MAHandle == INVALID_HANDLE) return(INIT_FAILED);
      
      PlotIndexGetInteger(0,PLOT_DRAW_BEGIN,PERIOD);
      
      return(INIT_SUCCEEDED);
  }
      for(int i = Limit-1; i >= 0; i--){
         int mtfbar = iBarShift(Symbol(),TIMEFRAME,time[i],false);
         double HTF_BUFFER[1];
         CopyBuffer(MAHandle,0,mtfbar,1,HTF_BUFFER);
         MABuffer[i] = HTF_BUFFER[0];
      }