How to make an indicator independent of the timeframe?

 

I want to make an indicator with a simple moving average.
I want to see the average of the last 30 days (always, regardless of the timeframe that I am looking at at that moment).
I was trying to do something like what I put below. But i don't get a constant value when i change timeframe.

Does anyone know how to do this correctly?
Thank you so much!!


//+------------------------------------------------------------------+
//| 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(rates_total<356*24)
     {         
         return(0);
     }


   int start;
   if(prev_calculated==0)start=0;
   else start=prev_calculated-1;
        


      if(prev_calculated>rates_total || prev_calculated<0)
      {
         to_copy=rates_total;
      }
      else
      {
         to_copy=rates_total-prev_calculated;
         
         if(prev_calculated>0) to_copy++;
      }
      
      



   if(Period()==PERIOD_H1)
   {
      period =24*30;
   }
   if(Period()==PERIOD_H4)
   {
      period =24*30/4;
   }
   if(Period()==PERIOD_D1)
   {
      period =30;
   }

   Handler  = iMA(NULL,PERIOD_CURRENT, period,0,MODE_SMA,PRICE_CLOSE);


   string message = "Error on CopyBuffer";        
   if(CopyBuffer(Handler,0,0,to_copy,Buffer)<=0)
   {
      Print(message,GetLastError());
      return 0;
   } 


   for(int i=start; i<rates_total && !IsStopped(); i++)
   {      
      indiBuffer[i] = Buffer[i];     
      
   }
}


 

Mistake number one: you create an indicator handle at every tick! This is a gross mistake. The indicator handle must be created once (this is desired in OnInit).

Indicator example: MA Other TimeFrame Correct

MA Other TimeFrame Correct
MA Other TimeFrame Correct
  • www.mql5.com
Отрисовка индикатора iMA (Moving Average, MA) с чужого таймфрейма
 
karp wak:

I want to make an indicator with a simple moving average.
I want to see the average of the last 30 days (always, regardless of the timeframe that I am looking at at that moment).
I was trying to do something like what I put below. But i don't get a constant value when i change timeframe.
Does anyone know how to do this correctly?

It is obvious that it will never be quite the same! They will always be slightly different.

You are using the principal of equivalence of the moving average period. but obviously at the lower time-frames you have more data and so the moving average will be more "refined" while at the higher time-frame there is less data and the moving average will be more "rough".

That is to be expected! They are equivalent but not the same!

 
Vladimir Karputov:

Mistake number one: you create an indicator handle at every tick! This is a gross mistake. The indicator handle must be created once (this is desired in OnInit).

Indicator example: MA Other TimeFrame Correct

Ok, I have changed it and I have put it in OnInit () .... The OnInit () event is executed every time the timeframe is changed? or does it only run once in the entire runtime?

Thank you very much for your answers !! Very Much appreciated !!
 
Fernando Carreiro:

It is obvious that it will never be quite the same! They will always be slightly different.

You are using the principal of equivalence of the moving average period. but obviously at the lower time-frames you have more data and so the moving average will be more "refined" while at the higher time-frame there is less data and the moving average will be more "rough".

That is to be expected! They are equivalent but not the same!

I think I have too big an error between the different values ​​of the averages in each timeframe ... it is almost + -0.1000 in XAGUSD ...

Is there something wrong in my code? I mean ... can this result be improved in any way?


Thank you very much for your answers !! Very Much appreciated !!

 
Vladimir Karputov:

Mistake number one: you create an indicator handle at every tick! This is a gross mistake. The indicator handle must be created once (this is desired in OnInit).

Indicator example: MA Other TimeFrame Correct

I forgot it !! thanks for the link!! it's great!! :-)
 
karp wak: I want to see the average of the last 30 days (always, regardless of the timeframe that I am looking at at that moment).

Read the SMA of 30 off of the D1. Use iBarShift to get the D1 shift.

Remember to repaint all bars with a D1 shift of zero.
          How to do your lookbacks correctly #9 - #14 & #19

 
William Roeder:

Read the SMA of 30 off of the D1. Use iBarShift to get the D1 shift.

Remember to repaint all bars with a D1 shift of zero.
          How to do your lookbacks correctly #9 - #14 & #19

I will. Excellent explanation in those posts!! Thank you so much for your help Mr. William!!
Reason: