| 1 | package Finance::GeniusTrader::Signals; | ||
| 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 | use strict; | ||
| 8 | use vars qw(%OBJECT_REPOSITORY @ISA @EXPORT); | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | @ISA = qw(Exporter Finance::GeniusTrader::Dependency); | ||
| 12 | @EXPORT = qw(&build_object_name &manage_object); | ||
| 13 | |||
| 14 | use Finance::GeniusTrader::Registry; | ||
| 15 | use Finance::GeniusTrader::Dependency; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Finance::GeniusTrader::Signals - Base module for all signals | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | =head2 Overview | ||
| 24 | |||
| 25 | Signals are objects that returns true/false for each quotation. This | ||
| 26 | value doesn't have any other direct meaning (ie it's not buy/sell). | ||
| 27 | However those results will probably be used by trading systems (in | ||
| 28 | cunjunction with other informations) to decide what to do | ||
| 29 | (buy/sell/update a stop/nothing). | ||
| 30 | |||
| 31 | =head2 Detailed description | ||
| 32 | |||
| 33 | =over | ||
| 34 | |||
| 35 | =item C<< my $sig = Finance::GeniusTrader::Signals::AnExample->new([ @args ]) >> | ||
| 36 | |||
| 37 | Create a signal object with the appropriate parameters. | ||
| 38 | |||
| 39 | =item C<< $sig->get_name or $sig->get_name($i) >> | ||
| 40 | |||
| 41 | Get the name of the signal. If the signal returns several values, | ||
| 42 | you can get the name corresponding to any value, you just have to | ||
| 43 | precise in the parameters the index of the value that you're interested | ||
| 44 | in. | ||
| 45 | |||
| 46 | =item C<< $sig->get_nb_values >> | ||
| 47 | |||
| 48 | Return the number of different values produced by this signal that are | ||
| 49 | available for use. | ||
| 50 | |||
| 51 | =item C<< $sig->initialize() >> | ||
| 52 | |||
| 53 | This callback function is called at creating time. Since the "new" function | ||
| 54 | is inherited, you should do the initialization via this function. | ||
| 55 | |||
| 56 | =item C<< $sig->detect($calc, $i) >> | ||
| 57 | |||
| 58 | Stores the value of the signal for the day $i. | ||
| 59 | |||
| 60 | =item C<< $sig->detect_interval($calc, $first, $last) >> | ||
| 61 | |||
| 62 | Stores the value of the signal for all the days of the specified interval. | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | =head2 General exported functions | ||
| 67 | |||
| 68 | =over | ||
| 69 | |||
| 70 | =item C<< build_object_name($encoded, [ @args ], $key) >> | ||
| 71 | |||
| 72 | Generate the name of a signal based on its "encoded" name. | ||
| 73 | |||
| 74 | =back | ||
| 75 | |||
| 76 | =head2 Functions to manage a repository of signals | ||
| 77 | |||
| 78 | Finance::GeniusTrader::Signals::get_registered_object($name); | ||
| 79 | Finance::GeniusTrader::Signals::register_object($name, $object); | ||
| 80 | Finance::GeniusTrader::Signals::get_or_register_object($name, $object); | ||
| 81 | Finance::GeniusTrader::Signals::manage_object(\@NAMES, $self, $class, $args, $key); | ||
| 82 | |||
| 83 | =cut | ||
| 84 | sub get_registered_object { | ||
| 85 | Finance::GeniusTrader::Registry::get_registered_object(\%OBJECT_REPOSITORY, @_); | ||
| 86 | } | ||
| 87 | sub register_object { | ||
| 88 | Finance::GeniusTrader::Registry::register_object(\%OBJECT_REPOSITORY, @_); | ||
| 89 | } | ||
| 90 | sub get_or_register_object { | ||
| 91 | Finance::GeniusTrader::Registry::get_or_register_object(\%OBJECT_REPOSITORY, @_); | ||
| 92 | } | ||
| 93 | sub manage_object { | ||
| 94 | Finance::GeniusTrader::Registry::manage_object(\%OBJECT_REPOSITORY, @_); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | # DEFAULT FUNCTIONS FOR SIGNALS | ||
| 99 | sub new { | ||
| 100 | my ($type, $args, $key) = @_; | ||
| 101 | my $class = ref($type) || $type; | ||
| 102 | |||
| 103 | no strict "refs"; | ||
| 104 | |||
| 105 | my $self = { }; | ||
| 106 | if (defined($args)) { | ||
| 107 | if ( $#{$args} < $#{"$class\::DEFAULT_ARGS"} ) { | ||
| 108 | for (my $n=($#{$args}+1); $n<=$#{"$class\::DEFAULT_ARGS"}; $n++) { | ||
| 109 | push @{$args}, ${"$class\::DEFAULT_ARGS"}[$n]; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{$args}); | ||
| 113 | } elsif (defined (@{"$class\::DEFAULT_ARGS"})) { | ||
| 114 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{"$class\::DEFAULT_ARGS"}); | ||
| 115 | } else { | ||
| 116 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(); # no args | ||
| 117 | } | ||
| 118 | |||
| 119 | return manage_object(\@{"$class\::NAMES"}, $self, $class, $self->{'args'}, $key); | ||
| 120 | } | ||
| 121 | |||
| 122 | # Provide a default non-optimized version of detect_interval that | ||
| 123 | # calls detect once for each day. | ||
| 124 | # | ||
| 125 | # Real signals are encouraged to override this function to provide an | ||
| 126 | # optimized version of the detection algorithm by possibly reusing | ||
| 127 | # the result of previous days. | ||
| 128 | sub detect_interval { | ||
| 129 | my ($self, $calc, $first, $last) = @_; | ||
| 130 | |||
| 131 | if (ref($self->{'args'}) =~ /Finance::GeniusTrader::ArgsTree/) { | ||
| 132 | $self->{'args'}->prepare_interval($calc, $first, $last); | ||
| 133 | } | ||
| 134 | for (my $i = $first; $i <= $last; $i++) | ||
| 135 | { | ||
| 136 | $self->detect($calc, $i); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | sub initialize { 1; } | ||
| 141 | |||
| 142 | 1; |