Hi,
As far as I know the close price for the current, not yet completed bar is the last price. Therefore the close price is constantly changing with each tick. I can see this happen when simulating indicators of other people. But of course not my indicator. It looks like it immediately jumps to the historic closing price of the bar before the tick data has reached it. My code is below. Can anyone tell me why my code is bugged?
Thanks.
Oh, cool. Solution is here:
https://www.mql5.com/en/forum/146942
Now lets see if I can figure out why this work.

- 2013.09.18
- www.mql5.com
Going by what was said here: https://www.mql5.com/en/forum/149695#comment_3755084
I wrote the code below. The referenceEma is updated at every tick as expected. The modifiedEma is not and just shows referenceEma delayed by one bar.
What I expected/want modifiedEma to do is to update at every tick that is lower than the lowest price in the current bar. So it should track referenceEma but instead of using the last price for calculating the current EMA, use the lowest price of the current bar. This should result in a modifiedEma that is in sync with referenceEma and not delayed and is also slightly lower than referenceEma if the low of that bar was lower than close.
Any ideas why this isn't working and how to fix it?
Thanks in advance.
#property version "1.00" #property strict #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 clrRed #property indicator_color2 clrAntiqueWhite #define LOOKBACK 1 extern int maPeriod = 5; double referenceEma[], modifiedEma[]; int OnInit(){ SetIndexBuffer(0, referenceEma); SetIndexBuffer(1, modifiedEma); return(INIT_SUCCEEDED); } 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[]){ int countedBars = prev_calculated; if(countedBars < LOOKBACK) countedBars = LOOKBACK; for(int i = rates_total - 1 - countedBars; i>=0; i--){ referenceEma[i] = iMA(NULL, 0, maPeriod, 0, MODE_EMA, PRICE_CLOSE, i); // current EMA value = a * current value + (1 - a) * previous EMA value double a = 2 / (maPeriod + 1); modifiedEma[i] = a * low[i] + (1 - a) * iMA(NULL, 0, maPeriod, 0, MODE_EMA, PRICE_CLOSE, i+1); } return(rates_total - 1); }

- 2014.02.16
- www.mql5.com
Turns out
double a = 2 / (maPeriod + 1);
is 0.0. The following works
double a = 2.0 / (maPeriod + 1);
modifiedEma now wobbles with the ticks as desired.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
As far as I know the close price for the current, not yet completed bar is the last price. Therefore the close price is constantly changing with each tick. I can see this happen when simulating indicators of other people. But of course not my indicator. It looks like it immediately jumps to the historic closing price of the bar before the tick data has reached it. My code is below. Can anyone tell me why my code is bugged?
Thanks.