Testing indicators, does not redraw on each tick

To add comments, please log in or register
heras
40
heras  

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.

// test.mq4

#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_color1 clrAntiqueWhite
#property indicator_color2 clrAntiqueWhite

double upper[], lower[];
extern int maPeriod = 5;
extern double deviation = 0.5;

int OnInit(){
    SetIndexBuffer(0, upper);
    SetIndexBuffer(1, lower);
    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 limit = rates_total - prev_calculated;
    int i = 0;

    while(i < limit){
            double mean  = iMA(NULL, 0, maPeriod, 0, MODE_EMA, PRICE_CLOSE, i);
            double barAvg = (open[i] + close[i]) / 2;
            double barDelta = barAvg - mean;
            upper[i] = iEnvelopes(NULL, 0, maPeriod, MODE_EMA, 0, PRICE_CLOSE, deviation, MODE_UPPER, i) + barDelta;
            lower[i] = iEnvelopes(NULL, 0, maPeriod, MODE_EMA, 0, PRICE_CLOSE, deviation, MODE_LOWER, i) + barDelta;
            i++;
    }
    return rates_total;
}
tuvshin1221
5
tuvshin1221  
heras:

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.

heras
40
heras  

Oh, cool. Solution is here:

https://www.mql5.com/en/forum/146942

Now lets see if I can figure out why this work.

need some help to clarify when objects update
need some help to clarify when objects update
  • 2013.09.18
  • www.mql5.com
Hi forum, had it recently pointed out to me by one of our esteemed moderators (THANK YOU RAPTOR) That I writing my code such that my indicators wer...
heras
40
heras  

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);
}
Show progress of indicator
Show progress of indicator
  • 2014.02.16
  • www.mql5.com
Hi! I need a way to give the user the chance to cancel the indicator when necessary. E.g...
heras
40
heras  

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.

To add comments, please log in or register