| 1 | # Copyright 2006, 2007, 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::ChaikinMF; | ||
| 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::Derived::EMA; | ||
| 27 | use App::Chart::Series::Derived::MFI; | ||
| 28 | use App::Chart::Series::Derived::RSI; | ||
| 29 | |||
| 30 | # http://www.incrediblecharts.com/technical/chaikin_money_flow.php | ||
| 31 | # | ||
| 32 | |||
| 33 | sub longname { __('Chaikin Money Flow') } | ||
| 34 | sub shortname { __('Chaikin MF') } | ||
| 35 | sub manual { __p('manual-node','Chaikin Money Flow') } | ||
| 36 | |||
| 37 | use constant | ||
| 38 | { type => 'indicator', | ||
| 39 | minimum => -1, | ||
| 40 | maximum => 1, | ||
| 41 | units => 'minus_one_to_plus_one', | ||
| 42 | hlines => [ 0 ], | ||
| 43 | parameter_info => [ { name => __('Days'), | ||
| 44 | key => 'chaikin_mf_days', | ||
| 45 | type => 'integer', | ||
| 46 | minimum => 1, | ||
| 47 | default => 21 } ] | ||
| 48 | }; | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my ($class, $parent, $N) = @_; | ||
| 52 | |||
| 53 | $N //= parameter_info()->[0]->{'default'}; | ||
| 54 | ($N > 0) || croak "ChaikinMF 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::RSI::warmup_count; # EMA(W)+1 | ||
| 63 | *fill_part = \&App::Chart::Series::Derived::MFI::fill_part; | ||
| 64 | |||
| 65 | sub proc { | ||
| 66 | my ($class_or_self, $N) = @_; | ||
| 67 | my $num_proc = App::Chart::Series::Calculation->sum ($N); | ||
| 68 | my $den_proc = App::Chart::Series::Calculation->sum ($N); | ||
| 69 | |||
| 70 | return sub { | ||
| 71 | my ($high, $low, $close, $volume) = @_; | ||
| 72 | $high //= $close; | ||
| 73 | $low //= $close; | ||
| 74 | $volume //= 0; | ||
| 75 | |||
| 76 | my $diff = $high - $low; | ||
| 77 | my $clv = ($diff == 0 ? 0 | ||
| 78 | # volume times -1 to +1 | ||
| 79 | : $volume * (-1 + 2*($close-$low)/$diff)); | ||
| 80 | my $num = $num_proc->($clv); | ||
| 81 | my $den = $den_proc->($volume); | ||
| 82 | return ($den == 0 ? 0 : $num/$den); | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | __END__ |