| 1 | package Finance::GeniusTrader::Indicators::Generic::ByName; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Oliver Bossert | ||
| 4 | # Copyright 2008 Thomas Weigert | ||
| 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 | # $Id$ | ||
| 9 | |||
| 10 | # Standards-Version: 1.0 | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 14 | |||
| 15 | use Finance::GeniusTrader::Indicators; | ||
| 16 | use Finance::GeniusTrader::ArgsTree; | ||
| 17 | use Finance::GeniusTrader::Prices; | ||
| 18 | |||
| 19 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 20 | @NAMES = ("ByName[#*]"); | ||
| 21 | @DEFAULT_ARGS = (""); | ||
| 22 | |||
| 23 | =head2 NAME | ||
| 24 | |||
| 25 | Finance::GeniusTrader::Indicators::Generic::ByName - Alias to another indicator | ||
| 26 | |||
| 27 | =head2 DESCRIPTION | ||
| 28 | |||
| 29 | Sometimes, during the computation of an indicator, one needs to reference | ||
| 30 | the current value of a series that is being computed by this indicator. | ||
| 31 | If the current indicator is used explicity, an infinite recursion arises | ||
| 32 | due to the dependency mechanism. | ||
| 33 | |||
| 34 | This indicator resolves the recursion. This indicator is nothing more than | ||
| 35 | an alias of a series calculated by an indicator. Just give as first parameter | ||
| 36 | the name of the value to use. | ||
| 37 | |||
| 38 | This indicator is used when, during the calculation of an indicator, | ||
| 39 | an intermediate series wants to leverage another intermediate series | ||
| 40 | or an output value. | ||
| 41 | |||
| 42 | For example, | ||
| 43 | |||
| 44 | $self->{'sma1'} = Finance::GeniusTrader::Indicators::SMA->new([ | ||
| 45 | $self->{'args'}->get_arg_names(1), | ||
| 46 | $self->{'args'}->get_arg_names(2) ]); | ||
| 47 | $self->{'sma2'} = Finance::GeniusTrader::Indicators::SMA->new([ | ||
| 48 | $self->{'args'}->get_arg_names(1), | ||
| 49 | "{I:Generic:ByName " | ||
| 50 | . $self->{'sma1'}->get_name . "}" ]); | ||
| 51 | |||
| 52 | The first statement defines an intermediate series which smoothes the | ||
| 53 | second parameter. The second statement takes that series and applies | ||
| 54 | smoothing again. Similarly, the following applies smoothing to | ||
| 55 | the first output value. | ||
| 56 | |||
| 57 | $self->{'sma3'} = Finance::GeniusTrader::Indicators::SMA->new([ | ||
| 58 | $self->{'args'}->get_arg_names(1), | ||
| 59 | "{I:Generic:ByName " . $self->get_name(0) . "}" ]); | ||
| 60 | |||
| 61 | Care has to be taken that I:Generic:ByName is in fact given the | ||
| 62 | name of an indicator, lest that series will not be found. Note | ||
| 63 | that if the series is an indicator, the name of the series is the | ||
| 64 | name of the selected return value. The get_name method will always | ||
| 65 | retrieve the name of a series. | ||
| 66 | |||
| 67 | Remember that the parseable syntax does not yield a name. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub new { | ||
| 72 | my ($type, $pieces, $key) = @_; | ||
| 73 | my $class = ref($type) || $type; | ||
| 74 | my $self = { }; | ||
| 75 | no strict "refs"; | ||
| 76 | |||
| 77 | my @args = @{$pieces}; | ||
| 78 | ### map {print "ByName new:$_\n"} @args; | ||
| 79 | my $argstr = shift @args; | ||
| 80 | foreach (@args) { | ||
| 81 | if ($_ =~ /^\]/) { | ||
| 82 | $argstr .= $_; | ||
| 83 | } elsif ($_ =~ /^,$/) { | ||
| 84 | $argstr .= $_; | ||
| 85 | } else { | ||
| 86 | $argstr .= " $_"; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | $self->{'args'}->[0] = $argstr; | ||
| 90 | ### print "ByName: $self->{'args'}->[0]\n"; | ||
| 91 | return manage_object(\@{"$class\::NAMES"}, $self, $class, $self->{'args'}, $key); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub initialize { | ||
| 95 | my ($self) = @_; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub calculate { | ||
| 100 | my ($self, $calc, $i) = @_; | ||
| 101 | my $indic = $calc->indicators; | ||
| 102 | my $name = $self->get_name(0); | ||
| 103 | |||
| 104 | my @pars = (); | ||
| 105 | my $parname = $self->{'args'}->[0]; | ||
| 106 | ### print "ByName($i): $parname = " . (($indic->is_available($parname, $i))?$indic->get($parname, $i):'') . "\n"; | ||
| 107 | |||
| 108 | $indic->set($name, $i, $indic->get($parname, $i)) | ||
| 109 | if ($indic->is_available($parname, $i)); | ||
| 110 | |||
| 111 | } | ||
| 112 | |||
| 113 | 1; |