iBands output

 

My first step is to just get the bollinger band values to print so that I can then add logic to use those values.  When I run the code below I am only getting '10.0' as the output for every tick

Also, I know that I should have a parameter that differentiates between the upper and lower bands.  When I searched for examples, they were using MODE_UPPER & MODE_LOWER in the iBands function.  Although when I tried using the same I got an unknown value error.


void OnTick() {
double bandsUpper,bandsLower;
bandsUpper=iBands(NULL,PERIOD_H1,20,0,2.0,PRICE_CLOSE);
bandsLower=iBands(NULL,PERIOD_H1,20,0,2.0,PRICE_CLOSE);
Print(bandsLower);
Print(bandsUpper);
}
 
Jeffrey Shumaker:

My first step is to just get the bollinger band values to print so that I can then add logic to use those values.  When I run the code below I am only getting '10.0' as the output for every tick

Also, I know that I should have a parameter that differentiates between the upper and lower bands.  When I searched for examples, they were using MODE_UPPER & MODE_LOWER in the iBands function.  Although when I tried using the same I got an unknown value error.


If im not wrong In MQL5, iBands() doesn't return a value directly it returns an indicator handle (that's why you're seeing a constant "10.0", it's just the handle number). You need to create the handle once in OnInit(), then use CopyBuffer() in OnTick() to actually read the values.

Also, MODE_UPPER/MODE_LOWER are MQL4 constants and don't exist in MQL5. Instead, use the buffer index in CopyBuffer(): 0 = base line, 1 = upper band, 2 = lower band.

Example:

int bandsHandle;

int OnInit() {
   bandsHandle = iBands(NULL, PERIOD_H1, 20, 0, 2.0, PRICE_CLOSE);
   return(INIT_SUCCEEDED);
}

void OnTick() {
   double upperBuffer[], lowerBuffer[];
   CopyBuffer(bandsHandle, 1, 0, 1, upperBuffer);
   CopyBuffer(bandsHandle, 2, 0, 1, lowerBuffer);
   Print("Upper: ", upperBuffer[0]);
   Print("Lower: ", lowerBuffer[0]);
}

void OnDeinit(const int reason) {
   IndicatorRelease(bandsHandle);
}

i hope that helps a bit
 
Thomas Eduard Van Der Jagt #:
If im not wrong In MQL5, iBands() doesn't return a value directly it returns an indicator handle (that's why you're seeing a constant "10.0", it's just the handle number). You need to create the handle once in OnInit(), then use CopyBuffer() in OnTick() to actually read the values.

Also, MODE_UPPER/MODE_LOWER are MQL4 constants and don't exist in MQL5. Instead, use the buffer index in CopyBuffer(): 0 = base line, 1 = upper band, 2 = lower band.

Example:


i hope that helps a bit

yeah I got confused trying to read the weak documentation they provided

thanks for the help

 
Jeffrey Shumaker #:
[Y]eah I got confused trying to read the weak documentation they provided[.]

Articles

Custom Indicators (Part 1): A Step-by-Step Introductory Guide to Developing Simple Custom Indicators in MQL5

Kelvin Muturi Muigua, 2024.05.02 13:26

Learn how to create custom indicators using MQL5. This introductory article will guide you through the fundamentals of building simple custom indicators and demonstrate a hands-on approach to coding different custom indicators for any MQL5 programmer new to this interesting topic.

 
Jeffrey Shumaker #:

yeah I got confused trying to read the weak documentation they provided

thanks for the help

I get that, but no problem at all good luck trading :)