| 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::SineMA; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | use Math::Trig (); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | use App::Chart::Series::Calculation; | ||
| 27 | use App::Chart::Series::Derived::SMA; | ||
| 28 | |||
| 29 | |||
| 30 | # http://www.linnsoft.com/tour/techind/movAvg.htm | ||
| 31 | # Formula, and sample SineMA[20] of Nasdaq 100 (symbol QQQ, yahoo now | ||
| 32 | # ^IXIC) from 2001. | ||
| 33 | # | ||
| 34 | |||
| 35 | sub longname { __('Sine Weighted MA') } | ||
| 36 | sub shortname { __('SineMA') } | ||
| 37 | sub manual { __p('manual-node','Sine Weighted Moving Average') } | ||
| 38 | |||
| 39 | use constant | ||
| 40 | { priority => -10, | ||
| 41 | type => 'average', | ||
| 42 | parameter_info => [ { name => __('Days'), | ||
| 43 | key => 'sine_ma_days', | ||
| 44 | type => 'integer', | ||
| 45 | minimum => 1, | ||
| 46 | default => 20 } ], | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $parent, $N) = @_; | ||
| 51 | ### SineMA new(): @_ | ||
| 52 | |||
| 53 | $N //= parameter_info()->[0]->{'default'}; | ||
| 54 | ($N > 0) || croak "SineMA bad N: $N"; | ||
| 55 | |||
| 56 | return $class->SUPER::new | ||
| 57 | (parent => $parent, | ||
| 58 | parameters => [ $N ], | ||
| 59 | arrays => { values => [] }, | ||
| 60 | array_aliases => { }); | ||
| 61 | } | ||
| 62 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 63 | |||
| 64 | sub proc { | ||
| 65 | my ($self_or_class, $N) = @_; | ||
| 66 | # / $i \ | ||
| 67 | # weight[$i] = sin | ------ * pi | | ||
| 68 | # \ $N+1 / | ||
| 69 | return App::Chart::Series::Calculation::ma_proc_by_weights | ||
| 70 | (map {sin ($_ / ($N+1) * Math::Trig::pi())} 1 .. $N); | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | __END__ |