functiondata-analysismql5

Can't Retrieve Upper & Lower Bands from iBand


I'm trying to get the upper and lower values from iBand...

void OnTick()
{
    double upperBand[];
    double lowerBand[];
    double range;

    int handle = iBands(_Symbol, _Period, 20, 2, 0, PRICE_CLOSE);
    // CopyBuffer(handle, which band, period, no. values, where to store)
    CopyBuffer(handle, 1, 0, 1, upperBand);
    CopyBuffer(handle, 2, 0, 1, lowerBand);
    
    
    // Use the upper_band and lower_band variables
    Print("Upper Band: ", upperBand[0]);
    Print("Lower Band: ", lowerBand[0]);
    range = upperBand[0] - lowerBand[0];
    Print("Range: ", range);
}

However the print functions reliably return an identical value, one that appears in fact to be the middle band value.

Of course, x = x, and so the range is always 0.

As I'm getting no error code and the data set is intact, I'm not even sure how to trouble shoot?


Solution

  • You are initializing the indicator incorrectly. Try the following

    int   handle_iBands;
    
    int OnInit()
    {
       handle_iBands = iBands(_Symbol, PERIOD_CURRENT, 20, 2, 0.0, PRICE_CLOSE);
    return(INIT_SUCCEEDED);
    }
    
    void OnTick()
    {
       double Upper[], Lower[], range;
       CopyBuffer(handle_iBands, 1, 0, 10, Upper);
       CopyBuffer(handle_iBands, 2, 0, 10, Lower);
    
       Print("Upper Band: ", Upper[0]);
       Print("Lower Band: ", Lower[0]);
       range = Upper[0] - Lower[0];
       Print("Range: ", range);
    }