App/Chart/Series/TA.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 233 CC 8 SCAR 17.6
CC
8
Cov
0%
fill_part
12.0
input_paramName_to_arrays
12.0
manual
6.0
CRAP 5.8
1 # Copyright 2008, 2009, 2010, 2011, 2016 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::TA;
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';
26  
27 use Finance::TA;
28  
29 # uncomment this to run the ### lines
30 #use Devel::Comments;
31  
32 sub manual {
33 my ($series) = @_;
34 if (! $series) { return; }
35 my $fi = $series->{'fi'};
36 return "http://tadoc.org/indicator/$fi->{'name'}.htm";
37 }
38  
39 sub parameter_info {
40 my ($series) = @_;
41 my $fi = $series->{'fi'};
42 return [ map {
43 my $i = $_;
44 my ($retcode, $info);
45 ($retcode = TA_GetOptInputParameterInfo ($fi, $i, \$info)) == $TA_SUCCESS
46 or croak "Oops, cannot TA_GetOptInputParameterInfo $i: ",_retcode_str($retcode);
47  
48 { name => $info->{'displayName'},
49 key => $info->{'paramName'},
50 # type => 'integer',
51 # minimum => 1
52 }
53 } (0 .. $fi->{'nbOptInput'}-1)
54 ];
55 }
56  
57 sub new {
58 my ($class, $type, $parent, @args) = @_;
59 ### TA new() ...
60 ### $type
61 ### parent: "$parent"
62  
63 my ($retcode, $fh, $fi);
64 ($retcode = TA_GetFuncHandle ($type, \$fh)) == $TA_SUCCESS
65 or croak "Cannot get TA function '$type': ",_retcode_str($retcode);
66 ($retcode = TA_GetFuncInfo ($fh, \$fi)) == $TA_SUCCESS
67 or croak "Cannot TA_GetFuncInfo for '$type': ",_retcode_str($retcode);
68 ### FuncInfo hint: $fi->{'hint'}
69  
70 my $self = $class->SUPER::new (parent => $parent,
71 fi => $fi,
72 args => \@args);
73  
74 my $funcname = 'TA_' . $fi->{'name'};
75 $self->{'func'} = Finance::TA->can($funcname)
76 || croak "Oops, '$funcname' not found";
77  
78 {
79 my $lookbackname = "${funcname}_Lookback";
80 my $lookbackfunc = Finance::TA->can($lookbackname)
81 || croak "Oops, function $lookbackname not found";
82 my @lookbackargs = map {
83 my $i = $_;
84 $args[$i] // do {
85 my $info;
86 ($retcode = TA_GetOptInputParameterInfo ($fh, $i, \$info)) == $TA_SUCCESS
87 or croak "Oops, cannot TA_GetOptInputParameterInfo '$type' $i: ",_retcode_str($retcode);
88 $info->{'defaultValue'}
89 } } (0 .. $fi->{'nbOptInput'}-1);
90 $self->{'parameters'} = \@lookbackargs;
91 ### @lookbackargs
92 my $lookback = $self->{'lookback'} = $lookbackfunc->(@lookbackargs);
93 ### $lookback
94 }
95  
96 {
97 my $info;
98 ($retcode = TA_GetInputParameterInfo ($fh, 0, \$info)) == $TA_SUCCESS
99 or croak "Oops, cannot TA_GetInputParameterInfo '$type' 0: ",_retcode_str($retcode);
100 my $inname = $info->{'paramName'};
101 ### input paramName: $inname
102 my @inarrays = input_paramName_to_arrays($inname);
103 ### @inarrays
104 $self->{'inarrays'} = [ map {$parent->array($_)} @inarrays ];
105 }
106  
107 {
108 my $nbOutput = $fi->{'nbOutput'};
109 ### $nbOutput
110 my @outarrays;
111 $self->{'outarrays'} = \@outarrays;
112 my $arrays = $self->{'arrays'} = {};
113 my $first_outname;
114 foreach my $i (0 .. $nbOutput-1) {
115 my $info;
116 ($retcode = TA_GetOutputParameterInfo ($fh, $i, \$info)) == $TA_SUCCESS
117 or croak "Oops, cannot TA_GetOutputParameterInfo '$type' $i: ",_retcode_str($retcode);
118 my $outname = $info->{'paramName'};
119 ### $outname
120 $arrays->{$outname} = $outarrays[$i] = [];
121 $first_outname //= $outname;
122 }
123 if (exists $arrays->{'outRealMiddleBand'}) {
124 # hack to prefer middle line of TA_BBANDS
125 $first_outname = 'outRealMiddleBand';
126 }
127 $self->{'array_aliases'} = { values => $first_outname };
128 }
129  
130 return $self;
131 }
132  
133 sub name {
134 my ($self) = @_;
135 my $name = $self->{'fi'}->{'hint'};
136  
137 my $parameters = join (__p('separator',',') . ' ',
138 @{$self->{'parameters'}});
139 $name = join (' ', $name, $parameters);
140  
141 my $parent_name = $self->parent->name;
142 if (defined $parent_name) {
143 $name = join (' - ', $parent_name, $name);
144 }
145 return $name;
146 }
147  
148 sub fill_part {
149 my ($self, $lo, $hi) = @_;
150 my $parent = $self->{'parent'};
151 my $start = $parent->find_before ($lo, $self->{'lookback'});
152 $parent->fill ($start, $hi);
153  
154 my $inarrays = $self->{'inarrays'};
155 my $values = $parent->values_array;
156 my $inpart_end = $hi - $start;
157 my @inparts = map { my @x; $#x = $inpart_end; \@x } @$inarrays;
158 my $inend = $#$inarrays;
159 my @inmap; $#inmap = $inpart_end;
160 my $inmap_lo_pos;
161 my $upto = 0;
162 foreach my $i ($start .. $hi) {
163 $values->[$i] // next;
164 foreach my $j (0 .. $inend) {
165 $inparts[$j]->[$upto] = $inarrays->[$j]->[$i] // $values->[$i];
166 }
167 $inmap[$upto] = $i;
168 if ($i >= $lo) { $inmap_lo_pos //= $upto; }
169 $upto++;
170 }
171 $#inmap = $upto-1;
172  
173 # Crib: as of TA-Lib Jun11 start_idx/end_idx aren't range checked against
174 # the actual array size, so bad values like -1 give segvs.
175 #
176 if (! defined $inmap_lo_pos) {
177 ### no input values, nothing to fill ...
178 return;
179 }
180  
181 my $func = $self->{'func'};
182 ### $func
183 ### start_idx inmap_lo_pos: $inmap_lo_pos
184 ### end_idx $#inmap: $#inmap
185 ### @inmap
186 ### @inparts
187 ### args: $self->{'args'}
188  
189 my ($retcode, $out_lo, @gotarrays)
190 = &$func ($inmap_lo_pos, # start_idx
191 $#inmap, # end_idx
192 @inparts, # in_array
193 @{$self->{'args'}});
194 ### $retcode
195 ### $out_lo
196 $retcode == $TA_SUCCESS
197 or croak "TA_$self->{'fi'}->{'name'} error ",_retcode_str($retcode);
198 ### @gotarrays
199  
200 my $outarrays = $self->{'outarrays'};
201 my $outend = $#$outarrays;
202  
203 splice @inmap, 0,$out_lo;
204 $inmap_lo_pos -= $out_lo;
205 ### gotarray for: $inmap[0]
206 ### cf lo: $lo
207 ### adjusted lopos: $inmap_lo_pos
208  
209 $inmap_lo_pos = max (0, $inmap_lo_pos);
210  
211 my @src = ($inmap_lo_pos .. $#inmap);
212 my @dst = @inmap[@src];
213 foreach my $j (0 .. $outend) {
214 @{$outarrays->[$j]}[@dst] = @{$gotarrays[$j]}[@src];
215 }
216 }
217  
218 sub _retcode_str {
219 my ($retcode) = @_;
220 my $rci = Finance::TA::TA_RetCodeInfo->new ($retcode);
221 return "[$rci->{'enumStr'}] $rci->{'infoStr'}";
222 }
223  
224 my %inchar_to_array = ('O' => 'opens',
225 'H' => 'highs',
226 'L' => 'lows',
227 'C' => 'closes',
228 'V' => 'volumes');
229  
230 # $inname like 'inReal' or 'inPriceHLCV' or 'inPriceHLC', coming from
231 # various TA_InputParameterInfo in ta_def_ui.c
232 #
233 sub input_paramName_to_arrays {
234 my ($inname) = @_;
235 if ($inname eq 'inReal') { return 'values'; }
236 if ($inname =~ /^inPrice([OHLCV]+)$/) {
237 my $parts = $1;
238 return (map {$inchar_to_array{$_}
239 || die "Unrecognised input paramName '$inname'"}
240 split //, $parts); # split into individual characters
241 }
242 }
243  
244 1;
245 __END__