| 1 | # Copyright 2004, 2005, 2006, 2007, 2009, 2010, 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::KVO; | ||
| 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::KVOforce; | ||
| 27 | use App::Chart::Series::Derived::MACD; | ||
| 28 | |||
| 29 | use constant DEBUG => 0; | ||
| 30 | |||
| 31 | sub longname { __('KVO - Klinger Volume Oscillator') } | ||
| 32 | sub shortname { __('KVO') } | ||
| 33 | sub manual { __p('manual-node','Klinger Volume Oscillator') } | ||
| 34 | |||
| 35 | use constant | ||
| 36 | { type => 'indicator', | ||
| 37 | units => 'KVO', | ||
| 38 | hlines => [ 0 ], | ||
| 39 | parameter_info => [ { name => __('Fast days'), | ||
| 40 | key => 'kvo_fast_days', | ||
| 41 | type => 'float', | ||
| 42 | minimum => 1, | ||
| 43 | default => 34, | ||
| 44 | decimals => 0, | ||
| 45 | step => 1 }, | ||
| 46 | { name => __('Slow days'), | ||
| 47 | key => 'kvo_slow_days', | ||
| 48 | type => 'float', | ||
| 49 | minimum => 1, | ||
| 50 | default => 55, | ||
| 51 | decimals => 0, | ||
| 52 | step => 1 }, | ||
| 53 | { name => __('Trigger'), | ||
| 54 | key => 'kvo_trigger_days', | ||
| 55 | type => 'float', | ||
| 56 | minimum => 1, | ||
| 57 | default => 13, | ||
| 58 | decimals => 0, | ||
| 59 | step => 1 }, | ||
| 60 | { name => __('Histogram'), | ||
| 61 | key => 'kvo_histogram', | ||
| 62 | type => 'boolean', | ||
| 63 | default => 1 } ], | ||
| 64 | |||
| 65 | # FIXME: LineStyle solid for histogram | ||
| 66 | line_colours => { values => App::Chart::UP_COLOUR(), | ||
| 67 | trigger => App::Chart::DOWN_COLOUR() }, | ||
| 68 | }; | ||
| 69 | |||
| 70 | sub new { | ||
| 71 | my ($class, $parent, $fast_N, $slow_N, $trigger_N, $histogram) = @_; | ||
| 72 | |||
| 73 | $fast_N //= parameter_info()->[0]->{'default'}; | ||
| 74 | ($fast_N > 0) || croak "KVO bad fast N: $fast_N"; | ||
| 75 | |||
| 76 | $slow_N //= parameter_info()->[1]->{'default'}; | ||
| 77 | ($slow_N > 0) || croak "KVO bad slow N: $slow_N"; | ||
| 78 | |||
| 79 | $trigger_N //= parameter_info()->[2]->{'default'}; | ||
| 80 | ($trigger_N > 0) || croak "KVO bad trigger N: $trigger_N"; | ||
| 81 | |||
| 82 | $histogram //= parameter_info()->[3]->{'default'}; | ||
| 83 | |||
| 84 | return $class->SUPER::new | ||
| 85 | (parent => $parent, | ||
| 86 | parameters => [ $fast_N, $slow_N, $trigger_N, $histogram ], | ||
| 87 | arrays => { values => [], | ||
| 88 | trigger => [], | ||
| 89 | ($histogram ? (histogram => []) : ()), | ||
| 90 | }); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub proc { | ||
| 94 | my ($class_or_self, $fast_N, $slow_N, $trigger_N, $histogram) = @_; | ||
| 95 | |||
| 96 | my $vf_proc = App::Chart::Series::Derived::KVOforce->proc(); | ||
| 97 | my $macd_proc = App::Chart::Series::Derived::MACD->proc | ||
| 98 | ($fast_N, $slow_N, $trigger_N); | ||
| 99 | |||
| 100 | return sub { | ||
| 101 | my ($high, $low, $close, $volume) = @_; | ||
| 102 | my $vf = $vf_proc->($high, $low, $close, $volume) // return; | ||
| 103 | return $macd_proc->($vf); | ||
| 104 | }; | ||
| 105 | } | ||
| 106 | sub warmup_count_for_position { | ||
| 107 | my ($self, $lo) = @_; | ||
| 108 | if (DEBUG) { print "KVO warmup_count_for_position $lo\n"; } | ||
| 109 | |||
| 110 | # MACD warmup | ||
| 111 | my $macd_warmup_count | ||
| 112 | = App::Chart::Series::Derived::MACD->warmup_count(@{$self->{'parameters'}}); | ||
| 113 | my $parent = $self->{'parent'}; | ||
| 114 | $lo = $parent->find_before ($lo, $macd_warmup_count); | ||
| 115 | if (DEBUG) { print " MACD $macd_warmup_count to $lo\n"; } | ||
| 116 | |||
| 117 | return $self->App::Chart::Series::Derived::KVOforce::warmup_count_for_position($lo); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub fill_part { | ||
| 121 | my ($self, $lo, $hi) = @_; | ||
| 122 | my $parent = $self->{'parent'}; | ||
| 123 | |||
| 124 | my $warmup_count = $self->warmup_count_for_position ($lo); | ||
| 125 | my $start = $parent->find_before ($lo, $warmup_count); | ||
| 126 | $parent->fill ($start, $hi); | ||
| 127 | my $p = $parent->values_array; | ||
| 128 | my $ph = $parent->array('highs') || $p; | ||
| 129 | my $pl = $parent->array('lows') || $p; | ||
| 130 | my $pv = $parent->array('volumes') || []; | ||
| 131 | |||
| 132 | my $s_values = $self->array('values'); | ||
| 133 | my $s_trigger = $self->array('trigger'); | ||
| 134 | my $s_histogram = $self->array('histogram') || []; | ||
| 135 | $hi = min ($hi, $#$p); | ||
| 136 | if ($#$s_values < $hi) { $#$s_values = $hi; } # pre-extend | ||
| 137 | if ($#$s_trigger < $hi) { $#$s_trigger = $hi; } # pre-extend | ||
| 138 | if ($#$s_histogram < $hi) { $#$s_histogram = $hi; } # pre-extend | ||
| 139 | |||
| 140 | my $proc = $self->proc(@{$self->{'parameters'}}); | ||
| 141 | |||
| 142 | foreach my $i ($start .. $lo-1) { | ||
| 143 | my $close = $p->[$i] // next; | ||
| 144 | $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]); | ||
| 145 | } | ||
| 146 | |||
| 147 | foreach my $i ($lo .. $hi) { | ||
| 148 | my $close = $p->[$i] // next; | ||
| 149 | ($s_values->[$i], $s_trigger->[$i], $s_histogram->[$i]) | ||
| 150 | = $proc->($ph->[$i], $pl->[$i], $close, $pv->[$i]); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | 1; | ||
| 156 | __END__ |