Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, Quad, 1 invisible), 721 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Calculation of bbands #468050
09/14/17 19:02
09/14/17 19:02
Joined: Aug 2017
Posts: 40
J
johnnyp Offline OP
Newbie
johnnyp  Offline OP
Newbie
J

Joined: Aug 2017
Posts: 40
I have a working strategy in Zorro and I need to code it in MQL4 in order to run it in a context that doesn't permit the use of DLLs. But I am having trouble implementing BBands properly.

Some plotting for testing purposes.
Code:
// compare Bollinger bands separation to StdDev, Moment, and Variance functions
int bbperiod = 10;
BBands(Price,bbperiod,2,2,MAType_SMA);
var bbdist = rRealUpperBand + rRealLowerBand;

var std = StdDev(Price, bbperiod);
plot("STD", 4.*std - bbdist, NEW, RED);
var mom = sqrt(Moment2(Price, bbperiod, 2));
plot("Moment", 4.*mom - bbdist, NEW, RED);
var vari = sqrt(Variance(Price, bbperiod));
plot("Var", 4.*vari - bbdist, NEW, RED);

printf("\nStd %f mom %f var %f", 4.*std-bbdist, 4.*mom-bbdist, 4.*vari-bbdist);


For periods >= 4, StdDev and Variance give the same results, for period = 3 there are three different results.

I may have found a bug in the implementation of Moment in indicators.c ... here is an extract
Code:
// rMean contains the mean over Period values
var variance=0.;
int i;
for(i=0; i<Period; i++) {
	var f = Data[i]-rMean;
	variance += f*f;
}
variance /= (Period-1);


This code takes the sum of Period values and divides that sum by (Period-1). Shouldn't the sum be divided by Period?

I tried a second test with a corrected version of the Moment function. For periods >= 4, StdDev and Variance give the same results, for period = 3 Variance gives the same result as Moment.

In any case, StdDev always gives the closest result. I have tried reading the ta-lib sources but I can't see the difference between Moment and StdDev. Any help would be appreciated.

Re: Calculation of bbands [Re: johnnyp] #468096
09/18/17 11:16
09/18/17 11:16
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Period-1 is the normally used formula. The difference that I know between Moment and StdDev is that the TA-LIB rounds StdDev down to zero when below 0.0001.

Re: Calculation of bbands [Re: jcl] #468102
09/18/17 14:52
09/18/17 14:52
Joined: Aug 2017
Posts: 40
J
johnnyp Offline OP
Newbie
johnnyp  Offline OP
Newbie
J

Joined: Aug 2017
Posts: 40
Some more research reveals that using Period-1 instead of Period is called Bessel's correction.

The justification is that among (x1 - x_mean, ..., xn - x_mean) there are only n-1 independant values because their sum is zero.

A quick inspection of the ta-lib sources suggests that ta-lib uses Period rather than Period-1.

Re: Calculation of bbands [Re: johnnyp] #468114
09/19/17 03:01
09/19/17 03:01
Joined: Dec 2016
Posts: 71
F
firecrest Offline
Junior Member
firecrest  Offline
Junior Member
F

Joined: Dec 2016
Posts: 71
Originally Posted By: johnnyp
Some more research reveals that using Period-1 instead of Period is called Bessel's correction.

The justification is that among (x1 - x_mean, ..., xn - x_mean) there are only n-1 independant values because their sum is zero.

A quick inspection of the ta-lib sources suggests that ta-lib uses Period rather than Period-1.


Interesting as n/(n-1) approaches infinity, n/(n-1) will approach 1.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1