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

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 245 CC 21 SCAR 39.3
CC
21
Cov
0%
fill_part
156.0
name
20.0
symbol_name
12.0
CRAP 50.7
1 # Copyright 2007, 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::Derived::Adjust;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use Locale::TextDomain ('App-Chart');
23  
24 use App::Chart::DBI;
25 use App::Chart::TZ;
26 use base 'App::Chart::Series::OHLCVI';
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 sub longname { __('Adjustments') }
32 sub shortname { __('Adj') }
33 sub manual { __p('manual-node','Dividends and Splits') }
34  
35 use constant { type => 'special',
36 parameter_info => [ { name => __('Splits'),
37 key => 'adj_splits',
38 type => 'boolean',
39 default => 1 },
40  
41 { name => __('Divs'),
42 key => 'adj_dividends',
43 type => 'boolean',
44 default => 1 },
45  
46 { name => __('Imp'),
47 key => 'adj_imputation',
48 type => 'boolean',
49 default => 1 },
50  
51 { name => __('Roll'),
52 key => 'adj_rollovers',
53 type => 'boolean',
54 default => 0 }],
55 };
56  
57 sub name {
58 my ($self) = @_;
59 return __x('{parent} - Adj {list}',
60 parent => $self->{'parent'}->name,
61 list =>
62 join (',',
63 ($self->{'adjust_splits'} ? __('Splits') : ()),
64 ($self->{'adjust_dividends'} ? __('Divs') : ()),
65 ($self->{'adjust_dividends'} && $self->{'adjust_imputation'}
66 ? __('Imp') : ())));
67 }
68 sub symbol_name {
69 my ($self) = @_;
70 if (my $parent = $self->{'parent'}) {
71 if (defined (my $symbol = $parent->{'symbol'})) {
72 return App::Chart::Database->symbol_name ($symbol);
73 }
74 }
75 return undef;
76 }
77  
78 sub derive {
79 my ($class, $parent, %options) = @_;
80  
81 my $adjust_splits = $options{'adjust_splits'};
82 my $adjust_dividends = $options{'adjust_dividends'};
83 if (! $adjust_splits && ! $adjust_dividends) { return $parent; }
84  
85 # Only go up to today (in the symbol's timezone) for adjustments. Stuff
86 # in the future shouldn't be applied until then. Decide this at init time
87 # in case we live long enough for the time to reach a new day.
88 #
89 my $symbol = $parent->{'symbol'};
90 my $timezone = App::Chart::TZ->for_symbol ($symbol);
91 my $hi_iso = $timezone->iso_date;
92  
93 my $timebase = $parent->timebase;
94 my $lo_iso = $timebase->to_iso (0);
95  
96 # if for each option it's either not wanted or there's no data for it,
97 # then can just return unmodified $parent
98 #
99 if ((! $adjust_splits
100 || ! App::Chart::DBI->read_single ('SELECT date FROM split
101 WHERE (symbol=? AND (date BETWEEN ? AND ?))
102 LIMIT 1', $symbol, $lo_iso, $hi_iso))
103 &&
104 (! $adjust_dividends
105 || ! App::Chart::DBI->read_single ('SELECT ex_date FROM dividend
106 WHERE (symbol=? AND (ex_date BETWEEN ? AND ?))
107 LIMIT 1', $symbol, $lo_iso, $hi_iso))) {
108 ### Adjust no splits or dividends to apply
109 return $parent;
110 }
111  
112 return $class->SUPER::new (parent => $parent,
113 adj_hi_iso => $hi_iso,
114 %options);
115 }
116  
117 sub fill_part {
118 my ($self, $lo, $hi) = @_;
119 ### Adjust fill_part() "$lo $hi"
120  
121 my $dbh = App::Chart::DBI->instance;
122 my $timebase = $self->timebase;
123 my $parent = $self->{'parent'};
124 my $symbol = $parent->{'symbol'};
125 my $lo_iso = $timebase->to_iso ($lo); # requested $lo
126 my $hi_iso = $self->{'adj_hi_iso'}; # all splits/divs to today
127  
128 my $splits = [];
129 if ($self->{'adjust_splits'}) {
130 my $sth = $dbh->prepare_cached
131 ('SELECT date, new, old
132 FROM split WHERE (symbol=? AND (date BETWEEN ? AND ?))
133 ORDER BY date DESC');
134 $splits = $dbh->selectall_arrayref ($sth, {Slice=>{}},
135 $symbol, $lo_iso, $hi_iso);
136 $sth->finish;
137 foreach my $row (@$splits) {
138 $row->{'date'} = $timebase->from_iso_floor ($row->{'date'});
139 }
140 }
141 push @$splits, { date => $lo-1 }; # sentinel
142 ### $splits
143  
144 my $dividends = [];
145 if ($self->{'adjust_dividends'}) {
146 my $sth = $dbh->prepare_cached
147 ('SELECT ex_date, amount, imputation
148 FROM dividend WHERE (symbol=? AND (ex_date BETWEEN ? AND ?))
149 ORDER BY ex_date DESC');
150 $dividends = $dbh->selectall_arrayref ($sth, {Slice=>{}},
151 $symbol, $lo_iso, $hi_iso);
152 $sth->finish;
153 foreach my $row (@$dividends) {
154 $row->{'ex_date'} = $timebase->from_iso_floor ($row->{'ex_date'});
155 }
156 }
157 push @$dividends, { ex_date => $lo-1 }; # sentinel
158 ### $dividends
159  
160 $hi = $parent->find_after($hi,1);
161  
162 $parent->fill ($lo, $hi);
163 my $p_opens = $parent->array('opens');
164 my $p_closes = $parent->array('closes');
165 my $p_highs = $parent->array('highs');
166 my $p_lows = $parent->array('lows');
167 my $p_volumes = $parent->array('volumes');
168 my $p_openints = $parent->array('openints');
169  
170 my $s_opens = $self->{'opens'};
171 my $s_closes = $self->{'closes'};
172 my $s_highs = $self->{'highs'};
173 my $s_lows = $self->{'lows'};
174 my $s_volumes = $self->{'volumes'};
175 my $s_openints = $self->{'openints'};
176  
177 my $factor = 1;
178 my $post_close;
179 for (my $t = $hi; $t >= $lo; $t--) {
180  
181 if ($p_opens->[$t]) { $s_opens->[$t] = $p_opens->[$t] * $factor; }
182 if ($p_highs->[$t]) { $s_highs->[$t] = $p_highs->[$t] * $factor; }
183 if ($p_lows->[$t]) { $s_lows->[$t] = $p_lows->[$t] * $factor; }
184 if ($p_closes->[$t]) { $post_close = $p_closes->[$t];
185 $s_closes->[$t] = $post_close * $factor;
186 }
187 if ($p_volumes->[$t]) { $s_volumes->[$t] = $p_volumes->[$t] / $factor;}
188 if ($p_openints->[$t]){ $s_openints->[$t] = $p_openints->[$t] / $factor;}
189  
190 for ( ; $splits->[0]->{'date'} >= $t; shift @$splits) {
191 $factor *= ($splits->[0]->{'old'} / $splits->[0]->{'new'});
192 ### factor now: $factor
193 }
194  
195 for ( ; $dividends->[0]->{'ex_date'} >= $t; shift @$dividends) {
196 my $div = $dividends->[0]->{'amount'};
197 Scalar::Util::looks_like_number ($div) or next;
198  
199 if ($self->{'adjust_imputation'}) {
200 my $imp = $dividends->[0]->{'imputation'};
201 if (Scalar::Util::looks_like_number ($imp)) { $div += $imp; }
202 }
203 if (defined $post_close) {
204 # factor chosen so if prev=post+div then prev*factor==post
205 $factor *= $post_close / ($post_close + $div);
206 ### dividend: $div
207 ### $post_close
208 ### factor now: $factor
209 }
210 }
211 }
212 }
213  
214 sub dividends {
215 my ($self) = @_;
216 return $self->{'parent'}->dividends;
217 }
218 sub splits {
219 my ($self) = @_;
220 return $self->{'parent'}->splits;
221 }
222 sub annotations {
223 my ($self) = @_;
224 return $self->{'parent'}->annotations;
225 }
226  
227 sub Alerts_arrayref {
228 my ($self) = @_;
229 my $parent = $self->{'parent'};
230 if (my $func = $parent->can('Alerts_arrayref')) {
231 return $parent->$func;
232 } else {
233 return [];
234 }
235 }
236  
237 # FIXME: Adjust endpoints per display
238 sub AnnLines_arrayref {
239 my ($self) = @_;
240 my $parent = $self->{'parent'};
241 if (my $func = $parent->can('AnnLines_arrayref')) {
242 return $parent->$func;
243 } else {
244 return [];
245 }
246 }
247  
248 1;
249 __END__