Indicator with '#property strict' directive showing line display issue

Back to topics list To post a new topic, please log in or register
berix 2014.07.22 22:32

The below code with

#property strict

directive compiles without error, but doesn't display anything.

If you comment '#property strict' directive

//#property strict

it will also compile without error and the indicator will display a line.

Not working code:

#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2

double lineBuffer[];

//| Custom indicator initialization function                         |
int OnInit()
    SetIndexBuffer(0, lineBuffer, INDICATOR_DATA);

//| Custom indicator iteration function                              |
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[])
    static  int i;

    for(i=rates_total - prev_calculated; i>=0; i--)     lineBuffer[i] = open[i];


What is going wrong?

berix 2014.07.22 23:12 #

Found out the issue.

Expert Terminal messages were showing :

2014.07.22 22:16:19.430    array out of range in 'TESTindie.mq4' (37,73)

Corrective is:

    static  int i, from;

    if(prev_calculated == 0) from = rates_total - prev_calculated -1;
    else from = rates_total - prev_calculated;
    for(i=from; i>=0; i--)      lineBuffer[i] = open[i];


SDC 2014.07.23 07:53 #

you could do

int from = MathMin(rates_total-1, rates_total-prev_calculated);

for (int i=from; i>=0; i--)

berix 2014.07.23 10:10 #

I prefer:

    from = rates_total - prev_calculated;
    if(prev_calculated == 0) from --;

    for(i=from; i>=0; i--)

It uses less CPU : no extra call (stack building & unwinding), less comparisons

Back to topics list  

To add comments, please log in or register