| 1 | package Finance::GeniusTrader::Systems; | ||
| 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(@ISA @EXPORT %OBJECT_REPOSITORY); | ||
| 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 | use Finance::GeniusTrader::OrderFactory::MarketPrice; | ||
| 17 | #ALL# use Log::Log4perl qw(:easy); | ||
| 18 | use Finance::GeniusTrader::ArgsTree; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Systems - | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | Trading systems are systems that decide what to buy and sell at which | ||
| 27 | prices and so on. | ||
| 28 | |||
| 29 | A system will propose buy/sell orders. The day after, those orders will | ||
| 30 | be either cancelled or confirmed. If the order is confirmed, then a | ||
| 31 | position is considered open. An open position will then be managed by | ||
| 32 | a close strategy. | ||
| 33 | |||
| 34 | =over | ||
| 35 | |||
| 36 | =item C<< $system->long_signal($calc, $i) >> | ||
| 37 | =item C<< $system->short_signal($calc, $i) >> | ||
| 38 | |||
| 39 | The system can generate 2 signals (buy or sell). A signal is an | ||
| 40 | intent to buy or sell. Those functions should be overriden by the specific | ||
| 41 | system. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | sub long_signal { | ||
| 45 | my ($self, $calc, $i) = @_; | ||
| 46 | |||
| 47 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 48 | |||
| 49 | if (defined($self->{'long_signal'})) | ||
| 50 | { | ||
| 51 | return $calc->signals->get($self->{'long_signal'}->get_name, $i); | ||
| 52 | } | ||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | sub short_signal { | ||
| 56 | my ($self, $calc, $i) = @_; | ||
| 57 | |||
| 58 | return 0 if (! $self->check_dependencies($calc, $i)); | ||
| 59 | |||
| 60 | if (defined($self->{'short_signal'})) | ||
| 61 | { | ||
| 62 | return $calc->signals->get($self->{'short_signal'}->get_name, $i); | ||
| 63 | } | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | =item C<< $system->set_long_signal() >> | ||
| 68 | |||
| 69 | =item C<< $system->set_short_signal() >> | ||
| 70 | |||
| 71 | Facility function to set which signal is used to generate buy/sell signal. | ||
| 72 | They are meant to be used in initialize only if long_signal and short_signal | ||
| 73 | are not overriden. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | sub set_long_signal { | ||
| 77 | my ($self, $signal) = @_; | ||
| 78 | $self->{'long_signal'} = $signal; | ||
| 79 | } | ||
| 80 | sub set_short_signal { | ||
| 81 | my ($self, $signal) = @_; | ||
| 82 | $self->{'short_signal'} = $signal; | ||
| 83 | } | ||
| 84 | |||
| 85 | =item C<< $system->precalculate_all($calc) >> | ||
| 86 | =item C<< $system->precalculate_interval($calc, $first, $last) >> | ||
| 87 | |||
| 88 | If you run a system on a long period of time you may want to precalculate | ||
| 89 | all the indicators in order to benefit of possible optimizations. This | ||
| 90 | is the role of those 2 functions. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | # Helper functions to optimize calculations | ||
| 94 | sub precalculate_all { | ||
| 95 | my ($self, $calc) = @_; | ||
| 96 | $self->precalculate_interval($calc, 0, $calc->prices->count - 1); | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | sub precalculate_interval { | ||
| 100 | my ($self, $calc, $first, $last) = @_; | ||
| 101 | # Can do nothing, I don't know which indicators are used | ||
| 102 | # This function must be overrident by specific systems | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | =item C<< $system->default_order_factory() >> | ||
| 107 | |||
| 108 | Return an object OrderFactory that can be used if no other objects was | ||
| 109 | to be used. | ||
| 110 | |||
| 111 | =cut | ||
| 112 | sub default_order_factory { | ||
| 113 | return Finance::GeniusTrader::OrderFactory::MarketPrice->new; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Default functions so that they are not mandatory | ||
| 117 | sub initialize { 1; } | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =head2 Functions to manage a repository of systems | ||
| 122 | |||
| 123 | Finance::GeniusTrader::Systems::get_registered_object($name); | ||
| 124 | Finance::GeniusTrader::Systems::register_object($name, $object); | ||
| 125 | Finance::GeniusTrader::Systems::get_or_register_object($name, $object); | ||
| 126 | Finance::GeniusTrader::Systems::manage_object(\@NAMES, $object, $class, $args, $key); | ||
| 127 | |||
| 128 | =over | ||
| 129 | |||
| 130 | =cut | ||
| 131 | sub get_registered_object { | ||
| 132 | Finance::GeniusTrader::Registry::get_registered_object(\%OBJECT_REPOSITORY, @_); | ||
| 133 | } | ||
| 134 | sub register_object { | ||
| 135 | Finance::GeniusTrader::Registry::register_object(\%OBJECT_REPOSITORY, @_); | ||
| 136 | } | ||
| 137 | sub get_or_register_object { | ||
| 138 | Finance::GeniusTrader::Registry::get_or_register_object(\%OBJECT_REPOSITORY, @_); | ||
| 139 | } | ||
| 140 | sub manage_object { | ||
| 141 | Finance::GeniusTrader::Registry::manage_object(\%OBJECT_REPOSITORY, @_); | ||
| 142 | } | ||
| 143 | |||
| 144 | =item C<< Finance::GeniusTrader::Systems::Module->new($args) >> | ||
| 145 | |||
| 146 | Create a new Systems with the given arguments. $args is optional. | ||
| 147 | |||
| 148 | =cut | ||
| 149 | sub new { | ||
| 150 | my ($type, $args) = @_; | ||
| 151 | my $class = ref($type) || $type; | ||
| 152 | |||
| 153 | no strict "refs"; | ||
| 154 | |||
| 155 | my $self = { }; | ||
| 156 | if (defined($args)) { | ||
| 157 | if ( $#{$args} < $#{"$class\::DEFAULT_ARGS"} ) { | ||
| 158 | for (my $n=($#{$args}+1); $n<=$#{"$class\::DEFAULT_ARGS"}; $n++) { | ||
| 159 | push @{$args}, ${"$class\::DEFAULT_ARGS"}[$n]; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{$args}); | ||
| 163 | } elsif (defined (@{"$class\::DEFAULT_ARGS"})) { | ||
| 164 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(@{"$class\::DEFAULT_ARGS"}); | ||
| 165 | } else { | ||
| 166 | $self->{'args'} = Finance::GeniusTrader::ArgsTree->new(); # no args | ||
| 167 | } | ||
| 168 | |||
| 169 | return manage_object(\@{"$class\::NAMES"}, $self, $class, $self->{'args'},''); | ||
| 170 | } | ||
| 171 | |||
| 172 | =back | ||
| 173 | =cut | ||
| 174 | 1; |