Download MetaTrader 5

This line crashes the debug session (porting old code to updated MQL4)

To add comments, please log in or register

Hello, I'm in the process of porting some old code (start(), init(), etc) to the updated MQL4.  I've gotten rid of all the compiler warnings and replaced start() with OnCalculate() and so on.  Now without this one line, the chart window stays open (desired operation), but with the line, it crashes as if it thinks something in the line is legacy code.  Please point out how this line should be updated (See bottom of code).


//| 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[])
        int limit;
        int counted_bars=IndicatorCounted();

   Comment("DEBUG: " + IntegerToString(counted_bars));
   if(counted_bars < 0)

        if(counted_bars > 0)
                counted_bars -= 10;

        limit = Bars - counted_bars;

        int i;
        for(i=0; i<CURRENCYCOUNT; i++) {


        int lowLimit = 0;
        limit = WindowFirstVisibleBar();
        lowLimit = limit - WindowBarsPerChart();

        for(i=limit; i >= lowLimit; i--) {
                double diff = 0.0;

                static int priorBar = -1;
                int bar = iBarShift(NULL, userTimeFrame, time[i]);  /************ THE LINE THAT BREAKS IT. within it I've replaced old "Time[i]" with updated time[i] ***********/
//              if(bar!=priorBar) {
//                      // Calc Slope into currencyValues[]
//                      ArrayInitialize(currencyValues,0.0);
//                      calcCSS(userTimeFrame,bar);
//              }
// .........

  1.         int counted_bars=IndicatorCounted();
            if(counted_bars > 0)
                    counted_bars -= 10;
            limit = Bars - counted_bars;
    Do your lookback properly
    int counted = IndicatorCounted();
    int lookback = ... // iMA(period) has look back of period.
                       // buffer[i+2] has look back of 2
                       // use maximum of all.
    for(int iBar = Bars - MathMax(lookback, counted); iBar >= 0; --iBar) ...

  2. int bar = iBarShift(NULL, userTimeFrame, time[i]);  /************ THE LINE THAT BREAKS IT. within it I've replaced old "Time[i]" with updated time[i] ***********/
    There is the parameters high[] and the The predefined Variables - MQL4 Documentation High[]. Don't get them confused. high[] can be ordered either direction; High[0] is the current bar always. Your limit/lowLimit implies series arrays but you didn't set time[] as series.
  3. limit = WindowFirstVisibleBar();
    lowLimit = limit - WindowBarsPerChart();
    You don't take chart shift into account so lowLimit will go negative
    iLeft   = WindowFirstVisibleBar(); 
    iRight  = MathMax(0,iLeft-WindowBarsPerChart() ); 

Thanks WHRoeder,  that's a lot of points.  I will look at each one in detail now.
To add comments, please log in or register