How to get iMA( buffer to plot when referencing iMA( on a higher TF?

 

Hi,

I was able to get the iMA( to successfully plot a moving average.

Suppose I wanted to plot a higher timeframe MA?

In the code below, there's an input of choosing a higher timeframe with the variable "TF".

For example, if I wanted to plot the M30 moving average when the indicator is on a M15 chart, what would I need to do differently in the code below?

I'm pretty sure it has to do with iBarShift(, since I've used it in MQL4?

Note: I'm asking more for the concept so I can apply to other built-in indicator functions, not specifically the MA for this indicator.

Thank you.

//+------------------------------------------------------------------+
//|                                             High_Low_Channel.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3
//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Lower
#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot MA
#property indicator_label3  "MA"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- input parameters
input ENUM_TIMEFRAMES TF=PERIOD_CURRENT;
input int      HighLowBars=8;
//--- indicator buffers
double         UpperBuffer[];
double         LowerBuffer[];
double         MABuffer[];

int ma_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,MABuffer,INDICATOR_DATA);
   ArraySetAsSeries(MABuffer,true);
   
   // Create a moving average on a different timeframe with variable "TF"
   ma_handle = iMA(_Symbol,TF,50,0,MODE_EMA,PRICE_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 &tickvolume[],
                const long &volume[],
                const int &spread[])
  {
//---
   ArraySetAsSeries(UpperBuffer,true);
   ArraySetAsSeries(LowerBuffer,true);
   ArraySetAsSeries(MABuffer,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   
   int bars = rates_total-1;
   if(prev_calculated>0) { bars = rates_total-(prev_calculated); }
   
   // If PERIOD_CURRENT is chosen for "TF", the MA draw fine; anything else doesn't.
   // 
   // How would I get the copied buffer below to plot the MA on a different timeframe?  What am I missing?
   // e.g. If "TF" is PERIOD_M30 and indicator is on a M15, what should be done differently to get the plot to work?
   CopyBuffer(ma_handle,0,0,(rates_total-prev_calculated)+1,MABuffer);
   
   for(int i=bars;i>=0;i--)
   {
     UpperBuffer[i] = high[ArrayMaximum(high,i,HighLowBars)];
     LowerBuffer[i] = low[ArrayMinimum(low,i,HighLowBars)];
   }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Matthew Renner: Suppose I wanted to plot a higher timeframe MA?
  1. Convert your shift to the HTF shift, get the value from there and plot it.

  2. Since the source data is a higher TF, you must reprocess all source bar zero values to get the stair step effect, (one source bar, multiple chart bars.)
              How to do your lookbacks correctly #9 - #14 & #19
Reason: