| 1 | # Copyright 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::CMO; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | use App::Chart::Series::Calculation; | ||
| 27 | use App::Chart::Series::Derived::MFI; | ||
| 28 | use App::Chart::Series::Derived::SMA; | ||
| 29 | |||
| 30 | |||
| 31 | # http://www.equis.com/customer/resources/formulas/formula.aspx?Id=8 | ||
| 32 | # Metastock formulas. | ||
| 33 | # | ||
| 34 | # http://trader.online.pl/MSZ/e-w-Chandes_Momentum_Oscillator.html | ||
| 35 | # Formulas and text from equis.com. | ||
| 36 | # | ||
| 37 | |||
| 38 | |||
| 39 | sub longname { __('CMO - Chande Momentum Oscillator') } | ||
| 40 | sub shortname { __('CMO') } | ||
| 41 | sub manual { __p('manual-node','Chande Momentum Oscillator') } | ||
| 42 | |||
| 43 | use constant | ||
| 44 | { hlines => [ -50, 50 ], | ||
| 45 | type => 'indicator', | ||
| 46 | units => 'percentage_plus_or_minus_100', | ||
| 47 | minimum => -100, | ||
| 48 | maximum => 100, | ||
| 49 | parameter_info => [ { name => __('Days'), | ||
| 50 | key => 'cmo_days', | ||
| 51 | type => 'integer', | ||
| 52 | minimum => 1, | ||
| 53 | default => 14 } ], | ||
| 54 | }; | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my ($class, $parent, $N) = @_; | ||
| 58 | |||
| 59 | $N //= parameter_info()->[0]->{'default'}; | ||
| 60 | ($N > 0) || croak "CMO bad N: $N"; | ||
| 61 | |||
| 62 | return $class->SUPER::new | ||
| 63 | (parent => $parent, | ||
| 64 | parameters => [ $N ], | ||
| 65 | arrays => { values => [] }, | ||
| 66 | array_aliases => { }); | ||
| 67 | } | ||
| 68 | *warmup_count = \&App::Chart::Series::Derived::MFI::warmup_count; # $N | ||
| 69 | |||
| 70 | sub proc { | ||
| 71 | my ($class_or_self, $N) = @_; | ||
| 72 | my $num_proc = App::Chart::Series::Calculation->sum ($N); | ||
| 73 | my $den_proc = App::Chart::Series::Calculation->sum ($N); | ||
| 74 | my $prev; | ||
| 75 | return sub { | ||
| 76 | my ($value) = @_; | ||
| 77 | my $ret; | ||
| 78 | if (defined $prev) { | ||
| 79 | my $diff = $value - $prev; | ||
| 80 | my $num = $num_proc->($diff); | ||
| 81 | my $den = $den_proc->(abs $diff); | ||
| 82 | $ret = ($den == 0 ? 0 : 100 * $num / $den); | ||
| 83 | } | ||
| 84 | $prev = $value; | ||
| 85 | return $ret; | ||
| 86 | }; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | __END__ |