Timeframe (60 minutes) Moving average inside Timeframe (5 minutes)

 

Hello,

I'm trying to add a SMA with 20 periods  from 1 hour chart to a 5 minutes chart.  

Tried to use the function IndicatorAdd but it didn't work...

Any advices?

#include <Charts\Chart.mqh>
   CChart grafic;
 
int average_handle;

//--- input parametrs
input ENUM_TIMEFRAMES   InpTimeFrame=PERIOD_H1; // Timeframe 
input ENUM_MA_METHOD    InpMethod=MODE_SMA;     // Method
input int               InpPeriod=20;           // Period
input int               InpShift=0;             // Shift

void OnInit()
{
   average_handle = iMA(Symbol(),InpTimeFrame,InpPeriod,InpShift,InpMethod,PRICE_CLOSE);
   if (average_handle<0) Print("Error ",GetLastError(),"in creation of indicator");

   grafic.IndicatorAdd(0,average_handle);
}  

void OnDeinit(const int reason) 
{
   IndicatorRelease(average_handle);
}
 
santosphm:

Hello,

I'm trying to add a SMA with 20 periods  from 1 hour chart to a 5 minutes chart.  

Tried to use the function IndicatorAdd but it didn't work...

Any advices?

I do that quite a lot - see if this is useful (change TF to any timeframe larger than the chart's timeframe):

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot EMA
#property indicator_label1  "EMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkGray
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- input parameters
input ENUM_TIMEFRAMES       TF = PERIOD_M2;
//--- indicator buffers
double         EMABuffer[];

int EMAHandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,EMABuffer,INDICATOR_DATA);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   EMAHandle = iMA(NULL,TF,10,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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if (TF<Period())
      return (0);
      
   int TFMaxBars = iBars(NULL,TF);
   int EMACalculated = BarsCalculated(EMAHandle);
   if (EMACalculated<TFMaxBars)
      return (prev_calculated);
//---
   ArraySetAsSeries(time,true);
   ArraySetAsSeries(EMABuffer,true);

   static datetime LT = 0;

   if (LT==0 || LT<time[0])
   {
      int Limit = MathMin(TerminalInfoInteger(TERMINAL_MAXBARS),(rates_total-prev_calculated));
      int TFBar = 0;
      double TempEMABuffer[1];

      CopyBuffer(EMAHandle,0,TFBar,1,TempEMABuffer);

      for (int i=0; i<Limit; i++)
      {
         while (time[i]<iTime(NULL,TF,TFBar))
         {
            TFBar++;
            CopyBuffer(EMAHandle,0,TFBar,1,TempEMABuffer);
         }
         EMABuffer[i] = TempEMABuffer[0];
      }
      
      LT = time[0];
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Seng Joo Thio:

I do that quite a lot - see if this is useful (change TF to any timeframe larger than the chart's timeframe):

Thanks man,

that is exactly what i needed. 

 

Easily use Period=240 (220 is better) for EMA

This Shows 1H EMA in 5Min Chart.