How exactly iMAonArray is implemented ???

 
I try to calculate Moving Average on different inidicators (built-in as well as custom). Whenever i try to use Metatrader's suggested method (listed at "double moving average of RSI" for example), I alwys get different results then those on my chart. Let's suppose my chart windows contains ATR(13) and then I drag-and-drop MA on this chart setting my parameters (number of periods, price, method etc). Now, if I want to get the MAvalue in my Expert Advisor, I get completely different values then expected. Why is this happening ?

Detailed step-by-step:
1. Open new chart eurusd 1h
2. Add Average True Range indicator with default parameters (14)
3. Drag and drop Moving Average indicator on ATR windows with parameters: Period=21, MA Method: linear weighted, Apply to: Previous indicator data
4. Compare values:
- in Data window I get for 2005-08-02 17:00: ATR = 0.0022, MA = 0.0020
- when I use the code suggested tfor calculating MA with iMAonArray function (post #177), I get
2005.08.02 18:19:50 Test EURUSD,H1: tmp[1] = 0.00153067 // WHICH IS INCORRECT
2005.08.02 18:19:50 Test EURUSD,H1: ATR_Buffer[1] = 0.00218571 // WHICH IS RIGHT
2005.08.02 18:19:50 Test EURUSD,H1: iTime(NULL, 0, 2) = 2005.08.02 17:00

The test code is below.Anyone has a good knowledge as to how iMAonArray is implemented ?

Rgrds

Simon

int i,limit;
double ATR_Buffer[];
ArrayResize(ATR_Buffer, 500); // for testing only
double tmp[];
ArrayResize(tmp, 500);  // for testing only
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(i=0; i<limit; i++)
      ATR_Buffer[i]=iATR(NULL,0,14,i);
   for(i=0; i<limit; i++)
      tmp[i]=iMAOnArray(ATR_Buffer,Bars,21,0,MODE_LWMA,i);
   Print("iTime(NULL, 0, 2) = " + TimeToStr(iTime(NULL, 0, 1)));
   Print("ATR_Buffer[1] = " + ATR_Buffer[1]);
   Print("tmp[1] = " + tmp[1]);







            
 
Hmm, that's kinda interesting. From the values you posted, it looks like the ATR indicator rounds it's values when it's on the chart. This code should shift the decimal over, Round the ATR, then shift the decimal back.

double mult=MathPow(10,MarketInfo(Symbol(),MODE_DIGITS))
ATR_Buffer[i]=MathRound(iATR(NULL,0,14,i)*mult)/mult;



Regards,
Loren

 
Well Loren, rounding is not the problem here ...
The real problem is the value of MA of ATR indicator on chart and the same MA calculated in EA are completely different.
2005.08.02 18:19:50 Test EURUSD,H1: tmp[1] = 0.00153067 // WHICH IS INCORRECT
2005.08.02 18:19:50 Test EURUSD,H1: ATR_Buffer[1] = 0.00218571 // WHICH IS RIGHT

Simon

Hmm, that's kinda interesting. From the values you posted, it looks like the ATR indicator rounds it's values when it's on the chart. This code should shift the decimal over, Round the ATR, then shift the decimal back.

double mult=MathPow(10,MarketInfo(Symbol(),MODE_DIGITS))
ATR_Buffer[i]=MathRound(iATR(NULL,0,14,i)*mult)/mult;



Regards,
Loren

 
Simi,
I hear you, but one of us is misunderstanding the other. Did you try my code? If so, disregard.

Otherwise, let me explain again. If ATR on the chart is rounded, and the MA of the ATR uses the rounded values, then of course the MA on the chart looks different than the MA from your EA.


4. Compare values:
- in Data window I get for 2005-08-02 17:00: ATR = 0.0022, MA = 0.0020
- when I use the code suggested tfor calculating MA with iMAonArray function (post #177), I get
2005.08.02 18:19:50 Test EURUSD,H1: tmp[1] = 0.00153067 // WHICH IS INCORRECT
2005.08.02 18:19:50 Test EURUSD,H1: ATR_Buffer[1] = 0.00218571 // WHICH IS RIGHT
2005.08.02 18:19:50 Test EURUSD,H1: iTime(NULL, 0, 2) = 2005.08.02 17:00


I say this because your ATR value from the chart is 0.0022, and the ATR value from the EA is 0.00218571. Which of those values gets passed to the MA function when you drop it on the ATR indicator? In your expert, I'd be pretty sure it's definitely the unrounded value. But on the chart??? Who knows? It could be the rounded value, which could account for the differences you see. So my idea was to try rounding the values within your expert and see if they agree with the values on the chart.

Regards,
Loren
Reason: