| 1 | package Finance::GeniusTrader::Indicators::Generic::MinInPeriod; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog | ||
| 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::Prices; | ||
| 14 | use Finance::GeniusTrader::Tools qw(:math); | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("MinInPeriod[#*]"); | ||
| 18 | @DEFAULT_ARGS = (2, "{I:Prices CLOSE}"); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Indicators::Generic::MinInPeriod - Calculate a mimimum | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This indicator calculates the minimum of any serie of data in the | ||
| 27 | last XX days or since a given date. | ||
| 28 | |||
| 29 | =head1 PARAMETERS | ||
| 30 | |||
| 31 | =over | ||
| 32 | |||
| 33 | =item Number of days / date | ||
| 34 | |||
| 35 | You can specify either a number or a date. In the first case the minimum will | ||
| 36 | be calculated with the last <number> days. In the second case it will be the minimum | ||
| 37 | since the given date. | ||
| 38 | |||
| 39 | =item Data | ||
| 40 | |||
| 41 | This is the data to use as input. If you don't specify anything, the | ||
| 42 | closing price will be used by default. | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | Example of accepted argument list : | ||
| 47 | |||
| 48 | =over | ||
| 49 | |||
| 50 | =item 10 {I:RSI} | ||
| 51 | |||
| 52 | =item 2005-01-03 | ||
| 53 | |||
| 54 | =item 5 | ||
| 55 | |||
| 56 | =item 2005-04-05 {I:Prices LOW} | ||
| 57 | |||
| 58 | =item "2005-04-05 14:30:00" {I:Prices LOW} | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | sub initialize { | ||
| 64 | my ($self) = @_; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub calculate { | ||
| 68 | my ($self, $calc, $i) = @_; | ||
| 69 | my $name = $self->get_name; | ||
| 70 | |||
| 71 | return if ($calc->indicators->is_available($name, $i)); | ||
| 72 | |||
| 73 | my $res = undef; | ||
| 74 | my $arg = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 75 | |||
| 76 | if ($arg =~ /^\d+$/) { | ||
| 77 | $res = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 78 | for (my $n = 1; $n < $arg; $n++) { | ||
| 79 | my $val = $self->{'args'}->get_arg_values($calc, $i - $n, 2); | ||
| 80 | if (defined($val) && defined($res)) { | ||
| 81 | $res = min($res, $val); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } elsif ($arg =~/^\d{4}-\d\d(-\d\d)?( \d\d:\d\d:\d\d)?$/) { | ||
| 85 | my $n = undef; | ||
| 86 | if ($calc->prices->has_date($arg)) { | ||
| 87 | $n = $calc->prices->date($arg); | ||
| 88 | } else { | ||
| 89 | $n = $calc->prices->date($calc->prices->find_nearest_date($arg)); | ||
| 90 | } | ||
| 91 | if ($i >= $n) { | ||
| 92 | $res = $self->{'args'}->get_arg_values($calc, $n++, 2); | ||
| 93 | for(;$n <= $i; $n++) { | ||
| 94 | my $val = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 95 | if (defined($val)) { | ||
| 96 | $res = min($res, $val); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | if (defined($res)) { | ||
| 103 | $calc->indicators->set($name, $i, $res); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | sub calculate_interval { | ||
| 108 | my ($self, $calc, $first, $last) = @_; | ||
| 109 | |||
| 110 | my $name = $self->get_name; | ||
| 111 | |||
| 112 | ($first, $last) = $self->update_interval($calc, $first, $last); | ||
| 113 | return if ($calc->indicators->is_available_interval($name, $first, $last)); | ||
| 114 | |||
| 115 | my $arg = $self->{'args'}->get_arg_names(1); | ||
| 116 | my $currentLow; | ||
| 117 | |||
| 118 | if ($arg =~ /^\d+$/) { | ||
| 119 | my $period = $arg; | ||
| 120 | return if (! $self->check_dependencies_interval($calc, $first - $period, $last)); | ||
| 121 | $currentLow = $self->{'args'}->get_arg_values($calc, $first - $period, 2); | ||
| 122 | for (my $i = $first - $period; $i < $first; $i++) { | ||
| 123 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 124 | if ($currentValue < $currentLow) { | ||
| 125 | $currentLow = $currentValue; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | for (my $i = $first; $i <= $last; $i++) { | ||
| 129 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 130 | my $oldestValue = $self->{'args'}->get_arg_values($calc, $i - $period, 2); | ||
| 131 | if ($oldestValue == $currentLow) { | ||
| 132 | # find new low between ($i-$period..$i) | ||
| 133 | my $tempLow; | ||
| 134 | $currentLow = $self->{'args'}->get_arg_values($calc, $i - $period + 1, 2); | ||
| 135 | for (my $j = $i - $period + 1; $j < $i; $j++) { | ||
| 136 | $tempLow = $self->{'args'}->get_arg_values($calc, $j, 2); | ||
| 137 | if ($tempLow < $currentLow) { | ||
| 138 | $currentLow = $tempLow; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | if ($currentValue < $currentLow) { | ||
| 143 | $currentLow = $currentValue; | ||
| 144 | } | ||
| 145 | $calc->indicators->set($name, $i, $currentLow); | ||
| 146 | } | ||
| 147 | } elsif ($arg =~/^\d{4}-\d\d(-\d\d)?( \d\d:\d\d:\d\d)?$/) { | ||
| 148 | my $n = undef; | ||
| 149 | if ($calc->prices->has_date($arg)) { | ||
| 150 | $n = $calc->prices->date($arg); | ||
| 151 | } else { | ||
| 152 | $n = $calc->prices->date($calc->prices->find_nearest_date($arg)); | ||
| 153 | } | ||
| 154 | if ($n > $first) { | ||
| 155 | $first = $n; | ||
| 156 | } | ||
| 157 | return if (! $self->check_dependencies_interval($calc, ($first > $n) ? $n : $first, $last)); | ||
| 158 | $currentLow = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 159 | for (my $i = $n; $i < $first; $i++) { | ||
| 160 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 161 | if ($currentValue < $currentLow) { | ||
| 162 | $currentLow = $currentValue; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | for (my $i = $first; $i <= $last; $i++) { | ||
| 166 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 167 | if ($currentValue < $currentLow) { | ||
| 168 | $currentLow = $currentValue; | ||
| 169 | } | ||
| 170 | $calc->indicators->set($name, $i, $currentLow); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } |