| 1 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Series::Derived::HullMA; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Locale::TextDomain 1.17; # for __p() | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | use Math::Trig (); | ||
| 25 | |||
| 26 | use base 'App::Chart::Series::Indicator'; | ||
| 27 | use App::Chart::Series::Derived::WMA; | ||
| 28 | |||
| 29 | |||
| 30 | # http://www.alanhull.com/ | ||
| 31 | # http://www.alanhull.com.au/ | ||
| 32 | # http://www.justdata.com.au/Journals/AlanHull/hull_ma.htm | ||
| 33 | # Alan Hull's explanation and formula, samples of BHP Billiton (symbol | ||
| 34 | # BHP.AX) weekly 2001-2004. | ||
| 35 | # | ||
| 36 | # Old link: http://www.alanhull.com.au/hma/hma.html | ||
| 37 | # | ||
| 38 | |||
| 39 | |||
| 40 | sub longname { __('Hull MA') } | ||
| 41 | *shortname = \&longname; | ||
| 42 | sub manual { __p('manual-node','Hull Moving Average') } | ||
| 43 | |||
| 44 | use constant | ||
| 45 | { type => 'average', | ||
| 46 | parameter_info => [ { name => __('Days'), | ||
| 47 | key => 'hull_ma_days', | ||
| 48 | type => 'integer', | ||
| 49 | minimum => 1, | ||
| 50 | default => 20 } ], | ||
| 51 | }; | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my ($class, $parent, $N) = @_; | ||
| 55 | ### HullMA new(): $N | ||
| 56 | |||
| 57 | $N //= parameter_info()->[0]->{'default'}; | ||
| 58 | ($N > 0) || croak "HullMA bad N: $N"; | ||
| 59 | |||
| 60 | return $class->SUPER::new | ||
| 61 | (parent => $parent, | ||
| 62 | parameters => [ $N ], | ||
| 63 | arrays => { values => [] }, | ||
| 64 | array_aliases => { }); | ||
| 65 | } | ||
| 66 | sub warmup_count { | ||
| 67 | my ($self_or_class, $N) = @_; | ||
| 68 | my $NS = int(sqrt($N)); | ||
| 69 | return (App::Chart::Series::Derived::WMA->warmup_count($N) | ||
| 70 | + App::Chart::Series::Derived::WMA->warmup_count($NS)); | ||
| 71 | } | ||
| 72 | sub proc { | ||
| 73 | my ($self, $N) = @_; | ||
| 74 | my $N2 = int($N/2); | ||
| 75 | my $NS = int(sqrt($N)); | ||
| 76 | |||
| 77 | my $wma_N_proc = App::Chart::Series::Derived::WMA->proc($N); | ||
| 78 | my $wma_N2_proc = App::Chart::Series::Derived::WMA->proc($N2); | ||
| 79 | my $wma_NS_proc = App::Chart::Series::Derived::WMA->proc($NS); | ||
| 80 | |||
| 81 | return sub { | ||
| 82 | my ($value) = @_; | ||
| 83 | return $wma_NS_proc->(2 * $wma_N2_proc->($value) - $wma_N_proc->($value)); | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub FIXME_weights { | ||
| 88 | my ($N) = @_; | ||
| 89 | my @N_weights = reverse 1 .. $N; | ||
| 90 | my $N_total = List::Util::sum (@N_weights); | ||
| 91 | foreach (@N_weights) { $_ /= $N_total } | ||
| 92 | |||
| 93 | my @N2_weights = reverse 1 .. int($N/2); | ||
| 94 | my $N2_total = List::Util::sum (@N2_weights); | ||
| 95 | foreach (@N_weights) { $_ /= $N2_total } | ||
| 96 | |||
| 97 | for (my $i = 0; $i < @N2_weights; $i++) { | ||
| 98 | $N_weights[$i] -= $N2_weights[$i]; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | __END__ |