| 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::TwiggsMF; | ||
| 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::RSI; | ||
| 28 | use App::Chart::Series::Derived::ChaikinMF; | ||
| 29 | |||
| 30 | # http://www.incrediblecharts.com/indicators/twiggs_money_flow.php | ||
| 31 | # | ||
| 32 | |||
| 33 | sub longname { __('Twiggs Money Flow') } | ||
| 34 | sub shortname { __('Twiggs MF') } | ||
| 35 | sub manual { __p('manual-node','Twiggs 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 => 'twiggs_mf_days', | ||
| 45 | type => 'float', | ||
| 46 | minimum => 1, | ||
| 47 | default => 21, | ||
| 48 | decimals => 0, | ||
| 49 | step => 1 } ], | ||
| 50 | }; | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($class, $parent, $N) = @_; | ||
| 54 | |||
| 55 | $N //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N > 0) || croak "TwiggsMF bad N: $N"; | ||
| 57 | |||
| 58 | return $class->SUPER::new | ||
| 59 | (parent => $parent, | ||
| 60 | parameters => [ $N ], | ||
| 61 | arrays => { values => [] }, | ||
| 62 | array_aliases => { }); | ||
| 63 | } | ||
| 64 | *warmup_count = \&App::Chart::Series::Derived::RSI::warmup_count; | ||
| 65 | |||
| 66 | sub proc { | ||
| 67 | my ($class_or_self, $N) = @_; | ||
| 68 | |||
| 69 | $N = App::Chart::Series::Derived::EMA::N_from_Wilder_N($N); | ||
| 70 | my $num_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 71 | my $den_proc = App::Chart::Series::Derived::EMA->proc ($N); | ||
| 72 | my $prev_close; | ||
| 73 | |||
| 74 | return sub { | ||
| 75 | my ($high, $low, $close, $volume) = @_; | ||
| 76 | $high //= $close; | ||
| 77 | $low //= $close; | ||
| 78 | $volume //= 0; | ||
| 79 | |||
| 80 | # true range style high and low | ||
| 81 | if (defined $prev_close) { | ||
| 82 | $high = max ($high, $prev_close); | ||
| 83 | $low = min ($low, $prev_close); | ||
| 84 | } | ||
| 85 | $prev_close = $close; | ||
| 86 | |||
| 87 | my $diff = $high - $low; | ||
| 88 | my $clv = ($diff == 0 ? 0 | ||
| 89 | # volume times -1 to +1 | ||
| 90 | : $volume * (-1 + 2*($close-$low)/$diff)); | ||
| 91 | my $num = $num_proc->($clv); | ||
| 92 | my $den = $den_proc->($volume); | ||
| 93 | return ($den == 0 ? 0 : $num/$den); | ||
| 94 | }; | ||
| 95 | } | ||
| 96 | |||
| 97 | *fill_part = \&App::Chart::Series::Derived::MFI::fill_part; | ||
| 98 | |||
| 99 | 1; | ||
| 100 | __END__ |