| 1 | # Copyright 2008, 2009, 2010 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::WMA; | ||
| 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::Calculation; | ||
| 28 | use App::Chart::Series::Derived::SMA; | ||
| 29 | |||
| 30 | # http://www.linnsoft.com/tour/techind/movAvg.htm | ||
| 31 | # Formula, and sample WMA N=20 on Nasdaq 100 (symbol QQQ, yahoo now in | ||
| 32 | # ^IXIC) from 2001. | ||
| 33 | # | ||
| 34 | |||
| 35 | sub longname { __('WMA - Weighted MA') } | ||
| 36 | sub shortname { __('WMA') } | ||
| 37 | sub manual { __p('manual-node','Weighted Moving Average') } | ||
| 38 | |||
| 39 | use constant | ||
| 40 | { priority => 11, | ||
| 41 | type => 'average', | ||
| 42 | parameter_info => [ { name => __('Days'), | ||
| 43 | key => 'wma_days', | ||
| 44 | type => 'integer', | ||
| 45 | minimum => 1, | ||
| 46 | default => 20 } ], | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $parent, $N) = @_; | ||
| 51 | |||
| 52 | $N //= parameter_info()->[0]->{'default'}; | ||
| 53 | ($N > 0) || croak "WMA bad N: $N"; | ||
| 54 | |||
| 55 | return $class->SUPER::new | ||
| 56 | (parent => $parent, | ||
| 57 | parameters => [ $N ], | ||
| 58 | arrays => { values => [] }, | ||
| 59 | array_aliases => { }); | ||
| 60 | } | ||
| 61 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 62 | |||
| 63 | sub proc { | ||
| 64 | my ($class_or_self, $N) = @_; | ||
| 65 | return App::Chart::Series::Calculation::ma_proc_by_weights (reverse 1 .. $N); | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | __END__ |