| 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::TDREI; | ||
| 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::Calculation; | ||
| 27 | use App::Chart::Series::Derived::WilliamsR; | ||
| 28 | |||
| 29 | |||
| 30 | # http://ta-glossary.netfirms.com/taglossary-R.htm | ||
| 31 | # Description quoting Tom DeMark on the calculation, sample BDY from | ||
| 32 | # 2004. | ||
| 33 | # | ||
| 34 | # http://www.linnsoft.com/tour/techind/tdrei.htm | ||
| 35 | # Formula, shows both num and den terms both zeroed by condition. | ||
| 36 | # Sample on AOL call options. | ||
| 37 | # | ||
| 38 | # http://www.prophet.net/analyze/popglossary.jsp?studyid=TD_REI | ||
| 39 | # Description, sample of Sunoco Inc (NYSE symbol SUN), year not shown | ||
| 40 | # but is Jan/Feb 2003. Note 6-day sums for the sample, not 5. | ||
| 41 | # | ||
| 42 | # http://trader.online.pl/MSZ/e-w-DeMark_Range_Expansion_Index.html | ||
| 43 | # Formula (parens in denominator look misplaced though). TD3 and TD4 | ||
| 44 | # work as an "or" of those two conditions. | ||
| 45 | # http://trader.online.pl/MSZ/e-w-DeMark_Range_Expansion_Index_II.html | ||
| 46 | # Formula, similar but using momentum func, sample on WIG20. | ||
| 47 | # | ||
| 48 | # http://secure.aspenres.com/Documents/help/userguide/help/dmrkhelp/demarkRange_Expansion_Index.html | ||
| 49 | # Description, sample of CME S&P 500 futures SPU05.CME. | ||
| 50 | # | ||
| 51 | # http://www.traders.com/documentation/feedbk_docs/archive/0897/Abstracts_new/DeMark/DEMARK9708.html | ||
| 52 | # http://store.traders.com/-v15-c08-thetdra-pdf.html | ||
| 53 | # Intro to Tom DeMark "The TD Range Expansion Index (TD REI)" TASC 15:8 | ||
| 54 | # August 1997 (rest for sale), revising original from his book "The New | ||
| 55 | # Science of Technical Analysis". | ||
| 56 | # | ||
| 57 | # http://www.meta-formula.com/Metastock-Formulas-T.html | ||
| 58 | # Formula. | ||
| 59 | # | ||
| 60 | # http://www.amibroker.com/library/formula.php?id=15 | ||
| 61 | # Formula, showing only numerator suppressed by condition. | ||
| 62 | # | ||
| 63 | # http://www.aiqsystems.com/PriceandVolumeBasedStrategies1.htm#down17 | ||
| 64 | # Code. | ||
| 65 | # http://www.aiqsystems.com/TDREI.gif | ||
| 66 | # Sample of Cabot Corp (NYSE symbol CBT) from Jan to Jun 2002. Period | ||
| 67 | # not shown, looks like 5 days. | ||
| 68 | # | ||
| 69 | |||
| 70 | sub longname { __('TD REI - Range Expansion Index') } | ||
| 71 | sub shortname { __('TDREI') } | ||
| 72 | sub manual { __p('manual-node','TD Range Expansion Index') } | ||
| 73 | |||
| 74 | use constant | ||
| 75 | { hlines => [ -40, 45 ], | ||
| 76 | type => 'indicator', | ||
| 77 | units => 'percentage_plus_or_minus_100', | ||
| 78 | minimum => -100, | ||
| 79 | maximum => 100, | ||
| 80 | parameter_info => [ { name => __('Stddev Days'), | ||
| 81 | key => 'tdrei_days', | ||
| 82 | type => 'integer', | ||
| 83 | minimum => 1, | ||
| 84 | default => 5 }], | ||
| 85 | }; | ||
| 86 | |||
| 87 | sub new { | ||
| 88 | my ($class, $parent, $N) = @_; | ||
| 89 | |||
| 90 | $N //= parameter_info()->[0]->{'default'}; | ||
| 91 | ($N > 0) || croak "TDREI bad N: $N"; | ||
| 92 | |||
| 93 | return $class->SUPER::new | ||
| 94 | (parent => $parent, | ||
| 95 | parameters => [ $N ], | ||
| 96 | arrays => { values => [] }, | ||
| 97 | array_aliases => { }); | ||
| 98 | } | ||
| 99 | *fill_part = \&App::Chart::Series::Derived::WilliamsR::fill_part; | ||
| 100 | |||
| 101 | # the first value goes into the sum at the 9th call, it's the first of | ||
| 102 | # $N-1 wanted for the sums, hence 8+$N-1 | ||
| 103 | sub warmup_count { | ||
| 104 | my ($class_or_self, $N) = @_; | ||
| 105 | return $N + 7; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Return a procedure which calculates a relative volatility index, using | ||
| 109 | # Dorsey's original 1993 definition, over an accumulated window. | ||
| 110 | # | ||
| 111 | sub proc { | ||
| 112 | my ($class_or_self, $N) = @_; | ||
| 113 | my $sumval_proc = App::Chart::Series::Calculation->sum($N); | ||
| 114 | my $sumabs_proc = App::Chart::Series::Calculation->sum($N); | ||
| 115 | my (@C, @H, @L); | ||
| 116 | |||
| 117 | return sub { | ||
| 118 | my ($high, $low, $close) = @_; | ||
| 119 | $high //= $close; | ||
| 120 | $low //= $close; | ||
| 121 | |||
| 122 | unshift @C, $close; | ||
| 123 | unshift @H, $high; | ||
| 124 | unshift @L, $low; | ||
| 125 | if (@C > 9) { | ||
| 126 | pop @C; | ||
| 127 | pop @H; | ||
| 128 | pop @L; | ||
| 129 | } | ||
| 130 | |||
| 131 | my $tdrei; | ||
| 132 | if (@C >= 9) { | ||
| 133 | my $h2 = $H[2]; | ||
| 134 | my $l2 = $L[2]; | ||
| 135 | |||
| 136 | my $use = (($h2 >= $C[7] | ||
| 137 | || $h2 >= $C[8] | ||
| 138 | || $high >= $L[5] | ||
| 139 | || $high >= $L[6]) | ||
| 140 | && ($l2 <= $C[7] | ||
| 141 | || $l2 <= $C[8] | ||
| 142 | || $low <= $H[5] | ||
| 143 | || $low <= $H[6])); | ||
| 144 | my $val = ($use ? ($high - $h2) + ($low - $l2) : 0); | ||
| 145 | my $absval = abs($high-$h2) + abs($low-$l2); | ||
| 146 | |||
| 147 | my $sumval = $sumval_proc->($val); | ||
| 148 | my $sumabs = $sumabs_proc->($absval); | ||
| 149 | $tdrei = ($sumabs == 0 ? 0 : 100 * $sumval / $sumabs); | ||
| 150 | } | ||
| 151 | return $tdrei; | ||
| 152 | }; | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | __END__ |