Download MetaTrader 5
To add comments, please log in or register
Launch your trading signal. Make profit from paid subscription!
berix
88
berix 2014.07.22 20: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()
{
    IndicatorBuffers(1);
    SetIndexBuffer(0, lineBuffer, INDICATOR_DATA);

    return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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];

    return(rates_total);
}


What is going wrong?

berix
88
berix 2014.07.22 21: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];

    return(rates_total);
}
Ian Venner
2583
Ian Venner 2014.07.23 05:53  

you could do

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

for (int i=from; i>=0; i--)
berix
88
berix 2014.07.23 08: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

/
To add comments, please log in or register