| 1 | # Copyright 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2024 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::MACD; | ||
| 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 | |||
| 28 | |||
| 29 | sub longname { __('MACD - Momentum Advance/Decline') } | ||
| 30 | sub shortname { __('MACD') } | ||
| 31 | sub manual { __p('manual-node','MACD') } | ||
| 32 | |||
| 33 | use constant | ||
| 34 | { type => 'indicator', | ||
| 35 | units => 'price', | ||
| 36 | hlines => [ 0 ], | ||
| 37 | parameter_info => [ { name => __('Fast days'), | ||
| 38 | key => 'macd_fast_days', | ||
| 39 | type => 'float', | ||
| 40 | minimum => 1, | ||
| 41 | default => 12, | ||
| 42 | decimals => 0, | ||
| 43 | step => 1 }, | ||
| 44 | { name => __('Slow days'), | ||
| 45 | key => 'macd_slow_days', | ||
| 46 | type => 'float', | ||
| 47 | minimum => 1, | ||
| 48 | default => 26, | ||
| 49 | decimals => 0, | ||
| 50 | step => 1 }, | ||
| 51 | { name => __('Smooth'), | ||
| 52 | key => 'macd_smooth_days', | ||
| 53 | type => 'float', | ||
| 54 | minimum => 1, | ||
| 55 | default => 9, | ||
| 56 | decimals => 0, | ||
| 57 | step => 1 }, | ||
| 58 | { name => __('Histogram'), | ||
| 59 | key => 'macd_histogram', | ||
| 60 | type => 'boolean', | ||
| 61 | default => 1 }], | ||
| 62 | |||
| 63 | # FIXME: LineStyle solid for histogram | ||
| 64 | line_colours => { macd => App::Chart::UP_COLOUR(), | ||
| 65 | smooth => App::Chart::DOWN_COLOUR() }, | ||
| 66 | }; | ||
| 67 | |||
| 68 | sub new { | ||
| 69 | my ($class, $parent, $fast_N, $slow_N, $smooth_N, $histogram) = @_; | ||
| 70 | |||
| 71 | $fast_N //= parameter_info()->[0]->{'default'}; | ||
| 72 | ($fast_N > 0) || croak "MACD bad fast N: $fast_N"; | ||
| 73 | |||
| 74 | $slow_N //= parameter_info()->[1]->{'default'}; | ||
| 75 | ($slow_N > 0) || croak "MACD bad slow N: $slow_N"; | ||
| 76 | |||
| 77 | $smooth_N //= parameter_info()->[2]->{'default'}; | ||
| 78 | ($smooth_N > 0) || croak "MACD bad smooth N: $smooth_N"; | ||
| 79 | |||
| 80 | $histogram //= parameter_info()->[3]->{'default'}; | ||
| 81 | |||
| 82 | return $class->SUPER::new | ||
| 83 | (parent => $parent, | ||
| 84 | parameters => [ $fast_N, $slow_N, $smooth_N, $histogram ], | ||
| 85 | arrays => { macd => [], | ||
| 86 | smooth => [], | ||
| 87 | ($histogram ? (histogram => []) : ()), | ||
| 88 | }, | ||
| 89 | array_aliases => { values => 'macd' }); | ||
| 90 | } | ||
| 91 | sub proc { | ||
| 92 | my ($class_or_self, $fast_N, $slow_N, $smooth_N) = @_; | ||
| 93 | # print "MACD proc $fast_N, $slow_N, $smooth_N\n"; | ||
| 94 | my $fast_proc = App::Chart::Series::Derived::EMA->proc($fast_N); | ||
| 95 | my $slow_proc = App::Chart::Series::Derived::EMA->proc($slow_N); | ||
| 96 | my $smooth_proc = App::Chart::Series::Derived::EMA->proc($smooth_N); | ||
| 97 | |||
| 98 | return sub { | ||
| 99 | my ($value) = @_; | ||
| 100 | |||
| 101 | my $fast = $fast_proc->($value); | ||
| 102 | my $slow = $slow_proc->($value); | ||
| 103 | my $macd = $fast - $slow; | ||
| 104 | my $smooth = $smooth_proc->($macd); | ||
| 105 | return ($macd, $smooth, $macd-$smooth); | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | sub warmup_count { | ||
| 109 | my ($self_or_class, $fast_N, $slow_N, $smooth_N) = @_; | ||
| 110 | # FIXME: as roughly EMA of EMA it should be a bit less than this ... | ||
| 111 | return (App::Chart::Series::Derived::EMA->warmup_count(max($fast_N,$slow_N)), | ||
| 112 | + App::Chart::Series::Derived::EMA->warmup_count($smooth_N)); | ||
| 113 | |||
| 114 | } | ||
| 115 | |||
| 116 | sub fill_part { | ||
| 117 | my ($self, $lo, $hi) = @_; | ||
| 118 | my $parent = $self->{'parent'}; | ||
| 119 | |||
| 120 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 121 | my $start = $parent->find_before ($lo, $warmup_count); | ||
| 122 | $parent->fill ($start, $hi); | ||
| 123 | my $p = $parent->values_array; | ||
| 124 | |||
| 125 | my $s_macd = $self->array('macd'); | ||
| 126 | my $s_smooth = $self->array('smooth'); | ||
| 127 | my $s_histogram = $self->array('histogram') || []; | ||
| 128 | $hi = min ($hi, $#$p); | ||
| 129 | if ($#$s_macd < $hi) { $#$s_macd = $hi; } # pre-extend | ||
| 130 | if ($#$s_smooth < $hi) { $#$s_smooth = $hi; } # pre-extend | ||
| 131 | if ($#$s_histogram < $hi) { $#$s_histogram = $hi; } # pre-extend | ||
| 132 | |||
| 133 | my $proc = $self->proc(@{$self->{'parameters'}}); | ||
| 134 | |||
| 135 | foreach my $i ($start .. $lo-1) { | ||
| 136 | my $value = $p->[$i] // next; | ||
| 137 | $proc->($value); | ||
| 138 | } | ||
| 139 | |||
| 140 | foreach my $i ($lo .. $hi) { | ||
| 141 | my $value = $p->[$i] // next; | ||
| 142 | ($s_macd->[$i], $s_smooth->[$i], $s_histogram->[$i]) = $proc->($value); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | __END__ |