| 1 | package Finance::GeniusTrader::Indicators::CNDL; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Indicators; | ||
| 13 | use Finance::GeniusTrader::Indicators::BOL; | ||
| 14 | use Finance::GeniusTrader::Prices; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("CNDL[#*]"); | ||
| 18 | @DEFAULT_ARGS = (55, 0.5, "{I:Prices OPEN}", "{I:Prices HIGH}", | ||
| 19 | "{I:Prices LOW}", "{I:Prices CLOSE}"); | ||
| 20 | |||
| 21 | =pod | ||
| 22 | |||
| 23 | =head1 Finance::GeniusTrader::Indicators::CNDL | ||
| 24 | |||
| 25 | =head2 Overview | ||
| 26 | |||
| 27 | The CandelCode (CNDL) indicator is based on the article "Coding Candelsticks" published in Technical Analysis of Stocks and Commodities (November 1999) by Viktor Likhovidov. | ||
| 28 | |||
| 29 | =head2 Calculation | ||
| 30 | |||
| 31 | =head2 Parameters | ||
| 32 | |||
| 33 | Two parameters are used to initialize the Bollinger Bands necessary to calculate all required thresholds; the standard deviation number is set to 0.5 with a period of 55. | ||
| 34 | |||
| 35 | =head2 Examples | ||
| 36 | |||
| 37 | Finance::GeniusTrader::Indicators::CNDL->new() | ||
| 38 | Finance::GeniusTrader::Indicators::CNDL->new([55, 0.5]) | ||
| 39 | |||
| 40 | =head2 Appendix | ||
| 41 | |||
| 42 | If you need to find quickly the candel code of a specific pattern, here is a conversion table : | ||
| 43 | /GT/Docs/CandelsticksCodes | ||
| 44 | |||
| 45 | =head2 Links | ||
| 46 | |||
| 47 | http://www.traders.com/Documentation/FEEDbk_docs/Archive/012000/TradersTips/TradersTips.html | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | |||
| 52 | sub initialize { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | # Initialize Body Thresholds | ||
| 56 | |||
| 57 | my $evalbody = "{I:Generic:Eval abs(" . | ||
| 58 | $self->{'args'}->get_arg_names(3) . " - " . | ||
| 59 | $self->{'args'}->get_arg_names(6) . ")}"; | ||
| 60 | |||
| 61 | $self->{'body_thresholds'} = | ||
| 62 | Finance::GeniusTrader::Indicators::BOL->new([$self->{'args'}->get_arg_names(1), | ||
| 63 | $self->{'args'}->get_arg_names(2), | ||
| 64 | $evalbody | ||
| 65 | ]); | ||
| 66 | |||
| 67 | # Initialize Upper Shadow Thresholds | ||
| 68 | |||
| 69 | my $evalupper = "{I:Generic:Eval " . | ||
| 70 | $self->{'args'}->get_arg_names(6) . " >= " . | ||
| 71 | $self->{'args'}->get_arg_names(3) . " ? " . | ||
| 72 | $self->{'args'}->get_arg_names(4) . " - " . | ||
| 73 | $self->{'args'}->get_arg_names(6) . " : " . | ||
| 74 | $self->{'args'}->get_arg_names(4) . " - " . | ||
| 75 | $self->{'args'}->get_arg_names(3) . "}"; | ||
| 76 | |||
| 77 | $self->{'upper_shadow_thresholds'} = | ||
| 78 | Finance::GeniusTrader::Indicators::BOL->new([$self->{'args'}->get_arg_names(1), | ||
| 79 | $self->{'args'}->get_arg_names(2), | ||
| 80 | $evalupper | ||
| 81 | ]); | ||
| 82 | |||
| 83 | # Initialize Lower Shadow Thresholds | ||
| 84 | |||
| 85 | my $evallower = "{I:Generic:Eval " . | ||
| 86 | $self->{'args'}->get_arg_names(6) . " >= " . | ||
| 87 | $self->{'args'}->get_arg_names(3) . " ? " . | ||
| 88 | $self->{'args'}->get_arg_names(3) . " - " . | ||
| 89 | $self->{'args'}->get_arg_names(5) . " : " . | ||
| 90 | $self->{'args'}->get_arg_names(6) . " - " . | ||
| 91 | $self->{'args'}->get_arg_names(5) . "}"; | ||
| 92 | |||
| 93 | $self->{'lower_shadow_thresholds'} = | ||
| 94 | Finance::GeniusTrader::Indicators::BOL->new([$self->{'args'}->get_arg_names(1), | ||
| 95 | $self->{'args'}->get_arg_names(2), | ||
| 96 | $evallower | ||
| 97 | ]); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub calculate { | ||
| 101 | my ($self, $calc, $i) = @_; | ||
| 102 | my $indic = $calc->indicators; | ||
| 103 | my $period = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 104 | my $cndl_name = $self->get_name(0); | ||
| 105 | my $cndl_code = 0; | ||
| 106 | my $body_color = 0; | ||
| 107 | |||
| 108 | return if (! defined($period)); | ||
| 109 | |||
| 110 | $self->remove_volatile_dependencies(); | ||
| 111 | $self->add_volatile_indicator_dependency($self->{'body_thresholds'}, $period); | ||
| 112 | $self->add_volatile_indicator_dependency($self->{'upper_shadow_thresholds'}, $period); | ||
| 113 | $self->add_volatile_indicator_dependency($self->{'lower_shadow_thresholds'}, $period); | ||
| 114 | |||
| 115 | return if ($indic->is_available($cndl_name, $i)); | ||
| 116 | return if (! $self->check_dependencies($calc, $i)); | ||
| 117 | |||
| 118 | # Calculate the size of : Body, Lower and Upper Shadows | ||
| 119 | my $body = abs($self->{'args'}->get_arg_values($calc, $i, 3) - | ||
| 120 | $self->{'args'}->get_arg_values($calc, $i, 6)); | ||
| 121 | |||
| 122 | my $upper_shadow = ($self->{'args'}->get_arg_values($calc, $i, 6) >= | ||
| 123 | $self->{'args'}->get_arg_values($calc, $i, 3)) ? | ||
| 124 | ($self->{'args'}->get_arg_values($calc, $i, 4) - | ||
| 125 | $self->{'args'}->get_arg_values($calc, $i, 6)) : | ||
| 126 | ($self->{'args'}->get_arg_values($calc, $i, 4) - | ||
| 127 | $self->{'args'}->get_arg_values($calc, $i, 3)); | ||
| 128 | |||
| 129 | my $lower_shadow = ($self->{'args'}->get_arg_values($calc, $i, 6) >= | ||
| 130 | $self->{'args'}->get_arg_values($calc, $i, 3)) ? | ||
| 131 | ($self->{'args'}->get_arg_values($calc, $i, 3) - | ||
| 132 | $self->{'args'}->get_arg_values($calc, $i, 5)) : | ||
| 133 | ($self->{'args'}->get_arg_values($calc, $i, 6) - | ||
| 134 | $self->{'args'}->get_arg_values($calc, $i, 5)); | ||
| 135 | |||
| 136 | # Get all thresholds | ||
| 137 | my $body_upper_threshold = | ||
| 138 | $indic->get($self->{'body_thresholds'}->get_name(1), $i); | ||
| 139 | my $body_lower_threshold = | ||
| 140 | $indic->get($self->{'body_thresholds'}->get_name(2), $i); | ||
| 141 | my $upper_shadow_upper_threshold = | ||
| 142 | $indic->get($self->{'upper_shadow_thresholds'}->get_name(1), $i); | ||
| 143 | my $upper_shadow_lower_threshold = | ||
| 144 | $indic->get($self->{'upper_shadow_thresholds'}->get_name(2), $i); | ||
| 145 | my $lower_shadow_upper_threshold = | ||
| 146 | $indic->get($self->{'lower_shadow_thresholds'}->get_name(1), $i); | ||
| 147 | my $lower_shadow_lower_threshold = | ||
| 148 | $indic->get($self->{'lower_shadow_thresholds'}->get_name(2), $i); | ||
| 149 | |||
| 150 | # Determine the candelstick code with a sequence of seven binary digits | ||
| 151 | |||
| 152 | # Step 1 : Candelstick Color | ||
| 153 | # The first binary digit represents the color of the candelstick : one (1) for a white one, and zero (0) for a black one. | ||
| 154 | # A Doji candelstick - which as zero body size - is white if its upper shadow is longer than its lower shadow. | ||
| 155 | if ((($self->{'args'}->get_arg_values($calc, $i, 6) - | ||
| 156 | $self->{'args'}->get_arg_values($calc, $i, 3)) > 0 ) | ||
| 157 | or | ||
| 158 | (($body eq 0) and ($upper_shadow > $lower_shadow))) | ||
| 159 | { | ||
| 160 | $body_color = 1; | ||
| 161 | $cndl_code += (2 ** 6); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Step 2 : Candelstick Body | ||
| 165 | # The second position, composed of two binary digits, denotes body size, depending on the first digit, or color, of the body. | ||
| 166 | |||
| 167 | # Candelsticks White Black | ||
| 168 | # No body size (doji) 00 11 | ||
| 169 | # Small body 01 10 | ||
| 170 | # Middle body 10 01 | ||
| 171 | # Large body 11 00 | ||
| 172 | |||
| 173 | if ((($body eq 0) and ($upper_shadow <= $lower_shadow)) | ||
| 174 | or | ||
| 175 | (($body_color eq 1) and ($body >= $body_upper_threshold))) | ||
| 176 | { | ||
| 177 | $cndl_code += (2 ** 5 + 2 ** 4); | ||
| 178 | } | ||
| 179 | if ((($body_color eq 0) and ($body_lower_threshold <= $body) and | ||
| 180 | ($body < $body_upper_threshold)) | ||
| 181 | or | ||
| 182 | (($body_color eq 1 ) and ($body < $body_lower_threshold) and | ||
| 183 | ($body > 0))) | ||
| 184 | { | ||
| 185 | $cndl_code += (2 ** 4); | ||
| 186 | } | ||
| 187 | if ((($body_color eq 0) and ($body < $body_lower_threshold) and | ||
| 188 | ($body > 0)) | ||
| 189 | or | ||
| 190 | (($body_color eq 1) and ($body_lower_threshold <= $body) and | ||
| 191 | ($body < $body_upper_threshold))) | ||
| 192 | { | ||
| 193 | $cndl_code += (2 ** 5); | ||
| 194 | } | ||
| 195 | |||
| 196 | # Step 3 : Candelstick Shadows | ||
| 197 | # The fird and the fourth positions (each consisting of two binay digits) code the sizes of upper and lower shadows, repecitvely : | ||
| 198 | # | ||
| 199 | # Shadows Upper Lower | ||
| 200 | # None 00 11 | ||
| 201 | # Small 01 10 | ||
| 202 | # Middle 10 01 | ||
| 203 | # Large 11 00 | ||
| 204 | |||
| 205 | if (($upper_shadow < $upper_shadow_lower_threshold) and | ||
| 206 | ($upper_shadow > 0)) { | ||
| 207 | $cndl_code += (2 ** 2); | ||
| 208 | } | ||
| 209 | if (($upper_shadow_lower_threshold <= $upper_shadow) and | ||
| 210 | ($upper_shadow < $upper_shadow_upper_threshold)) { | ||
| 211 | $cndl_code += (2 ** 3); | ||
| 212 | } | ||
| 213 | if ($upper_shadow >= $upper_shadow_upper_threshold) { | ||
| 214 | $cndl_code += (2 ** 3 + 2 ** 2); | ||
| 215 | } | ||
| 216 | if (($lower_shadow < $lower_shadow_lower_threshold) and | ||
| 217 | ($lower_shadow > 0)) { | ||
| 218 | $cndl_code += (2 ** 1); | ||
| 219 | } | ||
| 220 | if (($lower_shadow_lower_threshold <= $lower_shadow) and | ||
| 221 | ($lower_shadow < $lower_shadow_upper_threshold)) { | ||
| 222 | $cndl_code += (2 ** 0); | ||
| 223 | } | ||
| 224 | if ($lower_shadow eq 0) { | ||
| 225 | $cndl_code += (2 ** 1 + 2 ** 0); | ||
| 226 | } | ||
| 227 | |||
| 228 | # Return the results | ||
| 229 | $indic->set($cndl_name, $i, $cndl_code); | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; |