| 1 | package Finance::GeniusTrader::Portfolio::Order; | ||
| 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 | our @ISA = qw(Finance::GeniusTrader::Serializable); | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | #ALL# use Log::Log4perl qw(:easy); | ||
| 11 | use Finance::GeniusTrader::Prices; | ||
| 12 | use Finance::GeniusTrader::Serializable; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Finance::GeniusTrader::Portfolio::Order - An order within the portfolio | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | =head2 Internal structure | ||
| 21 | |||
| 22 | { | ||
| 23 | "order" => "B", # Buy/Sell | ||
| 24 | "type" => "L", # Limited|Stop|APD|ATP|TR | ||
| 25 | "code" => "13000", | ||
| 26 | "quantity" => 100, | ||
| 27 | "price" => "12.4", # Main price | ||
| 28 | "price2" => "12.6", # Second limit (if needed) | ||
| 29 | "source" => "Trend", # Trading system that opened the position | ||
| 30 | # maybe "manual" | ||
| 31 | "date" => "2001-07-01", # date of submission | ||
| 32 | "validity" => "2001-07-02", # valable until this day | ||
| 33 | "no_discard" => 1, # don't remove the order automatically next day | ||
| 34 | "id" => 123 # id automatically assigned when added to | ||
| 35 | # the portfolio | ||
| 36 | } | ||
| 37 | |||
| 38 | =head2 Functions | ||
| 39 | |||
| 40 | =over | ||
| 41 | |||
| 42 | =item C<< $o = Finance::GeniusTrader::Portfolio::Order->new; >> | ||
| 43 | |||
| 44 | =cut | ||
| 45 | sub new { | ||
| 46 | my $type = shift; | ||
| 47 | my $class = ref($type) || $type; | ||
| 48 | |||
| 49 | my $self = { }; | ||
| 50 | |||
| 51 | bless $self, $class; | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | =item C<< $o->set_sell_order() >> | ||
| 56 | |||
| 57 | =item C<< $o->set_buy_order() >> | ||
| 58 | |||
| 59 | =item C<< $o->is_sell_order() >> | ||
| 60 | |||
| 61 | =item C<< $o->is_buy_order() >> | ||
| 62 | |||
| 63 | |||
| 64 | =cut | ||
| 65 | sub set_sell_order { $_[0]->{'order'} = 'S'; } | ||
| 66 | sub set_buy_order { $_[0]->{'order'} = 'B'; } | ||
| 67 | sub is_sell_order { $_[0]->{'order'} eq 'S' } | ||
| 68 | sub is_buy_order { $_[0]->{'order'} eq 'B' } | ||
| 69 | |||
| 70 | =item C<< $o->set_type($type) >> | ||
| 71 | |||
| 72 | =item C<< $o->get_type() >> | ||
| 73 | |||
| 74 | Manage the type of the order. Valid types are : | ||
| 75 | |||
| 76 | =over 8 | ||
| 77 | |||
| 78 | =item M | ||
| 79 | |||
| 80 | Market price | ||
| 81 | |||
| 82 | =item L | ||
| 83 | |||
| 84 | Limit | ||
| 85 | |||
| 86 | =item S | ||
| 87 | |||
| 88 | Stop | ||
| 89 | |||
| 90 | =item APD | ||
| 91 | |||
| 92 | A plage de déclenchement (french market only) | ||
| 93 | |||
| 94 | =item TR | ||
| 95 | |||
| 96 | Tout ou rien (french market) | ||
| 97 | |||
| 98 | =item ATP | ||
| 99 | |||
| 100 | A tout prix (french market) | ||
| 101 | |||
| 102 | =back | ||
| 103 | |||
| 104 | =cut | ||
| 105 | sub set_type { | ||
| 106 | my ($self, $type) = @_; | ||
| 107 | #ERR# ERROR "valid order type" unless ($type =~ /^(M|L|S|APD|ATP|TR)$/); | ||
| 108 | $self->{'type'} = $type; | ||
| 109 | } | ||
| 110 | sub type { $_[0]->{'type'} } | ||
| 111 | |||
| 112 | =item C<< $o->set_type_{limited,market_price,stop,stop_limited}() >> | ||
| 113 | |||
| 114 | =item C<< $o->is_type_{limited,market_price,stop,stop_limited}() >> | ||
| 115 | |||
| 116 | Change/checks the type of the order. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | sub set_type_limited { $_[0]->{'type'} = 'L'; } | ||
| 120 | sub set_type_market_price { $_[0]->{'type'} = 'M'; } | ||
| 121 | sub set_type_stop { $_[0]->{'type'} = 'S'; } | ||
| 122 | sub set_type_stop_limited { $_[0]->{'type'} = 'APD'; } | ||
| 123 | |||
| 124 | sub set_type_theoric_at_open { $_[0]->{'type'} = 'OPEN'; } | ||
| 125 | sub set_type_theoric_at_high { $_[0]->{'type'} = 'HIGH'; } | ||
| 126 | sub set_type_theoric_at_low { $_[0]->{'type'} = 'LOW'; } | ||
| 127 | sub set_type_theoric_at_close { $_[0]->{'type'} = 'CLOSE'; } | ||
| 128 | sub set_type_theoric_at_signal { $_[0]->{'type'} = 'SIGNAL'; } | ||
| 129 | |||
| 130 | sub is_type_limited { $_[0]->{'type'} eq 'L' } | ||
| 131 | sub is_type_market_price { $_[0]->{'type'} eq 'M' } | ||
| 132 | sub is_type_stop { $_[0]->{'type'} eq 'S' } | ||
| 133 | sub is_type_stop_limited { $_[0]->{'type'} eq 'APD' } | ||
| 134 | |||
| 135 | sub is_type_theoric_at_open { $_[0]->{'type'} eq 'OPEN' } | ||
| 136 | sub is_type_theoric_at_high { $_[0]->{'type'} eq 'HIGH' } | ||
| 137 | sub is_type_theoric_at_low { $_[0]->{'type'} eq 'LOW' } | ||
| 138 | sub is_type_theoric_at_close { $_[0]->{'type'} eq 'CLOSE' } | ||
| 139 | sub is_type_theoric_at_signal { $_[0]->{'type'} eq 'SIGNAL' } | ||
| 140 | |||
| 141 | =item C<< $o->set_code($code) >> | ||
| 142 | |||
| 143 | =item C<< $o->code() >> | ||
| 144 | |||
| 145 | Set/get the symbol of the traded share. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | sub set_code { | ||
| 149 | my ($self, $code) = @_; | ||
| 150 | $self->{'code'} = $code; | ||
| 151 | } | ||
| 152 | sub code { $_[0]->{'code'} } | ||
| 153 | |||
| 154 | =item C<< $o->set_quantity($quantity) >> | ||
| 155 | |||
| 156 | =item C<< $o->quantity() >> | ||
| 157 | |||
| 158 | Set/get the quantity of shares. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | sub set_quantity { | ||
| 162 | my ($self, $q) = @_; | ||
| 163 | $self->{'quantity'} = $q; | ||
| 164 | } | ||
| 165 | sub quantity { $_[0]->{'quantity'} } | ||
| 166 | |||
| 167 | =item C<< $o->set_price($price) >> | ||
| 168 | |||
| 169 | =item C<< $o->price() >> | ||
| 170 | |||
| 171 | =item C<< $o->set_second_price($price) >> | ||
| 172 | |||
| 173 | =item C<< $o->second_price() >> | ||
| 174 | |||
| 175 | Set/get the prices on the order. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | sub set_price { | ||
| 179 | my ($self, $price) = @_; | ||
| 180 | $self->{'price'} = $price; | ||
| 181 | } | ||
| 182 | sub set_second_price { | ||
| 183 | my ($self, $price) = @_; | ||
| 184 | $self->{'price2'} = $price; | ||
| 185 | } | ||
| 186 | sub price { $_[0]->{'price'} } | ||
| 187 | sub second_price { $_[0]->{'price2'} } | ||
| 188 | |||
| 189 | =item C<< $o->set_source($source) >> | ||
| 190 | |||
| 191 | =item C<< $o->source() >> | ||
| 192 | |||
| 193 | |||
| 194 | =cut | ||
| 195 | sub set_source { | ||
| 196 | my ($self, $source) = @_; | ||
| 197 | $self->{'source'} = $source; | ||
| 198 | } | ||
| 199 | sub source { $_[0]->{'source'} } | ||
| 200 | |||
| 201 | =item C<< $o->set_submission_date($date) >> | ||
| 202 | |||
| 203 | =item C<< $o->submissiont_date() >> | ||
| 204 | |||
| 205 | |||
| 206 | =cut | ||
| 207 | sub set_submission_date { | ||
| 208 | my ($self, $date) = @_; | ||
| 209 | $self->{'date'} = $date; | ||
| 210 | } | ||
| 211 | sub submission_date { $_[0]->{'date'} } | ||
| 212 | |||
| 213 | =item C<< $o->set_indicative_stop($price) >> | ||
| 214 | |||
| 215 | =item C<< $o->indicative_stop() >> | ||
| 216 | |||
| 217 | |||
| 218 | =cut | ||
| 219 | sub set_indicative_stop { | ||
| 220 | my ($self, $price) = @_; | ||
| 221 | $self->{'indicative_stop'} = $price; | ||
| 222 | } | ||
| 223 | sub indicative_stop { $_[0]->{'indicative_stop'} } | ||
| 224 | |||
| 225 | =item C<< $o->set_not_discardable() >> | ||
| 226 | |||
| 227 | =item C<< $o->set_discardable() >> | ||
| 228 | |||
| 229 | =item C<< $o->discardable() >> | ||
| 230 | |||
| 231 | A normal order has a validity of one period (ie one day usually). If | ||
| 232 | you want to place an order that should be kept until it's executed | ||
| 233 | (a close on target for example) you need to modify the order | ||
| 234 | by calling this function on it. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | sub set_not_discardable { $_[0]->{'no_discard'} = 1 } | ||
| 238 | sub set_discardable { $_[0]->{'no_discard'} = 0 } | ||
| 239 | sub discardable { | ||
| 240 | my ($self) = @_; | ||
| 241 | if (defined($self->{'no_discard'}) && $self->{'no_discard'}) | ||
| 242 | { | ||
| 243 | return 0; | ||
| 244 | } | ||
| 245 | return 1; | ||
| 246 | } | ||
| 247 | |||
| 248 | =item C<< $o->set_id($id) >> | ||
| 249 | |||
| 250 | =item C<< $o->id() >> | ||
| 251 | |||
| 252 | |||
| 253 | =cut | ||
| 254 | sub set_id { | ||
| 255 | my ($self, $id) = @_; | ||
| 256 | $self->{'id'} = $id; | ||
| 257 | } | ||
| 258 | sub id { $_[0]->{'id'} } | ||
| 259 | |||
| 260 | =item C<< $o->set_attribute($key, [ $value ]); >> | ||
| 261 | |||
| 262 | =item C<< $o->has_attribute($key); >> | ||
| 263 | |||
| 264 | =item C<< $o->attribute($key); >> | ||
| 265 | |||
| 266 | =item C<< $o->delete_attribute($key); >> | ||
| 267 | |||
| 268 | An order can have "attributes" associated to keep track of its status | ||
| 269 | in various strategies. has_attribute returns only true if the attribute | ||
| 270 | exists (whatever its value is). attribute returns the attribute value if | ||
| 271 | it exists or undef otherwise. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | sub set_attribute { | ||
| 275 | my ($self, $key, $value) = @_; | ||
| 276 | $value = 1 if (! defined($value)); | ||
| 277 | $self->{'attributes'}{$key} = $value; | ||
| 278 | } | ||
| 279 | sub has_attribute { | ||
| 280 | my ($self, $key) = @_; | ||
| 281 | return 1 if (exists $self->{'attributes'}{$key}); | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | sub attribute { | ||
| 285 | my ($self, $key) = @_; | ||
| 286 | if (exists $self->{'attributes'}{$key}) | ||
| 287 | { | ||
| 288 | return $self->{'attributes'}{$key}; | ||
| 289 | } | ||
| 290 | return undef; | ||
| 291 | } | ||
| 292 | sub delete_attribute { | ||
| 293 | my ($self, $key) = @_; | ||
| 294 | delete $self->{'attributes'}{$key}; | ||
| 295 | } | ||
| 296 | |||
| 297 | =item C<< $o->set_timeframe($timeframe) >> | ||
| 298 | |||
| 299 | =item C<< $o->timeframe() >> | ||
| 300 | |||
| 301 | Set and return the timeframe associated to this order. | ||
| 302 | |||
| 303 | =cut | ||
| 304 | sub set_timeframe { $_[0]->{'timeframe'} = $_[1] } | ||
| 305 | sub timeframe { $_[0]->{'timeframe'} } | ||
| 306 | |||
| 307 | =item C<< $o->set_marged() >> | ||
| 308 | |||
| 309 | =item C<< $o->set_not_marged() >> | ||
| 310 | |||
| 311 | =item C<< $o->is_marged() >> | ||
| 312 | |||
| 313 | A marged order will not cost cash since the cash is "rented" until | ||
| 314 | the position is closed. | ||
| 315 | |||
| 316 | =cut | ||
| 317 | sub set_marged { $_[0]->{'marged'} = 1 } | ||
| 318 | sub set_not_marged { $_[0]->{'marged'} = 0 } | ||
| 319 | sub is_marged { defined($_[0]->{'marged'}) && $_[0]->{'marged'} } | ||
| 320 | |||
| 321 | =item C<< $o->is_executed($calc, $i) >> | ||
| 322 | |||
| 323 | Returns the price of execution if the order has been executed. Otherwise | ||
| 324 | returns 0. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | sub is_executed { | ||
| 328 | my ($self, $calc, $i) = @_; | ||
| 329 | |||
| 330 | my $prices = $calc->prices->at($i); | ||
| 331 | my $price = -1; | ||
| 332 | |||
| 333 | if ($self->is_type_market_price) { | ||
| 334 | # Market Price | ||
| 335 | $price = $prices->[$FIRST]; | ||
| 336 | } elsif ($self->is_type_theoric_at_open) { | ||
| 337 | # Market Open | ||
| 338 | $price = $prices->[$OPEN]; | ||
| 339 | } elsif ($self->is_type_theoric_at_high) { | ||
| 340 | # Market High | ||
| 341 | $price = $prices->[$HIGH]; | ||
| 342 | } elsif ($self->is_type_theoric_at_low) { | ||
| 343 | # Market Low | ||
| 344 | $price = $prices->[$LOW]; | ||
| 345 | } elsif ($self->is_type_theoric_at_close) { | ||
| 346 | # Market Close | ||
| 347 | $price = $prices->[$CLOSE]; | ||
| 348 | } elsif ($self->is_type_theoric_at_signal) { | ||
| 349 | # At Signal Closing Price | ||
| 350 | $price = $calc->prices->at($i - 1)->[$CLOSE] if ($i >= 1); | ||
| 351 | } elsif ($self->is_type_limited) { | ||
| 352 | # At limited price | ||
| 353 | if (($self->price >= $prices->[$LOW]) && | ||
| 354 | ($self->price <= $prices->[$HIGH])) | ||
| 355 | { | ||
| 356 | $price = $self->price; | ||
| 357 | } else | ||
| 358 | { | ||
| 359 | if ($self->is_buy_order && ($self->price > $prices->[$HIGH])) | ||
| 360 | { | ||
| 361 | $price = $prices->[$HIGH]; | ||
| 362 | } elsif ($self->is_sell_order && ($self->price < $prices->[$LOW])) | ||
| 363 | { | ||
| 364 | $price = $prices->[$LOW]; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | } elsif ($self->is_type_stop) { | ||
| 368 | # On stop (conditional order) | ||
| 369 | if (($self->is_buy_order) && | ||
| 370 | ($prices->[$HIGH] >= $self->price)) | ||
| 371 | { | ||
| 372 | $price = ($prices->[$FIRST] > $self->price) ? | ||
| 373 | $prices->[$FIRST] : $self->price; | ||
| 374 | } elsif (($self->is_sell_order) && | ||
| 375 | ($prices->[$LOW] <= $self->price)) | ||
| 376 | { | ||
| 377 | $price = ($prices->[$FIRST] < $self->price) ? | ||
| 378 | $prices->[$FIRST] : $self->price; | ||
| 379 | } | ||
| 380 | } elsif ($self->is_type_stop_limited) { | ||
| 381 | # On stop (conditional order) | ||
| 382 | if (($self->is_buy_order) && | ||
| 383 | ($prices->[$HIGH] >= $self->price) && | ||
| 384 | ($prices->[$LOW] <= $self->second_price)) | ||
| 385 | { | ||
| 386 | $price = ($prices->[$HIGH] < $self->second_price) ? | ||
| 387 | $prices->[$HIGH] : $self->second_price; | ||
| 388 | } elsif (($self->is_sell_order) && | ||
| 389 | ($prices->[$LOW] <= $self->price) && | ||
| 390 | ($prices->[$HIGH] >= $self->second_price)) | ||
| 391 | { | ||
| 392 | $price = ($prices->[$LOW] > $self->second_price) ? | ||
| 393 | $prices->[$LOW] : $self->second_price; | ||
| 394 | } | ||
| 395 | } else { | ||
| 396 | die "Order type $self->{'type'} not managed by is_executed\n"; | ||
| 397 | } | ||
| 398 | |||
| 399 | $price = "0E0" if (! $price); # Return 0 but true | ||
| 400 | $price = "0" if ($price == -1); # Return false | ||
| 401 | |||
| 402 | return $price; | ||
| 403 | } | ||
| 404 | |||
| 405 | =pod | ||
| 406 | |||
| 407 | =back | ||
| 408 | |||
| 409 | =cut | ||
| 410 | 1; |