Retrieve Upper and Lower Bollinger Band values 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? only one band is generated on the chart whenI test it.


NEVER create an indicator handle in OnTick() you will crash as with each tick a new handle is created and these are limited!

See here:

Quickstart for newbies:

If the program compiles with no error but doesn't do what is should use the debugger:

Code debugging:
Error Handling and Logging in MQL5:
Tracing, Debugging and Structural Analysis of Source Code, scroll down to: "Launching and Debuggin":

Bear in mind there's virtually nothing that hasn't already been programmed for MT4/MT5.

So searching might be faster than trying:

Quick Start: Short Guide for Beginners
Quick Start: Short Guide for Beginners
Hello dear reader! In this article, I will try to explain and show you how you can easily and quickly get the hang of the principles of creating Expert Advisors, working with indicators, etc. It is beginner-oriented and will not feature any difficult or abstruse examples.

Thank you for the advice I will move my handles and try snooping at other people's code.

I'm currently having this exact issue. Kindly assist with how you were able to resolve it. I've been on it for a really long time now
this code for beginner like me are more readable..nice william
joe ola #:
this code for beginner like me are more readable..nice william
Hello. You're welcome. Consider putting a star for SEO. Nice day