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

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 101 CC 3 SCAR 13.3
CC
3
Cov
0%
proc
12.0
longname
2.0
shortname
2.0
CRAP 3.8
1 # Copyright 2007, 2009, 2010 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::TSI;
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 # "The True Strength Index", William Blau, TASC Nov 1991.
30 #
31 # http://store.traders.com/-v10-c05-trading-pdf.html
32 # "Trading With the True Strength Index", William Blau, TASC V.10:5
33 # May 1992 by William Blau. Intro, rest for sale.
34 #
35 # http://store.traders.com/-v11-c01-sidetru-pdf.html
36 # Sidebar included of "Stochastic Momentum" article January 1993.
37 # Formula not shown, but 1-day momentum described.
38 #
39 # http://www.traders.com/Documentation/FEEDbk_docs/Archive/112005/Letters/Letters.html
40 # TASC letters Nov 2005, Perry Kaufman giving formula.
41 #
42 # http://www.linnsoft.com/tour/techind/tsi.htm
43 # Formula, parameters 9 and 3, sample intraday AOL.
44 #
45 # http://www.meta-formula.com/Metastock-Formulas-T.html
46 # Formula.
47 #
48 # http://trader.online.pl/MSZ/e-w-True_Strength_Index.html
49 # Formula with ROC percentage and programmable period.
50 # http://trader.online.pl/MSZ/e-w-True_Strength_Index_II.html
51 # Formula, plain mom, periods 25 and 13.
52 # http://trader.online.pl/MSZ/e-w-True_Strength_Index_III.html
53 # Formula, plain momentum, periods 15 and 5.
54 # http://trader.online.pl/MSZ/e-w-True_Strength_Index_IV.html
55 # Formula, plain momentum, periods 25 and 13.
56 # http://trader.online.pl/MSZ/e-w-True_Strength_Index_V.html
57 # Formula, showing EMA periods 40 and 20, sample on WIG20.
58 #
59 # http://www.fmlabs.com/reference/default.htm?url=TSI.htm
60 # Formula.
61 #
62 # http://www.prophet.net/analyze/popglossary.jsp?studyid=TSI
63 # Formula, sample on Maxtor Corp (symbol MXO, taken over by Seagate, no
64 # data in Yahoo).
65 #
66  
67  
68 sub longname { __('TSI - True Strength Index') }
69 sub shortname { __('TSI') }
70 sub manual { __p('manual-node','True Strength Index') }
71  
72 use constant
73 { hlines => [ -25, 25 ],
74 type => 'indicator',
75 units => 'percentage_plus_or_minus_100',
76 minimum => -100,
77 maximum => 100,
78 parameter_info => [ { name => __('EMA-1 Days'),
79 key => 'tsi_ema1_days',
80 type => 'float',
81 minimum => 1,
82 default => 25,
83 decimals => 0,
84 step => 1 },
85 { name => __('EMA-2 Days'),
86 key => 'tsi_ema2_days',
87 type => 'float',
88 minimum => 1,
89 default => 13,
90 decimals => 0,
91 step => 1 }],
92 };
93  
94 sub new {
95 my ($class, $parent, $N1, $N2) = @_;
96  
97 $N1 //= parameter_info()->[0]->{'default'};
98 ($N1 > 0) || croak "TSI bad N1: $N1";
99  
100 $N2 //= parameter_info()->[1]->{'default'};
101 ($N2 > 0) || croak "TSI bad N2: $N2";
102  
103 return $class->SUPER::new
104 (parent => $parent,
105 parameters => [ $N1, $N2 ],
106 arrays => { values => [] },
107 array_aliases => { });
108 }
109 sub proc {
110 my ($class_or_self, $N1, $N2) = @_;
111  
112 my $num1_proc = App::Chart::Series::Derived::EMA->proc ($N1);
113 my $num2_proc = App::Chart::Series::Derived::EMA->proc ($N2);
114 my $den1_proc = App::Chart::Series::Derived::EMA->proc ($N1);
115 my $den2_proc = App::Chart::Series::Derived::EMA->proc ($N2);
116  
117 my $prev;
118 return sub {
119 my ($value) = @_;
120 my $tsi;
121 if (defined $prev) {
122 my $diff = $value - $prev;
123 my $num1 = $num1_proc->($diff);
124 my $den1 = $den1_proc->(abs($diff));
125  
126 my $num2 = $num2_proc->($num1);
127 my $den2 = $den2_proc->($den1);
128  
129 $tsi = ($den2 == 0 ? 50 : 100 * $num2 / $den2);
130 }
131 $prev = $value;
132 return $tsi;
133 };
134 }
135  
136 # One before the momentum difference comes out, then warmup for the ema1
137 # to get it coming out, then likewise for the ema2.
138 #
139 # FIXME: This is an overestimate for the EMAs.
140 #
141 sub warmup_count {
142 my ($class_or_self, $N1, $N2) = @_;
143 return (1
144 + App::Chart::Series::Derived::EMA->warmup_count($N1)
145 + App::Chart::Series::Derived::EMA->warmup_count($N2));
146 }
147  
148 1;
149 __END__