| 1 | package Finance::GeniusTrader::Indicators::SMI; | ||
| 2 | |||
| 3 | # Copyright 2008 Thomas Weigert | ||
| 4 | # Based on and for GeniusTrader (C) 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 5 | # This file is distributed under the terms of the General Public License | ||
| 6 | # version 2 or (at your option) any later version. | ||
| 7 | |||
| 8 | # Standards-Version: 1.0 | ||
| 9 | |||
| 10 | # $Id: SMI.pm,v 1.10 2008/03/08 20:16:16 ras Exp ras $ | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 14 | |||
| 15 | use Finance::GeniusTrader::Indicators; | ||
| 16 | use Finance::GeniusTrader::Indicators::EMA; | ||
| 17 | use Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 18 | use Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 19 | use Finance::GeniusTrader::Indicators::Generic::Container; | ||
| 20 | use Finance::GeniusTrader::Prices; | ||
| 21 | |||
| 22 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 23 | @NAMES = ("\%K[#1,#2,#3]","\%D[#1,#2,#3,#4]"); | ||
| 24 | @DEFAULT_ARGS = (5, 3, 3, 3, "{I:Prices HIGH}", "{I:Prices LOW}", "{I:Prices CLOSE}" ); | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =head1 Finance::GeniusTrader::Indicators::SMI | ||
| 29 | |||
| 30 | =head2 Overview | ||
| 31 | |||
| 32 | The Stochastic Momentum Index (SMI) is based on the Stochastic | ||
| 33 | Oscillator. The difference is that the Stochastic Oscillator | ||
| 34 | calculates where the close is relative to the high/low range, while | ||
| 35 | the SMI calculates where the close is relative to the midpoint of the | ||
| 36 | high/low range. The values of the SMI range from +100 to -100. When | ||
| 37 | the close is greater than the midpoint, the SMI is above zero, when | ||
| 38 | the close is less than than the midpoint, the SMI is below zero. | ||
| 39 | |||
| 40 | The SMI is interpreted the same way as the Stochastic | ||
| 41 | Oscillator. Extreme high/low SMI values indicate overbought/oversold | ||
| 42 | conditions. A buy signal is generated when the SMI rises above -50, or | ||
| 43 | when it crosses above the signal line. A sell signal is generated when | ||
| 44 | the SMI falls below +50, or when it crosses below the signal | ||
| 45 | line. Also look for divergence with the price to signal the end of a | ||
| 46 | trend or indicate a false trend. | ||
| 47 | |||
| 48 | The Stochastic Momentum Index was developed by William Blau and was | ||
| 49 | introduced in his article in the January, 1993 issue of Technical | ||
| 50 | Analysis of Stocks & Commodities magazine. | ||
| 51 | |||
| 52 | =head2 Calculation | ||
| 53 | |||
| 54 | CM = Close - ( Highest high(n) + Lowest low(n) ) / 2 | ||
| 55 | CM' = EMA(EMA(CM, A), B) | ||
| 56 | HL = Highest high(n) - Lowest low(n) | ||
| 57 | HL' = EMA(EMA(HL, A), B) | ||
| 58 | |||
| 59 | %K = 100 * CM' / (HL' / 2) | ||
| 60 | %D = SMA(%K) | ||
| 61 | |||
| 62 | =head2 Restrictions | ||
| 63 | |||
| 64 | This indicator requires that the first four parameters are constant | ||
| 65 | values and will abort otherwise. | ||
| 66 | |||
| 67 | |||
| 68 | =head2 Examples | ||
| 69 | |||
| 70 | Finance::GeniusTrader::Indicators::SMI->new() | ||
| 71 | Finance::GeniusTrader::Indicators::SMI->new([14, 3, 3, 3]) | ||
| 72 | |||
| 73 | =head2 Links | ||
| 74 | |||
| 75 | http://www.fmlabs.com/reference/default.htm?url=SMI.htm | ||
| 76 | http://trader.online.pl/MSZ/e-w-Stochastic_Momentum_Indicator.html | ||
| 77 | (note that the former incorrectly uses "-" in CM). | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub initialize { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | for (my $i=1;$i<=4;$i++) { | ||
| 87 | die "Argument $i must be a constant value.\n" unless $self->{'args'}->is_constant($i); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Define a container for CM and HL | ||
| 91 | $self->{'cm'} = Finance::GeniusTrader::Indicators::Generic::Container->new(['CM']); | ||
| 92 | $self->{'hl'} = Finance::GeniusTrader::Indicators::Generic::Container->new(['HL']); | ||
| 93 | |||
| 94 | |||
| 95 | # We need to call MIN and MAX first | ||
| 96 | $self->{'min'} = Finance::GeniusTrader::Indicators::Generic::MinInPeriod->new([ $self->{'args'}->get_arg_names(1), | ||
| 97 | $self->{'args'}->get_arg_names(6) ]); | ||
| 98 | $self->{'max'} = Finance::GeniusTrader::Indicators::Generic::MaxInPeriod->new([ $self->{'args'}->get_arg_names(1), | ||
| 99 | $self->{'args'}->get_arg_names(5) ]); | ||
| 100 | |||
| 101 | # Initialize smoothing of CM | ||
| 102 | $self->{'smooth_cm'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(3), "{I:EMA " . $self->{'args'}->get_arg_names(2) . "{I:Generic:Container CM }}" ]); | ||
| 103 | |||
| 104 | # Initialize smoothing of HL | ||
| 105 | $self->{'smooth_hl'} = Finance::GeniusTrader::Indicators::EMA->new([ $self->{'args'}->get_arg_names(3), "{I:EMA " . $self->{'args'}->get_arg_names(2) . "{I:Generic:Container HL }}" ]); | ||
| 106 | |||
| 107 | # Initialize smoothing of %K | ||
| 108 | $self->{'%d'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(4), "{I:Generic:ByName " . $self->get_name(0) . "}" ]); | ||
| 109 | |||
| 110 | # Smoothing functions are args 2 and 3 | ||
| 111 | my $nb_days = $self->{'args'}->get_arg_names(2) + $self->{'args'}->get_arg_names(3) + $self->{'args'}->get_arg_names(4); | ||
| 112 | |||
| 113 | $self->add_indicator_dependency($self->{'min'}, $nb_days); | ||
| 114 | $self->add_indicator_dependency($self->{'max'}, $nb_days); | ||
| 115 | $self->add_arg_dependency(7, $nb_days + $self->{'args'}->get_arg_constant(1)); | ||
| 116 | |||
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | =pod | ||
| 121 | |||
| 122 | =head2 Finance::GeniusTrader::Indicators::SMI::calculate($calc, $day) | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub calculate { | ||
| 127 | my ($self, $calc, $i) = @_; | ||
| 128 | my $indic = $calc->indicators; | ||
| 129 | my $prices = $calc->prices; | ||
| 130 | my $min_name = $self->{'min'}->get_name; | ||
| 131 | my $max_name = $self->{'max'}->get_name; | ||
| 132 | my $k_name = $self->get_name(0); | ||
| 133 | my $d_name = $self->get_name(1); | ||
| 134 | my $cm_name = $self->{'cm'}->get_name; | ||
| 135 | my $hl_name = $self->{'hl'}->get_name; | ||
| 136 | |||
| 137 | return if ($indic->is_available($cm_name, $i) && | ||
| 138 | $indic->is_available($hl_name, $i) && | ||
| 139 | $indic->is_available($k_name, $i)); | ||
| 140 | |||
| 141 | |||
| 142 | # Smoothing functions are args 2 and 3 | ||
| 143 | my $nb_days = $self->{'args'}->get_arg_values($calc, $i, 2) + $self->{'args'}->get_arg_values($calc, $i, 3) + $self->{'args'}->get_arg_values($calc, $i, 4); | ||
| 144 | |||
| 145 | return if (! $self->check_dependencies($calc, $i)); | ||
| 146 | |||
| 147 | # Calculate CM | ||
| 148 | for (my $n = 0; $n < $nb_days; $n++) { | ||
| 149 | |||
| 150 | # Return if CM is available | ||
| 151 | next if $indic->is_available($cm_name, $i - $n); | ||
| 152 | |||
| 153 | # Get MIN and MAX | ||
| 154 | my $lowest_low = $indic->get($min_name, $i - $n); | ||
| 155 | my $highest_high = $indic->get($max_name, $i - $n); | ||
| 156 | |||
| 157 | # Calculate CM | ||
| 158 | my $cm_value = $self->{'args'}->get_arg_values($calc, $i - $n, 7) - (($highest_high + $lowest_low) / 2 ); | ||
| 159 | |||
| 160 | # Return the results | ||
| 161 | $indic->set($cm_name, $i - $n, $cm_value); | ||
| 162 | |||
| 163 | # Calculate HL | ||
| 164 | my $hl_value = $highest_high - $lowest_low; | ||
| 165 | |||
| 166 | # Return the results | ||
| 167 | $indic->set($hl_name, $i - $n, $hl_value); | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | my $k_first = $i - $self->{'args'}->get_arg_values($calc, $i, 4) + 1; | ||
| 172 | |||
| 173 | $self->{'smooth_cm'}->calculate_interval($calc, $k_first, $i); | ||
| 174 | $self->{'smooth_hl'}->calculate_interval($calc, $k_first, $i); | ||
| 175 | |||
| 176 | for (my $n=$k_first; $n<=$i; $n++) { | ||
| 177 | my $s2 = $indic->get($self->{'smooth_cm'}->get_name, $n); | ||
| 178 | my $h2 = $indic->get($self->{'smooth_hl'}->get_name, $n); | ||
| 179 | my $k_value = 100 * $s2 / ( $h2 / 2 ); | ||
| 180 | $indic->set($k_name, $n, $k_value); | ||
| 181 | } | ||
| 182 | |||
| 183 | $self->{'%d'}->calculate($calc, $i); | ||
| 184 | $indic->set($d_name, $i, $indic->get($self->{'%d'}->get_name, $i)); | ||
| 185 | |||
| 186 | } | ||
| 187 | |||
| 188 | sub calculate_interval { | ||
| 189 | my ($self, $calc, $first, $last) = @_; | ||
| 190 | my $indic = $calc->indicators; | ||
| 191 | my $prices = $calc->prices; | ||
| 192 | my $min_name = $self->{'min'}->get_name; | ||
| 193 | my $max_name = $self->{'max'}->get_name; | ||
| 194 | my $k_name = $self->get_name(0); | ||
| 195 | my $d_name = $self->get_name(1); | ||
| 196 | my $cm_name = $self->{'cm'}->get_name; | ||
| 197 | my $hl_name = $self->{'hl'}->get_name; | ||
| 198 | |||
| 199 | return if ($indic->is_available_interval($cm_name, $first, $last) && | ||
| 200 | $indic->is_available_interval($hl_name, $first, $last) && | ||
| 201 | $indic->is_available_interval($k_name, $first, $last)); | ||
| 202 | |||
| 203 | # Smoothing functions are args 2 and 3 | ||
| 204 | my $nb_days = $self->{'args'}->get_arg_constant(2) + $self->{'args'}->get_arg_constant(3) + $self->{'args'}->get_arg_names(4); | ||
| 205 | |||
| 206 | while (! $self->check_dependencies_interval($calc, $first, $last)) { | ||
| 207 | return if $first == $last; | ||
| 208 | $first++; | ||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | for (my $i=$first-$nb_days+1;$i<=$last;$i++) { | ||
| 213 | |||
| 214 | # Return if CM is available | ||
| 215 | next if $indic->is_available($cm_name, $i); | ||
| 216 | |||
| 217 | # Get MIN and MAX | ||
| 218 | my $lowest_low = $indic->get($min_name, $i); | ||
| 219 | my $highest_high = $indic->get($max_name, $i); | ||
| 220 | |||
| 221 | # Calculate CM | ||
| 222 | my $cm_value = $self->{'args'}->get_arg_values($calc, $i, 7) - (($highest_high + $lowest_low) / 2 ); | ||
| 223 | |||
| 224 | # Return the results | ||
| 225 | $indic->set($cm_name, $i, $cm_value); | ||
| 226 | |||
| 227 | # Calculate HL | ||
| 228 | my $hl_value = $highest_high - $lowest_low; | ||
| 229 | |||
| 230 | # Return the results | ||
| 231 | $indic->set($hl_name, $i, $hl_value); | ||
| 232 | |||
| 233 | } | ||
| 234 | |||
| 235 | my $k_first = $first - $self->{'args'}->get_arg_names(4) + 1; | ||
| 236 | |||
| 237 | # Calculate smoothing | ||
| 238 | $self->{'smooth_cm'}->calculate_interval($calc, $k_first, $last); | ||
| 239 | $self->{'smooth_hl'}->calculate_interval($calc, $k_first, $last); | ||
| 240 | |||
| 241 | for (my $i=$k_first;$i<=$last;$i++) { | ||
| 242 | my $s2 = $indic->get($self->{'smooth_cm'}->get_name, $i); | ||
| 243 | my $h2 = $indic->get($self->{'smooth_hl'}->get_name, $i); | ||
| 244 | my $k_value = 100 * $s2 / ( $h2 / 2 ); | ||
| 245 | $indic->set($k_name, $i, $k_value); | ||
| 246 | } | ||
| 247 | |||
| 248 | $self->{'%d'}->calculate_interval($calc, $first, $last); | ||
| 249 | for (my $i=$first;$i<=$last;$i++) { | ||
| 250 | $indic->set($d_name, $i, $indic->get($self->{'%d'}->get_name, $i)); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | 1; |