# Newbie needs a bit of help with Indicator

14

Hi, this is my first post and my first MQL4 project - writing code for the %B indicator.

I've used a combination of copy/paste and reading both the online book, articles & a Metatrader book to write my first code.

I hope I've understood buffers and most things correctly. The compiler says there's nothing wrong.

EXCEPT it doesn't actually draw the indicator line.

I've attached the mq4 file.

_

If anyone could help that would be much appreciated. Thanks.

Files:
zbpv.3.mq4 7 kb
7089

(Without testing!)

I assume there is zero divide error in line 129

`UpperBuffer[i]-LowerBuffer[i]`

could be zero at the beginning of the chart...

14

OK. (Thanks for looking btw) .. Yes, my inclination was / is it's something around the area "how it starts"

I have to say that was the area I understood the least. Hmm ...

How would I get around that? Like - How could I make it "wait" until it wasn't?

14

' Cuz I thought this:

if(prev_calculated<1)
{
for(i=0; i<pBperiod; i++)
{
pBBuffer[i]=EMPTY_VALUE;
BasisBuffer[i]=EMPTY_VALUE;
UpperBuffer[i]=EMPTY_VALUE;
LowerBuffer[i]=EMPTY_VALUE;

}

... was the "making it wait" bit

7089

1) Look into the expert tab for the log entries!!

2) check not to risk zero divide like: if ( ...== 0.0) continue; // skip

14

Ahh, OK. Great - thank you! I didn't know that was there ... Yes, the messages I get are, e.g.: %B v.3 EURUSD, M5: zero divide in '%B v.3.mq4' (129,60)

The formula needs that, as its: %b = (price-lower)/(upper-lower)

where price = last price, lower = lower bollinger band value, upper = upper bollinger band value.

Hmm. OK. Hehe.

It can be done somehow because I see at least one other person has the %b indicator as a free download.

Do you mean by "not to risk zero divide like: if ( ...== 0.0) continue;" ... that is how to get around it? By inserting a 'continue' operator ...

14

So I inserted this:

//--- %b
double UminusL;
UminusL = (UpperBuffer[i]-LowerBuffer[i]);
if (UminusL == 0.00) continue;

pBBuffer[i]=(close[i]-LowerBuffer[i])/UminusL;

And it now produces a line on the indicator. it's still obviously not quite right, but thank you very much for your help.