Might be a bug in bollinger Bands calculation

32 views
Skip to first unread message

Jim

unread,
Jul 19, 2009, 4:40:36 PM7/19/09
to stockmonkey
There is some same source code on
http://search.cpan.org/~jettero/stockmonkey-2.9013/Business/BollingerBands.pm.

It has 22 data and bollinger bands numbers are:
BB: 3.60824863529101 < 6.15 < 8.69175136470899.

I think the first two numbers (3 and 4) should not count.

I removed the first two numbers, I got this result:
BB: 3.7604393709303 < 6.15 < 8.5395606290697.

Note that the average is the same. But the standard deviation changed.
Should it be the same.

I added the first two numbers back, and changed the second one from 4
to 400. I got this:
BB: -32.191576649898 < 6.15 < 44.491576649898.

For what I understand, the standard deviation should be calculated in
the past 20 days, the same days as you calculate average. So the extra
two days' data should not matter.

Let me know if I understand it wrong. Otherwise, it's a bug.

Paul Miller

unread,
Jul 20, 2009, 9:30:37 AM7/20/09
to stockmonkey


On Jul 19, 4:40 pm, Jim <jimyu...@gmail.com> wrote:
> It has 22 data and bollinger bands numbers are:
> BB: 3.60824863529101 < 6.15 < 8.69175136470899.
>
> I think the first two numbers (3 and 4) should not count.

Probably.
my $bb = Math::Business::BollingerBands->new(20);
$bb->insert( (5) x 27 );

my(undef,$mean,undef) = $bb->query;
# I'm getting 5 like I should

$bb->insert(7,7);
(undef,$mean,undef) = $bb->query;
# I'm getting 5.2 like I should

> I removed the first two numbers, I got this result:
> BB: 3.7604393709303 < 6.15 < 8.5395606290697.

How did you "remove" them? There isn't really an interface for that.
This module automatically shifts irrelevant data from the data store
on insert, so inserting extra values likely has little effect.

use Statistics::Basic qw(mean);

# Note that while this
mean(qw(3 4 4 5 6 5 6 5 5 5 5 6 6 6 6 7 7 7 8 8 8 8));
# returns 5.91

# and this
mean(qw(4 5 6 5 6 5 5 5 5 6 6 6 6 7 7 7 8 8 8 8));
# returns 6.51,

# this
$bb->insert(qw(3 4 4 5 6 5 6 5 5 5 5 6 6 6 6 7 7 7 8 8 8 8))
(undef,$mean,undef) = $bb->query;
# $mean is 6.15

$bb->insert(qw(4 5 6 5 6 5 5 5 5 6 6 6 6 7 7 7 8 8 8 8))
(undef,$mean,undef) = $bb->query;
# $mean is 6.15

In order to use those two extra values, you'd have to increase the
size of the bands from 20 days to 22 days.

> For what I understand, the standard deviation should be calculated in
> the past 20 days, the same days as you calculate average. So the extra
> two days' data should not matter.

They don't appear to.

> Let me know if I understand it wrong. Otherwise, it's a bug.

Likewise.

Paul Miller

unread,
Jul 20, 2009, 9:41:27 AM7/20/09
to stockmonkey


On Jul 20, 9:30 am, Paul Miller <jett...@gmail.com> wrote:
> my $bb = Math::Business::BollingerBands->new(20);

Grrrz, errata, this should be:

my $bb = Math::Business::BollingerBands->new(20, 2);

Or simply:

my $bb = Math::Business::BollingerBands->recommended;
Reply all
Reply to author
Forward
0 new messages