MQL4 instantly get MA200 figure

 

Hi,

im using MQL4 and im using iMA function as below :

double MAscds = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0); 

After executed the EA, the MA200 only able to appear after 200 Candlesticks. MY Question is how can i have the MA200 at the 1st candlestick after i run my EA ?


Thanks

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
Paulf: how can i have the MA200 at the 1st candlestick after i run my EA ?
How do you calculate a 200 period SMA when you have less than 200 candles available?
 
William Roeder #:
How do you calculate a 200 period SMA when you have less than 200 candles?


because when i start the backtest i saw there are history candlestick available and there are candlestick value available when mouseover the candlestick.
Files:
aaaaa.png  17 kb
 
if my EA are running in M5 chart and after 200 candlesticks then will need 16.67hours. 
 

As a work around you can use a self calculated  EMA with c = 2/(201) = 0,01:

double emaCalc(const double nxt, const double prv, const double c) { 
   return( (nxt-prv)*c + prv );
}

Start with the previous close as the prev. value of this ema. This way you have values from the beginning but of course it's not the sma(200) but it slowly approaches the sma.

 
Carl Schreiber #:

As a work around you can use a self calculated  EMA with c = 2/(201) = 0,01:

Start with the previous close as the prev. value of this ema. This way you have values from the beginning but of course it's not the sma(200) but it slowly approaches the sma.

Thanks ~
 

Just curious , Can use a "for loop" to read Close[1] until Close[200] and divide 200 ? then continue insert the new candlestick after every new close price into it ?

i didnt know can work this way or not, looks very complicated to code it.

 
  1. Carl Schreiber #: As a work around you can use a self calculated  EMA with c = 2/(201) = 0,01:

    That will not work. Exponential requires 3.45×(Length+1) bars to converge to 1/10 percent. (EMA Period 5 requires 21 initial bars.)
              Moving average - Wikipedia

    OP's original post was about the oldest 200 bars.


  2. Paulf #: because when i start the backtest i saw there are history candlestick available and there are candlestick value available when mouseover the candlestick.

    “Because” is not an answer to my question: “how do you calculate a 200 period SMA when you have less than 200 candle?” The answer is you can't. Doesn't matter if you use SMA or EMA, it can not be done. You “saw there are history candlesticks available.” Yes, you did, but you did not see 200. That is why the MA did not display.


  3. What I do, when dealing with large MAs, is compute MA(n) when n is the number of candles available up to length. This also solves № 1 problem. But this requires coding. Alternatively, just ignore all ticks until you have 200 bars.