Bollinger Bands: upper and lower band unite with middle band

Posted By: michas

Bollinger Bands: upper and lower band unite with middle band - 05/15/14 12:38

I am trying to plot a price curve with Bollinger Bands. Using the sample code from Zorro help seems to work. However, there are periods when all three bands unite. For instance using price data from 12/2/2013 between 5am and 7am (but one can find this in any others day data). How come?

Best wishes

Michas

I am using the following code:

function run()
{
set(PLOTNOW);

StartDate = 20121203;
EndDate = 20121203;

BarPeriod = 1;

BBands(series(price()),30,2,2,MAType_SMA);
plot("Bollinger1",rRealUpperBand,BAND1,0x000000CC);
plot("Bollinger2",rRealLowerBand,BAND2,0x800000FF);
plot("Price", price(),LINE, RED);

}

Attached picture BBands.png
Posted By: jcl

Re: Bollinger Bands: upper and lower band unite with middle band - 05/16/14 06:23

The Bollinger Bands function is not from Zorro, but from the TA-Lib, so I can't say why it unites. Maybe this happens due to gaps in the data. 1-minute data are very likely to have gaps. We'll look into that, maybe we can automatically fill the gaps for working around such cases.
Posted By: jcl

Re: Bollinger Bands: upper and lower band unite with middle band - 05/16/14 10:35

Ok, I was informed that the reason was _not_ gaps in the data, but a lower limit to the standard deviation function in the TA-Lib. If the StdDev is below 0.0001, it's assumed to be zero. Therefore you'll get bands with zero width.

As a simple workaround, just multiply the data with 10 for preventing that the StdDev gets below 0.0001:

BBands(series(10*price()),30,2,2,MAType_SMA);
plot("Bollinger1",rRealUpperBand/10,BAND1,0x000000CC);
plot("Bollinger2",rRealLowerBand/10,BAND2,0x800000FF);
Posted By: johnnyp

Re: Bollinger Bands: upper and lower band unite with middle band - 09/16/17 14:51

This isn't mentionned in the manual, and unlike the SAR, a corrected lite-c version hasn't been made.

Why?

I can understand not wanting to replace the current function because it might throw off some existing strategies, but at the very least this should be mentioned in the Remarks section of the manual page on indicators.
Posted By: qweqwe

Re: Bollinger Bands: upper and lower band unite with middle band - 09/19/17 17:04

But, How we can use it with optimize function?
Posted By: johnnyp

Re: Bollinger Bands: upper and lower band unite with middle band - 09/19/17 19:25

Just multiply the prices by 10 before calculating the bands, then divide by 10 before storing/using the results.

Code:
function run()
{
StartDate = 20170501;
EndDate = 20170502;
NumWFOCycles = 10;
DataSplit = 60; // activate WFO
NumCores = 4;
BarPeriod = 5;
set(BINARY);
Slippage = 0;
Spread = Commission = 0;
LifeTime = 1;

WinPayout = 75;
LossPayout = 0;

set(PARAMETERS);
int signal = optimize(20,5,30,1);
vars Close = series(priceClose());
var NbDevUp = optimize(2,1,4,0.5);
var NbDevDn = optimize(2,1,4,0.5);
BBands((series(10*priceClose())),signal,NbDevUp,NbDevDn,MAType_SMA);
vars upperBand = series(rRealUpperBand/10);
vars lowerBand = series(rRealLowerBand/10);

plot("Bollinger1",rRealUpperBand,BAND1,BLACK);
plot("Bollinger2",rRealLowerBand,BAND2,GREY);
set(PLOTNOW);

if (crossOver(Close, upperBand))
enterShort(); //close short trades and open a long position
if (crossUnder(Close, lowerBand))
enterLong();

}

Posted By: MatPed

Re: Bollinger Bands: upper and lower band unite with middle band - 09/20/17 08:55

If you trade a low value asset as Corn/USD I recommend to use x100, more safe...
© 2024 lite-C Forums