Moving average from an upper time frame

 

Hi,

I tried to draw two moving averages, one from the actual TF and one from the upper TF, but the values in the charts are completly false.

In the M1 Chart, must I have for the upper TF (blue line) the same values as from the M5 chart (red line), and this isnt so.

Have you an explanation, or I have a bug?

//+------------------------------------------------------------------+
//|                                             FX-ACD MTFMvAvgs.mq4 |
//|                                                         SwingMan |
//+------------------------------------------------------------------+
#property indicator_chart_window
 
#property indicator_buffers 2
//---- colors
#property indicator_color1 Red
#property indicator_color2 Blue
 
//---- extern parameters ---------------------------------------------
extern int Period_MovingAverage = 5;
//--------------------------------------------------------------------
 
//---- variables
int thisPeriod, upperTF;
//---- Buffers
double MavgThisWindow[], MavgUpperWindow[];
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   thisPeriod = Period();
   
   if (thisPeriod == PERIOD_M1)  upperTF = PERIOD_M5; else
   if (thisPeriod == PERIOD_M5)  upperTF = PERIOD_M30; else
   if (thisPeriod == PERIOD_M15) upperTF = PERIOD_H1; else
   if (thisPeriod == PERIOD_H1)  upperTF = PERIOD_H4; else
   if (thisPeriod == PERIOD_H4)  upperTF = PERIOD_D1; else
   if (thisPeriod == PERIOD_D1)  upperTF = PERIOD_W1; else
   if (thisPeriod == PERIOD_W1)  upperTF = PERIOD_MN1; 
   //Print("Period=",thisPeriod,"  TimeFrame=",upperTF);
//---- indicators
   string sName = "FX-ACD MTFMvAvgs";
   IndicatorShortName(sName);
   Comment(sName);
//----
   SetIndexBuffer(0,MavgThisWindow);
   SetIndexBuffer(1,MavgUpperWindow);
//----   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
//----      
   SetIndexLabel(0,"maThisWindow");
   SetIndexLabel(1,"maUpperWindow");
   return(0);
}
 
int deinit()
{ return(0); }
 
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars = IndicatorCounted();
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   
//---- Calculation ---------------------------------------------------
   for(int i = 0; i <limit; i++) 
   {
      MavgThisWindow[i]  = iMA(NULL, thisPeriod, Period_MovingAverage, 0, MODE_SMA, PRICE_CLOSE, i);           
   }   
   
   for(i = 0; i <limit; i++) 
   {      
      MavgUpperWindow[i] = iMA(NULL, upperTF, Period_MovingAverage, 0, MODE_SMA, PRICE_CLOSE, i);
   }    
//----
   return(0);
}
//+------------------------------------------------------------------+
 

Here is the chart:

 

That is right!

for(i = 0; i <limit; i++)
{
MavgUpperWindow[i] = iMA(NULL, upperTF, Period_MovingAverage, 0, MODE_SMA, PRICE_CLOSE, i);
}
In fact, the time at i in MavgUpperWindow[i] is not the time at i in iMA(NULL, upperTF, Period_MovingAverage, 0, MODE_SMA, PRICE_CLOSE, i) !!!!

The line is draw on this time axis !

ref below:

int i,ind ;
int limit=Bars;
int mm = upperTF /Period()+1;
double dd;
   for(i=limit*Period()/upperTF ; i>=0; i--)
   {   
 ind = iBarShift(NULL, Period(), iTime(NULL,upperTF ,i)) ;
 dd = iMA(NULL, upperTF, Period_MovingAverage, 0, MODE_SMA, PRICE_CLOSE, i);
 
 for (int j=0; j<mm; j++)
    SATLBuffer2[ind+j] = dd;
}
 

@DXdCn

Thank you!

I thinked that this where the solution. Very nice.

 
SwingMan:

@DXdCn

Thank you!

I thinked that this where the solution. Very nice.


Can you please send me this code?
 
can you send the complete code ? I can not understand how to do that sir.
 

Hello, I have build a Moving Average Solution that is also works in the Strategietester Modus, this Indicator above you dont can use it in the strategietestermodus!

And with my indicator you can also show the Movingaverag in different ways and set from different Timeframes like 7 or 32 :-).