Gamestudio Links
Zorro Links
Newest Posts
SGT_FW
by Aku_Aku. 11/30/26 08:19
Removing outliers from files
by JeyKey II. 11/29/26 10:01
Experiences with cTrader
by JeyKey II. 11/27/26 09:04
make a complaint
by EW_bytes. 11/26/26 23:00
FXCM => AVA Trade Plugin???
by Martin_HH. 11/25/26 21:59
No connection to MT4
by Martin_HH. 11/23/26 06:38
MinMax function on History t6 file
by LucasJoshua. 11/21/26 14:28
What are you working on?
by rayp. 11/20/26 18:10
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
2 registered members (JeyKey II, Grant), 5,607 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EvanTJ, rht, MaliceTheMaker, Haitao_Ren, JimSimons
19250 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: 28,149
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,149
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: 74
F
firecrest Offline
Junior Member
firecrest  Offline
Junior Member
F

Joined: Dec 2016
Posts: 74
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