Skip Menu |

This queue is for tickets about the stockmonkey CPAN distribution.

Report information
The Basics
Id: 37269
Status: resolved
Priority: 0/
Queue: stockmonkey

People
Owner: Nobody in particular
Requestors: johnb [...] listbrokers.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Laguerre Moving Average
Did some more work on finding a better moving average and came upon the laguerre polynomial filter. Looks like the best so far. Very smooth and responsive. You can find more info at: http://www.mesasoftware.com/technicalpapers.htm - look for the Time Warp paper. Hacked out some POS code based on the paper, don't laugh, maybe you can make a module out of it. Email me if it's too confusing. Thanks, John Baker
Subject: laguerre.filter.pl
#!/usr/bin/perl use List::Util qw(max min); sub Median { # median may or may not be an element of the array # got this from Math::NumberCruncher my $arrayref = shift; return undef unless defined $arrayref && @$arrayref > 0; my @array = sort { $a <=> $b } @$arrayref; if ( @array % 2 ) { $median = $array[ @array / 2 ]; } else { $median = ( $array[ @array / 2 - 1 ] + $array[ @array / 2 ] ) / 2; } return $median; } sub FIR { my $price1 = $_[0]; my $price2 = $_[1]; my $price3 = $_[2]; my $price4 = $_[3]; return ( ( $price1 + 2 * $price2 + 2 * $price3 + $price4 ) / 6 ); } sub dollar_round { my $n = shift; my $minus = $n < 0 ? '-' : ''; $n = abs($n); $n = int( ( $n + .005 ) * 100 ) / 100; $n .= '.00' unless $n =~ /\./; $n .= '0' if substr( $n, ( length($n) - 2 ), 1 ) == '.'; chop $n if $n =~ /\.\d\d0$/; return "$minus$n"; } my $L0; my $L1; my $L2; my $L3; my $gamma = .8; #Starting Gamma, will change my $length = 20; #Set Days for gamma adjustment my $counter; # used last 50 odd days of IWM for testdata @testdata = qw ( 70.92 71.67 71.68 70.37 70.55 71.23 71.90 72.38 71.74 71.40 72.63 72.55 72.27 72.86 71.56 71.71 71.50 73.07 73.49 73.40 73.55 73.95 73.97 73.70 72.65 73.17 72.21 73.32 73.66 74.41 74.67 73.99 73.84 74.30 76.20 73.92 73.68 73.14 71.87 72.02 73.32 73.78 73.72 73.13 73.74 72.57 71.89 70.70 71.50 69.66 69.73 69.05); $L0 = $L1 = $L2 = $L3 = $testdata[0]; foreach $datum (@testdata) { # gcf array holds the variances between the latest price and the last # filter value push @gcf, abs( $datum - $firfilter[ $counter - 1 ] ) if $counter > 0; shift @gcf if $counter >= $length; # we only want to see the last $length values $L0A = $L0; $L1A = $L1; $L2A = $L2; $L3A = $L3; $L0 = $gamma * $datum + ( 1 - $gamma ) * $L0A; $L1 = -( 1 - $gamma ) * $L0 + $L0A + ( 1 - $gamma ) * $L1A; $L2 = -( 1 - $gamma ) * $L1 + $L1A + ( 1 - $gamma ) * $L2A; $L3 = -( 1 - $gamma ) * $L2 + $L2A + ( 1 - $gamma ) * $L3A; push @firfilter, FIR( $L0, $L1, $L2, $L3 ); $counter++; if ( $counter >= $length ) { # we have enough values my $highesthigh = max(@gcf); # the greatest variance my $lowestlow = min(@gcf); # the least variance my @lastfive = @gcf; # splice off the last five of this below my @gammafinder; # calculates the gamma as per the paper; the author recommended # the last five bars for ( splice( @lastfive, $#lastfive - 4 ) ) { if ( $highesthigh - $lowestlow != 0 ) { push @gammafinder, ( $_ - $lowestlow ) / ( $highesthigh - $lowestlow ); } } # voila, adjustable gamma $gamma = Median( \@gammafinder ); print dollar_round($gamma), "\t", dollar_round( $firfilter[ $counter - 1 ] ), "\n"; } }
Subject: Re: [rt.cpan.org #37269] Laguerre Moving Average
Date: Tue, 1 Jul 2008 06:20:52 -0400
To: John Baker via RT <bug-stockmonkey [...] rt.cpan.org>
From: Paul Miller <jettero [...] cpan.org>
On Tue, Jul 01, 2008 at 02:25:23AM -0400, John Baker via RT wrote: Show quoted text
> http://www.mesasoftware.com/technicalpapers.htm - look for the Time Warp > paper.
This is really interesting. I'll check it out. -- If riding in an airplane is flying, then riding in a boat is swimming. 85 jumps, 36.0 minutes of freefall, 69.1 freefall miles.
On Tue Jul 01 02:25:18 2008, johnab wrote: Show quoted text
> Hacked out some POS code based on the paper, don't laugh, maybe you can > make a module out of it. Email me if it's too confusing.
It actually seems pretty readable to me. Perhaps there's some things I could speed up. The real problem is that I can't find any original sources on this. The site you linked to has all it's docs in .exe format. Seriously. Documents in .exe format. Or perhaps they're not documents. I wouldn't know, not using win32. -- If riding in an airplane is flying, then riding in a boat is swimming. 85 jumps, 36.0 minutes of freefall, 69.1 freefall miles.
Subject: Re: [rt.cpan.org #37269] Laguerre Moving Average
Date: Tue, 01 Jul 2008 13:07:49 -0500
To: bug-stockmonkey [...] rt.cpan.org
From: John Baker <JohnB [...] listbrokers.com>
Yeah, it's a doc. I used OpenOffice on it. Reworked as a pdf. Attached. You are too kind on your code comments. Paul Miller via RT wrote: Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=37269 > > > On Tue Jul 01 02:25:18 2008, johnab wrote: >
>> Hacked out some POS code based on the paper, don't laugh, maybe you can >> make a module out of it. Email me if it's too confusing. >>
> > It actually seems pretty readable to me. Perhaps there's some things I > could speed up. The real problem is that I can't find any original > sources on this. The site you linked to has all it's docs in .exe format. > > Seriously. Documents in .exe format. Or perhaps they're not documents. > I wouldn't know, not using win32. > >

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #37269] Laguerre Moving Average
Date: Tue, 1 Jul 2008 15:04:50 -0400
To: John Baker via RT <bug-stockmonkey [...] rt.cpan.org>
From: Paul Miller <jettero [...] cpan.org>
On Tue, Jul 01, 2008 at 02:10:33PM -0400, John Baker via RT wrote: Show quoted text
> Yeah, it's a doc. I used OpenOffice on it. Reworked as a pdf. Attached.
Oh, my mistake. I should have tried that. Still, .exe? Were you using a Laguerre Filter? or the Adaptive Laguerre Filter? He Ehlers discusses both in that unlikely named paper. I may implement both as they both seem pretty useful for data smoothing even outside technical analysis. -Paul -- If riding in an airplane is flying, then riding in a boat is swimming. 85 jumps, 36.0 minutes of freefall, 69.1 freefall miles.
Subject: Re: [rt.cpan.org #37269] Laguerre Moving Average
Date: Tue, 01 Jul 2008 15:05:26 -0500
To: bug-stockmonkey [...] rt.cpan.org
From: John Baker <JohnB [...] listbrokers.com>
Agreed, the exe was kinda screwy. My program uses the adaptive filtering method, but it's easy to do either one: The adaptive filtering works like this: First I store the latest 20 (number stored in $length) differences between what the FIR Filter returns and the next day's price in @gcf. I only chose 20 because it's what I use in my ATR calculations. (And I think it's in his paper) The maximum difference - or variance - is $highesthigh, smallest is $lowestlow Once I have 20 values, I splice off the last five and normalize with this line: ( $_ - $lowestlow ) / ( $highesthigh - $lowestlow ). Say 10 days ago the FIR filter returned 42.00 and nine days ago the actual value was 43.25 and furthermore this 1.25 difference turned out to be the greatest difference from the last 20. Let's say also that the lowest difference from the last 20 days was .25 Now let's say you're on day 20 and the last 5 differences are .50 , .25 , .40 , .75, .60. Now we can figure out the gamma as the median of these 5 values: Day 16 - Take (.50 - .25) / (1.25 - .25) = .25 Day 17 - Take (.25 - .25) / (1.25 - .25) = 0 Day 18 - Take (.40 - .25) / (1.25 - .25) = .15 Day 19 - Take (.75 - .25) / (1.25 - .25) = .50 Day 20 - Take (.60 - .25) / (1.25 - .25) = .35 Median value = gamma = .25 That's how it works. As the variances get closer to the largest difference (meaning big price moves), the gamma value gets closer to 1. If the variances are low, the filter doesn't move much since the gamma is low. By the way, the value of 5 for choosing the median came from his paper. So, the if clause [starts with if ( $counter >= $length )] does the adaptive filtering. It only starts to adapt after $length number of records. If you drop the clause, you could just go static with the gamma value of your choosing. - John Paul Miller via RT wrote: Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=37269 > > > > > On Tue, Jul 01, 2008 at 02:10:33PM -0400, John Baker via RT wrote: >
>> Yeah, it's a doc. I used OpenOffice on it. Reworked as a pdf. Attached. >>
> > Oh, my mistake. I should have tried that. Still, .exe? > > Were you using a Laguerre Filter? or the Adaptive Laguerre > Filter? > > He Ehlers discusses both in that unlikely named paper. > > I may implement both as they both seem pretty useful for data > smoothing even outside technical analysis. > > -Paul > > >
On Tue Jul 01 02:25:18 2008, johnab wrote: Show quoted text
> Did some more work on finding a better moving average and came upon the > laguerre polynomial filter. Looks like the best so far. Very smooth > and responsive. You can find more info at: > http://www.mesasoftware.com/technicalpapers.htm - look for the Time Warp > paper.
I forgot to close this ticket, but did in fact post the LF to CPAN a week or so ago. -- If riding in an airplane is flying, then riding in a boat is swimming. 94 jumps, 38.8 minutes of freefall, 75.7 freefall miles.
Then somehow failed to close the ticket when I meant to close the ticket. -- If riding in an airplane is flying, then riding in a boat is swimming. 94 jumps, 38.8 minutes of freefall, 75.7 freefall miles.
Subject: Re: [rt.cpan.org #37269] Laguerre Moving Average
Date: Fri, 25 Jul 2008 10:54:02 -0500
To: bug-stockmonkey [...] rt.cpan.org
From: John Baker <JohnB [...] listbrokers.com>
I saw it earlier but forgot to say thanks for your work - and thanks for my credit. I made CPAN! If I run across anything else interesting, I'll pass it along. John Paul Miller via RT wrote: Show quoted text
> <URL: http://rt.cpan.org/Ticket/Display.html?id=37269 > > > On Tue Jul 01 02:25:18 2008, johnab wrote: >
>> Did some more work on finding a better moving average and came upon the >> laguerre polynomial filter. Looks like the best so far. Very smooth >> and responsive. You can find more info at: >> http://www.mesasoftware.com/technicalpapers.htm - look for the Time Warp >> paper. >>
> > I forgot to close this ticket, but did in fact post the LF to CPAN a > week or so ago. > >
Glad you like the credit. FYI, replying re-opens the ticket... so I'm just re-closing it. -Paul -- If riding in an airplane is flying, then riding in a boat is swimming. 94 jumps, 38.8 minutes of freefall, 75.7 freefall miles.