| 1 | package Finance::GeniusTrader::Indicators::Generic::MaxInPeriod; | ||
| 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 = ("MaxInPeriod[#*]"); | ||
| 18 | @DEFAULT_ARGS = (2, "{I:Prices CLOSE}"); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Indicators::Generic::MaxInPeriod - Calculate a maximum | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This indicator calculates the maximum 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 maximum will | ||
| 36 | be calculated with the last <number> days. In the second case it will be the maximum | ||
| 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 HIGH} | ||
| 57 | |||
| 58 | =item "2005-04-05 14:30:00" {I:Prices HIGH} | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | sub initialize { | ||
| 64 | my ($self) = @_; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub calculate_interval { | ||
| 68 | my ($self, $calc, $first, $last) = @_; | ||
| 69 | |||
| 70 | my $name = $self->get_name; | ||
| 71 | |||
| 72 | ($first, $last) = $self->update_interval($calc, $first, $last); | ||
| 73 | return if ($calc->indicators->is_available_interval($name, $first, $last)); | ||
| 74 | |||
| 75 | my $arg = $self->{'args'}->get_arg_names(1); | ||
| 76 | my $currentHigh; | ||
| 77 | |||
| 78 | if ($arg =~ /^\d+$/) { | ||
| 79 | my $period = $arg; | ||
| 80 | return if (! $self->check_dependencies_interval($calc, $first - $period, $last)); | ||
| 81 | $currentHigh = $self->{'args'}->get_arg_values($calc, $first - $period, 2); | ||
| 82 | for (my $i = $first - $period; $i < $first; $i++) { | ||
| 83 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 84 | if ($currentValue > $currentHigh) { | ||
| 85 | $currentHigh = $currentValue; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | for (my $i = $first; $i <= $last; $i++) { | ||
| 89 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 90 | my $oldestValue = $self->{'args'}->get_arg_values($calc, $i - $period, 2); | ||
| 91 | if ($oldestValue == $currentHigh) { | ||
| 92 | # find new high between ($i-$period..$i) | ||
| 93 | my $tempHigh; | ||
| 94 | $currentHigh = $self->{'args'}->get_arg_values($calc, $i - $period + 1, 2); | ||
| 95 | for (my $j = $i - $period + 1; $j < $i; $j++) { | ||
| 96 | $tempHigh = $self->{'args'}->get_arg_values($calc, $j, 2); | ||
| 97 | if ($tempHigh > $currentHigh) { | ||
| 98 | $currentHigh = $tempHigh; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | if ($currentValue > $currentHigh) { | ||
| 103 | $currentHigh = $currentValue; | ||
| 104 | } | ||
| 105 | $calc->indicators->set($name, $i, $currentHigh); | ||
| 106 | } | ||
| 107 | } elsif ($arg =~/^\d{4}-\d\d(-\d\d)?( \d\d:\d\d:\d\d)?$/) { | ||
| 108 | my $n = undef; | ||
| 109 | if ($calc->prices->has_date($arg)) { | ||
| 110 | $n = $calc->prices->date($arg); | ||
| 111 | } else { | ||
| 112 | $n = $calc->prices->date($calc->prices->find_nearest_date($arg)); | ||
| 113 | } | ||
| 114 | if ($n > $first) { | ||
| 115 | $first = $n; | ||
| 116 | } | ||
| 117 | return if (! $self->check_dependencies_interval($calc, ($first > $n) ? $n : $first, $last)); | ||
| 118 | $currentHigh = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 119 | for (my $i = $n; $i < $first; $i++) { | ||
| 120 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 121 | if ($currentValue > $currentHigh) { | ||
| 122 | $currentHigh = $currentValue; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | for (my $i = $first; $i <= $last; $i++) { | ||
| 126 | my $currentValue = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 127 | if ($currentValue > $currentHigh) { | ||
| 128 | $currentHigh = $currentValue; | ||
| 129 | } | ||
| 130 | $calc->indicators->set($name, $i, $currentHigh); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub calculate { | ||
| 136 | my ($self, $calc, $i) = @_; | ||
| 137 | my $name = $self->get_name; | ||
| 138 | |||
| 139 | return if ($calc->indicators->is_available($name, $i)); | ||
| 140 | |||
| 141 | my $res = undef; | ||
| 142 | my $arg = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 143 | |||
| 144 | if ($arg =~ /^\d+$/) { | ||
| 145 | $res = $self->{'args'}->get_arg_values($calc, $i, 2); | ||
| 146 | for (my $n = 1; $n < $arg; $n++) { | ||
| 147 | my $val = $self->{'args'}->get_arg_values($calc, $i - $n, 2); | ||
| 148 | if (defined($val) && defined($res)) { | ||
| 149 | $res = max($res, $val); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } elsif ($arg =~/^\d{4}-\d\d(-\d\d)?( \d\d:\d\d:\d\d)?$/) { | ||
| 153 | my $n = undef; | ||
| 154 | if ($calc->prices->has_date($arg)) { | ||
| 155 | $n = $calc->prices->date($arg); | ||
| 156 | } else { | ||
| 157 | $n = $calc->prices->date($calc->prices->find_nearest_date($arg)); | ||
| 158 | } | ||
| 159 | if ($i >= $n) { | ||
| 160 | $res = $self->{'args'}->get_arg_values($calc, $n++, 2); | ||
| 161 | for(;$n <= $i; $n++) { | ||
| 162 | my $val = $self->{'args'}->get_arg_values($calc, $n, 2); | ||
| 163 | if (defined($val)) { | ||
| 164 | $res = max($res, $val); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | if (defined($res)) { | ||
| 171 | $calc->indicators->set($name, $i, $res); | ||
| 172 | } | ||
| 173 | } |