App/Chart/Series/Derived/KVOforce.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 169 CC 12 SCAR 31.7
CC
12
Cov
0%
warmup_count_for_position
56.0
proc
30.0
new
6.0
CRAP 23.9
1 # Copyright 2006, 2007, 2009, 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::KVOforce;
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::MFI;
27  
28 use constant DEBUG => 0;
29  
30  
31 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/1297/TradersTips/Tips9712.html
32 # Traders Tips December 1997 formulas.
33 #
34 # http://trader.online.pl/ELZ/t-i-Klinger_Volume_Oscillator.html
35 # Repeat of EasyChart section of traders tips above.
36 #
37 # http://www.prophet.net/learn/taglossary.jsp?index=K
38 # Sample chart of intel (INTC) Nov02-Feb03, KVO and also histogram.
39 # http://www.prophet.net/analyze/popglossary.jsp?studyid=KVO
40 # Same chart of INTC, but just the KVO not the histogram.
41  
42  
43 use constant
44 { longname => __('KVO volume force'),
45 shortname => __('KVO force'),
46 manual => __p('manual-node','Klinger Volume Oscillator'),
47 priority => -10,
48 type => 'indicator',
49 units => 'KVOforce',
50 hlines => [ 0 ],
51 parameter_info => [ ],
52 };
53  
54 sub new {
55 my ($class, $parent) = @_;
56 if (DEBUG) { print "KVOforce new\n"; }
57  
58 return $class->SUPER::new
59 (parent => $parent,
60 parameters => [ ],
61 arrays => { values => [] },
62 array_aliases => { });
63 }
64 *fill_part = \&App::Chart::Series::Derived::MFI::fill_part;
65  
66 sub proc {
67 my ($class_or_self) = @_;
68  
69 my $prev_hlc;
70 my $prev_trend = -100;
71 my $prev_dm = 0;
72 my $cm = 0;
73  
74 return sub {
75 my ($high, $low, $close, $volume) = @_;
76 $high //= $close;
77 $low //= $close;
78 $volume //= 0;
79  
80 my $hlc = $high + $low + $close;
81 my $dm = $high - $low;
82 my $force;
83 if (defined $prev_hlc) {
84 my $trend = ($hlc > $prev_hlc ? 1 : -1);
85  
86 if ($trend == $prev_trend) {
87 $cm += $dm;
88 } else {
89 $cm = $dm + $prev_dm;
90 }
91 $force = ($cm == 0 ? 0
92 : $volume * $trend * abs (2*$dm/$cm - 1));
93  
94 $prev_trend = $trend;
95 }
96 $prev_hlc = $hlc;
97 $prev_dm = $dm;
98  
99 return $force;
100 };
101 }
102  
103 sub warmup_count_for_position {
104 my ($self, $lo) = @_;
105 if (DEBUG) {
106 print "KVOforce warmup_count_for_position $lo, parent=$self->{'parent'}\n"; }
107  
108 my $parent = $self->{'parent'};
109 my $p = $parent->values_array;
110 my $ph = $parent->array('highs') || $p;
111 my $pl = $parent->array('lows') || $p;
112  
113 my $p_lo = $lo + 1; # fill initially
114 my $chunk = 16;
115  
116 my $i = $lo;
117  
118 # looking back for a point where $trend changes
119 my ($after_hlc, $after_trend);
120 for ( ; $i >= 0; $i--) {
121 if ($i < $p_lo) {
122 my $new_p_lo = min ($i, $p_lo - $chunk);
123 $chunk *= 2;
124 $parent->fill ($new_p_lo, $i);
125 $p_lo = $new_p_lo;
126 }
127 my $value = $p->[$i] // next;
128 my $hlc = ($ph->[$i]//$value) + ($pl->[$i]//$value) + $value;
129 if (defined $after_hlc) {
130 my $trend = ($after_hlc > $hlc ? 1 : -1);
131 if (defined $after_trend && $trend != $after_trend) {
132 last;
133 }
134 $after_trend = $trend;
135 }
136 $after_hlc = $hlc;
137 }
138  
139 # then one further close for the $prev_dm for $cm=$dm+$prev_dm
140 $lo = $parent->find_before ($lo, 1);
141  
142 if (DEBUG) { print "KVOforce warmup_count_for_position for $lo==@{[$self->timebase->to_iso($lo)]} is @{[$lo-$i]}\n"; }
143 return $lo - $i;
144 }
145  
146 1;
147 __END__