« GeniusTrader/Indicators/Generic/SignalLength.pm
↑ Summary
GeniusTrader/Indicators/BPCorrelation.pm »
| 1 | package Finance::GeniusTrader::SystemManager; | ||
| 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); | ||
| 9 | |||
| 10 | #ALL# use Log::Log4perl qw(:easy); | ||
| 11 | use Finance::GeniusTrader::Eval; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Finance::GeniusTrader::SystemManager - Manages trading systems | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | A SystemManager is an entity interacting between a PortfolioManager, a | ||
| 20 | Trading System (signals), TradeFilters, OrderFactory and CloseStrategy. | ||
| 21 | |||
| 22 | Filters can be applied to accept/refuse trades proposed by the trading | ||
| 23 | system. | ||
| 24 | |||
| 25 | A system manager is not completely defined until all desired objects have | ||
| 26 | been "linked" to it using all the add_* and set_* functions. When all | ||
| 27 | those calls have been made, you should call B<finalize> to let the manager | ||
| 28 | know that you've finished setting it up. After that, the system manager | ||
| 29 | can be identified with an unique (and quite long) name. | ||
| 30 | |||
| 31 | Later you'll be able to setup the same system manager by using | ||
| 32 | setup_from_name($name). | ||
| 33 | |||
| 34 | =over | ||
| 35 | |||
| 36 | =item C<< my $sm = Finance::GeniusTrader::SystemManager->new($system) >> | ||
| 37 | |||
| 38 | Create a new system manager used to control a trading system. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | sub new { | ||
| 42 | my $type = shift; | ||
| 43 | my $class = ref($type) || $type; | ||
| 44 | my $system = shift; | ||
| 45 | |||
| 46 | my $self = { "filters" => [], "position_managers" => [] }; | ||
| 47 | |||
| 48 | bless $self, $class; | ||
| 49 | |||
| 50 | if (defined($system)) { | ||
| 51 | $self->set_portfolio($system); | ||
| 52 | } | ||
| 53 | |||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | =item C<< $sm->set_system($system) >> | ||
| 58 | |||
| 59 | Define the system that is managed. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | sub set_system { | ||
| 63 | my ($self, $system) = @_; | ||
| 64 | |||
| 65 | #WAR# WARN "system is defined" if ( defined($system)); | ||
| 66 | |||
| 67 | $self->{"system"} = $system; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | =item C<< $sm->system() >> | ||
| 72 | |||
| 73 | Return the system managed by this manager. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | sub system { | ||
| 77 | my ($self) = @_; | ||
| 78 | return $self->{'system'}; | ||
| 79 | } | ||
| 80 | |||
| 81 | =item C<< $manager->add_trade_filter() >> | ||
| 82 | |||
| 83 | =item C<< $manager->delete_all_trade_filter() >> | ||
| 84 | |||
| 85 | Use a trade filter and remove all trade filters currently used. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | sub add_trade_filter { | ||
| 89 | my ($self, $filter) = @_; | ||
| 90 | |||
| 91 | push @{$self->{'filters'}}, $filter; | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 | sub delete_all_trade_filter { | ||
| 96 | my ($self) = @_; | ||
| 97 | |||
| 98 | $self->{'filters'} = []; | ||
| 99 | |||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | =item C<< $self->accept_trade($order, $i, $calc, $pf_manager) >> | ||
| 104 | |||
| 105 | Apply all the trade filters to the proposed tarde and return | ||
| 106 | the result (accepted or not). | ||
| 107 | |||
| 108 | =cut | ||
| 109 | sub accept_trade { | ||
| 110 | my ($self, $order, $i, $calc, $pf_manager) = @_; | ||
| 111 | |||
| 112 | foreach (@{$self->{'filters'}}) | ||
| 113 | { | ||
| 114 | if (! $_->accept_trade($order, $i, $calc, $pf_manager->portfolio)) | ||
| 115 | { | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | =item C<< $self->send_buy_order($calc, $i, $pf_manager) >> | ||
| 123 | |||
| 124 | =item C<< $self->send_sell_order($calc, $i, $pf_manager) >> | ||
| 125 | |||
| 126 | Those functions are called by the systems to launch an order. The | ||
| 127 | SystemManager delegates this to an Order object. It will | ||
| 128 | use the Order object given by set_default_order() or it will | ||
| 129 | fallback to the order suggested by the system. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | sub get_buy_order { | ||
| 133 | my ($self, $calc, $i, $pf_manager) = @_; | ||
| 134 | |||
| 135 | my $order; | ||
| 136 | if (defined($self->{'order_factory'})) | ||
| 137 | { | ||
| 138 | $order = $self->{'order_factory'}->create_buy_order($calc, $i, | ||
| 139 | $self, $pf_manager); | ||
| 140 | } else { | ||
| 141 | $order = $self->{'system'}->default_order_factory->create_buy_order( | ||
| 142 | $calc, $i, $self, $pf_manager); | ||
| 143 | } | ||
| 144 | return $order; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get_sell_order { | ||
| 148 | my ($self, $calc, $i, $pf_manager) = @_; | ||
| 149 | |||
| 150 | my $order; | ||
| 151 | if (defined($self->{'order_factory'})) | ||
| 152 | { | ||
| 153 | $order = $self->{'order_factory'}->create_sell_order($calc, $i, | ||
| 154 | $self, $pf_manager); | ||
| 155 | } else { | ||
| 156 | $order = $self->{'system'}->default_order_factory->create_sell_order( | ||
| 157 | $calc, $i, $self, $pf_manager); | ||
| 158 | } | ||
| 159 | return $order; | ||
| 160 | } | ||
| 161 | |||
| 162 | =item C<< $self->set_order_factory($order_factory) >> | ||
| 163 | |||
| 164 | Defines which OrderFactory object will be used to send the orders. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | sub set_order_factory { | ||
| 168 | my ($self, $os) = @_; | ||
| 169 | $self->{'order_factory'} = $os; | ||
| 170 | } | ||
| 171 | |||
| 172 | =item C<< $self->add_position_manager($close_strategy) >> | ||
| 173 | |||
| 174 | =item C<< $self->delete_all_position_manager() >> | ||
| 175 | |||
| 176 | |||
| 177 | Add a position manager to the chain of position manager. A position | ||
| 178 | manager is better known as "CloseStragegy". | ||
| 179 | |||
| 180 | =cut | ||
| 181 | sub add_position_manager { | ||
| 182 | my ($self, $cs) = @_; | ||
| 183 | #WAR# WARN "Parameter must be a close strategy object" if ( ref($cs) =~ /Finance::GeniusTrader::CloseStrategy/); | ||
| 184 | push @{$self->{'position_managers'}}, $cs; | ||
| 185 | return; | ||
| 186 | } | ||
| 187 | sub delete_all_position_manager { | ||
| 188 | my ($self) = @_; | ||
| 189 | $self->{'position_managers'} = []; | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | sub default_position_manager { | ||
| 193 | my ($self, $cs) = @_; | ||
| 194 | if (! scalar(@{$self->{'position_managers'}})) | ||
| 195 | { | ||
| 196 | push @{$self->{'position_managers'}}, $cs; | ||
| 197 | } | ||
| 198 | return; | ||
| 199 | } | ||
| 200 | |||
| 201 | =item C<< $sm->manage_position($calc, $i, $position, $pf_manager) >> | ||
| 202 | |||
| 203 | Manages a open position with the current system. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | sub manage_position { | ||
| 207 | my ($self, $calc, $i, $position, $pf_manager) = @_; | ||
| 208 | |||
| 209 | #WAR# WARN "position is defined" if ( defined($position)); | ||
| 210 | #WAR# WARN "position quantity is positive" if ( $position->quantity > 0); | ||
| 211 | |||
| 212 | return if ($position->code() ne $calc->code()); | ||
| 213 | |||
| 214 | foreach my $cs (@{$self->{'position_managers'}}) | ||
| 215 | { | ||
| 216 | #ERR# ERROR "$_ should be a CloseStrategy object" if ( ref($cs) =~ /Finance::GeniusTrader::CloseStrategy/); | ||
| 217 | if ($position->is_long) { | ||
| 218 | $cs->manage_long_position($calc, $i, $position, $pf_manager, $self); | ||
| 219 | } else { | ||
| 220 | $cs->manage_short_position($calc, $i, $position, $pf_manager, $self); | ||
| 221 | } | ||
| 222 | # Stop the close strategy chain if the last strategy | ||
| 223 | # decided to close the position | ||
| 224 | last if ($position->being_closed); | ||
| 225 | } | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | =item C<< $sm->position_opened($calc, $i, $position, $pf_manager) >> | ||
| 230 | |||
| 231 | Has to be called once a position has been opened and wants to be | ||
| 232 | managed by this system manager. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | sub position_opened { | ||
| 236 | my ($self, $calc, $i, $position, $pf_manager) = @_; | ||
| 237 | |||
| 238 | #WAR# WARN "position is defined" if ( defined($position)); | ||
| 239 | #WAR# WARN "position quantity is positive" if ( $position->quantity > 0); | ||
| 240 | |||
| 241 | foreach my $cs (@{$self->{'position_managers'}}) | ||
| 242 | { | ||
| 243 | if ($position->is_long) { | ||
| 244 | $cs->long_position_opened($calc, $i, $position, $pf_manager, $self); | ||
| 245 | } else { | ||
| 246 | $cs->short_position_opened($calc, $i, $position, $pf_manager, $self); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | |||
| 252 | =item C<< $sm->get_indicative_stop($calc, $i, $order, $pf_manager) >> | ||
| 253 | |||
| 254 | Get an indicative stop level for the position that will be opened by | ||
| 255 | this order. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | sub get_indicative_stop { | ||
| 259 | my ($self, $calc, $i, $order, $pf_manager) = @_; | ||
| 260 | |||
| 261 | my $stop = 0; | ||
| 262 | foreach my $cs (@{$self->{'position_managers'}}) | ||
| 263 | { | ||
| 264 | my $newstop = $cs->get_indicative_stop($calc, $i, $order, $pf_manager, $self); | ||
| 265 | next if ($newstop == 0); | ||
| 266 | $stop = $newstop if ($stop == 0); | ||
| 267 | if ($order->is_buy_order) | ||
| 268 | { | ||
| 269 | $stop = $newstop if ($newstop > $stop); | ||
| 270 | } else { | ||
| 271 | $stop = $newstop if ($newstop < $stop); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | return $stop; | ||
| 275 | } | ||
| 276 | |||
| 277 | =item C<< $sm->apply_system($calc, $i, $pf_manager) >> | ||
| 278 | |||
| 279 | This function will use the generated signals to pass the order. It | ||
| 280 | delegates this responsibility to the PortfolioManager. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | sub apply_system { | ||
| 284 | my ($self, $calc, $i, $pf_manager) = @_; | ||
| 285 | |||
| 286 | if ($self->system->long_signal($calc, $i)) | ||
| 287 | { | ||
| 288 | my $order = $self->get_buy_order($calc, $i, $pf_manager); | ||
| 289 | if (ref($order)) # Stop if order is not created | ||
| 290 | { | ||
| 291 | $order->set_indicative_stop( | ||
| 292 | $self->get_indicative_stop($calc, $i, $order, $pf_manager)); | ||
| 293 | if ($self->accept_trade($order, $i, $calc, $pf_manager)) | ||
| 294 | { | ||
| 295 | $pf_manager->submit_order($order, $i ,$calc); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | if ($self->system->short_signal($calc, $i)) | ||
| 300 | { | ||
| 301 | my $order = $self->get_sell_order($calc, $i, $pf_manager); | ||
| 302 | if (ref($order)) # Stop if order is not created | ||
| 303 | { | ||
| 304 | $order->set_indicative_stop( | ||
| 305 | $self->get_indicative_stop($calc, $i, $order, $pf_manager)); | ||
| 306 | if ($self->accept_trade($order, $i, $calc, $pf_manager)) | ||
| 307 | { | ||
| 308 | $pf_manager->submit_order($order, $i ,$calc); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | return; | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | sub apply_system_parked { | ||
| 317 | my ($self, $calc, $i, $pf_manager) = @_; | ||
| 318 | |||
| 319 | if ($self->system->long_signal($calc, $i)) | ||
| 320 | { | ||
| 321 | my $order = $self->get_buy_order($calc, $i, $pf_manager); | ||
| 322 | if (ref($order)) # Stop if order is not created | ||
| 323 | { | ||
| 324 | $order->set_indicative_stop( | ||
| 325 | $self->get_indicative_stop($calc, $i, $order, $pf_manager)); | ||
| 326 | if ($self->accept_trade($order, $i, $calc, $pf_manager)) | ||
| 327 | { | ||
| 328 | $pf_manager->park_order($order, $i ,$calc); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | } | ||
| 332 | if ($self->system->short_signal($calc, $i)) | ||
| 333 | { | ||
| 334 | my $order = $self->get_sell_order($calc, $i, $pf_manager); | ||
| 335 | if (ref($order)) # Stop if order is not created | ||
| 336 | { | ||
| 337 | $order->set_indicative_stop( | ||
| 338 | $self->get_indicative_stop($calc, $i, $order, $pf_manager)); | ||
| 339 | if ($self->accept_trade($order, $i, $calc, $pf_manager)) | ||
| 340 | { | ||
| 341 | $pf_manager->park_order($order, $i ,$calc); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | } | ||
| 345 | return; | ||
| 346 | } | ||
| 347 | |||
| 348 | =item C<< $sm->precalculate_interval($calc, $i, $first, $last) >> | ||
| 349 | |||
| 350 | =cut | ||
| 351 | sub precalculate_interval { | ||
| 352 | my ($self, $calc, $first, $last) = @_; | ||
| 353 | |||
| 354 | $self->{'system'}->precalculate_interval($calc, $first, $last); | ||
| 355 | |||
| 356 | foreach my $cs (@{$self->{'position_managers'}}) | ||
| 357 | { | ||
| 358 | $cs->precalculate_interval($calc, $first, $last); | ||
| 359 | } | ||
| 360 | foreach my $filter (@{$self->{'filters'}}) | ||
| 361 | { | ||
| 362 | $filter->precalculate_interval($calc, $first, $last); | ||
| 363 | } | ||
| 364 | |||
| 365 | return; | ||
| 366 | } | ||
| 367 | |||
| 368 | =item C<< $sm->finalize() >> | ||
| 369 | |||
| 370 | Finalize the setup of the manager. Calculate its name. You can get its | ||
| 371 | name afterward using $sm->get_name | ||
| 372 | |||
| 373 | =cut | ||
| 374 | sub finalize { | ||
| 375 | my ($self) = @_; | ||
| 376 | |||
| 377 | my $name = get_standard_name($self->{'system'}); | ||
| 378 | if (defined($self->{'order_factory'})) | ||
| 379 | { | ||
| 380 | $name .= "|" . get_standard_name($self->{'order_factory'}); | ||
| 381 | } | ||
| 382 | foreach my $obj ( (@{$self->{'filters'}}, @{$self->{'position_managers'}}) ) | ||
| 383 | { | ||
| 384 | $name .= "|" . get_standard_name($obj); | ||
| 385 | } | ||
| 386 | $self->{'name'} = $name; | ||
| 387 | |||
| 388 | $OBJECT_REPOSITORY{$name} = $self; | ||
| 389 | |||
| 390 | return $name; | ||
| 391 | } | ||
| 392 | |||
| 393 | =item C<< $sm->get_name() >> | ||
| 394 | |||
| 395 | Return the name of the system. | ||
| 396 | |||
| 397 | =cut | ||
| 398 | sub get_name { | ||
| 399 | my ($self) = @_; | ||
| 400 | #ERR# ERROR "name is defined" if ( defined($self->{'name'})); | ||
| 401 | return $self->{'name'}; | ||
| 402 | } | ||
| 403 | |||
| 404 | =item C<< $sm->setup_from_name($name) >> | ||
| 405 | |||
| 406 | Setup the system manager according to the name. | ||
| 407 | |||
| 408 | =cut | ||
| 409 | sub setup_from_name { | ||
| 410 | my ($self, $name) = @_; | ||
| 411 | |||
| 412 | # Separate the system from the rest | ||
| 413 | my ($system_name, @addons) = map { s/^\s*//g; s/\s*$//g; $_ } | ||
| 414 | (split (/\|/, $name)); | ||
| 415 | my ($type, @system_args) = split(/\s+/, $system_name); | ||
| 416 | $self->{'system'} = create_standard_object($type, @system_args); | ||
| 417 | |||
| 418 | # Add all trade filters/order sender/close strategies | ||
| 419 | foreach (@addons) | ||
| 420 | { | ||
| 421 | my ($obj_name, @args) = split /\s+/; | ||
| 422 | my $object = create_standard_object($obj_name, @args); | ||
| 423 | |||
| 424 | if (ref($object) =~ /TradeFilters::/) { | ||
| 425 | $self->add_trade_filter($object); | ||
| 426 | } elsif (ref($object) =~ /OrderFactory::/) { | ||
| 427 | $self->set_order_factory($object); | ||
| 428 | } elsif (ref($object) =~ /CloseStrategy::/) { | ||
| 429 | $self->add_position_manager($object); | ||
| 430 | } elsif (ref($object) =~ /MoneyManagement::/) { | ||
| 431 | #$manager->add_money_management_rule($object); | ||
| 432 | } else { | ||
| 433 | die "Unknown object type: $name\n"; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | # Set the name | ||
| 437 | $self->{'name'} = $name; | ||
| 438 | |||
| 439 | return; | ||
| 440 | } | ||
| 441 | |||
| 442 | =item C<< $sm->set_alias_name($name) >> | ||
| 443 | |||
| 444 | =item C<< $sm->alias_name >> | ||
| 445 | |||
| 446 | |||
| 447 | =cut | ||
| 448 | sub set_alias_name { | ||
| 449 | $_[0]->{'alias'} = $_[1]; | ||
| 450 | } | ||
| 451 | sub alias_name { defined($_[0]->{'alias'}) && $_[0]->{'alias'} } | ||
| 452 | |||
| 453 | =item C<< my $sm = Finance::GeniusTrader::SystemManager::get_registered_object($name) >> | ||
| 454 | |||
| 455 | Returns the system manager corresponding to this name. | ||
| 456 | |||
| 457 | =cut | ||
| 458 | sub get_registered_object { | ||
| 459 | my ($name) = @_; | ||
| 460 | return $OBJECT_REPOSITORY{$name}; | ||
| 461 | } | ||
| 462 | |||
| 463 | =pod | ||
| 464 | |||
| 465 | =back | ||
| 466 | |||
| 467 | =cut | ||
| 468 | 1; |