GeniusTrader/Portfolio/Position.pm untested

filter: Statementstmt 0.0% 0 / 199 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 51 Podpod 0.0% 0 / 48 totaltotal 0.0% 0 / 426 CC 38 SCAR 38.8
CC
38
Cov
0%
apply_pending_orders
210.0
apply_order
90.0
set_stop
30.0
CRAP 48.5
1 package Finance::GeniusTrader::Portfolio::Position;
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 our @ISA = qw(Finance::GeniusTrader::Serializable);
9  
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::Position - An open position within a portfolio
17  
18 =head1 DESCRIPTION
19  
20 =head2 Internal structure
21  
22 {
23 "long" => 1, # 1:Long 0:Short
24 "code" => "13000",
25 "quantity" => 100,
26 "initial_quantity" => 100, # Quantity when position was opened
27 "open_price" => 12.4, # Price when position has been taken
28 "close_price" => 13.2, # Prices when position has been closed
29 "source" => "Trend", # Which trading system opened the position
30 "open_date" => "2001-07-01",
31 "close_date" => "2001-07-04",
32 "stop" => 12 # Stop is at 12
33 }
34  
35 =head2 Functions
36  
37 =over
38  
39 =item C<< $p = Finance::GeniusTrader::Portfolio::Position->new($code, $source, $date) >>
40  
41 =cut
42 sub new {
43 my $type = shift;
44 my $class = ref($type) || $type;
45 my ($code, $source, $date) = @_;
46  
47 my $self = { "details" => [], "pending_orders" => [], "quantity" => 0,
48 "attributes" => {} };
49 bless $self, $class;
50  
51 $self->set_code($code) if (defined($code));
52 $self->set_source($source) if (defined($source));
53 $self->set_open_date($date) if (defined($date));
54
55 return $self;
56 }
57  
58 =item C<< $p->set_long() >>
59  
60 =item C<< $p->set_short() >>
61  
62 =item C<< $p->is_long() >>
63  
64 =item C<< $p->is_short() >>
65  
66  
67 =cut
68 sub set_long { $_[0]->{'long'} = 1 }
69 sub set_short { $_[0]->{'long'} = 0 }
70 sub is_long { $_[0]->{'long'} == 1 }
71 sub is_short { $_[0]->{'long'} == 0 }
72  
73 =item C<< $p->set_code($code) >>
74  
75 =item C<< $p->code() >>
76  
77  
78 =cut
79 sub set_code {
80 my ($self, $code) = @_;
81 $self->{'code'} = $code;
82 }
83 sub code { $_[0]->{'code'} }
84  
85 =item C<< $p->set_quantity($quantity) >>
86  
87 =item C<< $p->quantity() >>
88  
89 =item C<< $p->set_initial_quantity($quantity) >>
90  
91 =item C<< $p->initial_quantity() >>
92  
93  
94 =cut
95 sub set_quantity {
96 my ($self, $q) = @_;
97 $self->{'quantity'} = $q;
98 }
99 sub quantity { $_[0]->{'quantity'} }
100 sub set_initial_quantity {
101 my ($self, $q) = @_;
102 $self->{'initial_quantity'} = $q;
103 }
104 sub initial_quantity { $_[0]->{'initial_quantity'} }
105  
106 =item C<< $p->set_open_price($price) >>
107  
108 =item C<< $p->open_price() >>
109  
110 =item C<< $p->set_close_price($price) >>
111  
112 =item C<< $p->close_price() >>
113  
114 =cut
115 sub set_open_price {
116 my ($self, $price) = @_;
117 $self->{'open_price'} = $price;
118 }
119 sub set_close_price {
120 my ($self, $price) = @_;
121 $self->{'close_price'} = $price;
122 }
123 sub open_price { $_[0]->{'open_price'} }
124 sub close_price { $_[0]->{'close_price'} }
125  
126 =item C<< $p->set_source($source) >>
127  
128 =item C<< $p->source() >>
129  
130  
131 =cut
132 sub set_source {
133 my ($self, $source) = @_;
134 $self->{'source'} = $source;
135 }
136 sub source { $_[0]->{'source'} }
137  
138 =item C<< $p->set_open_date($date) >>
139  
140 =item C<< $p->open_date() >>
141  
142 =item C<< $p->set_close_date($date) >>
143  
144 =item C<< $p->close_date() >>
145  
146  
147 =cut
148 sub set_open_date {
149 my ($self, $date) = @_;
150 $self->{'open_date'} = $date;
151 }
152 sub open_date { $_[0]->{'open_date'} }
153 sub set_close_date {
154 my ($self, $date) = @_;
155 $self->{'close_date'} = $date;
156 }
157 sub close_date { $_[0]->{'close_date'} }
158  
159 =item C<< $p->set_id($id) >>
160  
161 =item C<< $p->id() >>
162  
163  
164 =cut
165 sub set_id {
166 my ($self, $id) = @_;
167 $self->{'id'} = $id;
168 }
169 sub id { $_[0]->{'id'} }
170  
171 =item C<< $p->set_stop($price) >>
172  
173 =item C<< $p->update_stop($price) >>
174  
175 =item C<< $p->force_stop($price) >>
176  
177 =item C<< $p->stop() >>
178  
179 set_stop and update_stop modifies the stop level but it won't let you
180 further the stop, you can only bring it nearer. If you want to further
181 the stop level use force_stop.
182  
183 =cut
184 sub set_stop {
185 my ($self, $price) = @_;
186 if (! (defined($self->{'stop'}) && $self->{'stop'}))
187 {
188 $self->{'stop'} = $price;
189 return;
190 }
191 if ($self->is_long) {
192 if ($price > $self->{'stop'})
193 {
194 $self->{'stop'} = $price;
195 }
196 } else {
197 if ($price < $self->{'stop'})
198 {
199 $self->{'stop'} = $price;
200 }
201 }
202 }
203 sub update_stop { set_stop(@_) }
204 sub force_stop {
205 my ($self, $price) = @_;
206 $self->{'stop'} = $price;
207 }
208 sub stop { $_[0]->{'stop'} }
209  
210 =item C<< $p->set_attribute($key, [ $value ]); >>
211  
212 =item C<< $p->has_attribute($key); >>
213  
214 =item C<< $p->attribute($key); >>
215  
216 =item C<< $p->delete_attribute($key); >>
217  
218 A position can have "attributes" associated to keep track of its status
219 in various strategies. has_attribute returns only true if the attribute
220 exists (whatever its value is). attribute returns the attribute value if
221 it exists or undef otherwise.
222  
223 =cut
224 sub set_attribute {
225 my ($self, $key, $value) = @_;
226 $value = 1 if (! defined($value));
227 $self->{'attributes'}{$key} = $value;
228 }
229 sub has_attribute {
230 my ($self, $key) = @_;
231 return 1 if (exists $self->{'attributes'}{$key});
232 return 0;
233 }
234 sub attribute {
235 my ($self, $key) = @_;
236 if (exists $self->{'attributes'}{$key})
237 {
238 return $self->{'attributes'}{$key};
239 }
240 return undef;
241 }
242 sub delete_attribute {
243 my ($self, $key) = @_;
244 delete $self->{'attributes'}{$key};
245 }
246  
247 =item C<< $p->set_timeframe($timeframe) >>
248  
249 =item C<< $p->timeframe() >>
250  
251 Set and return the timeframe associated to this position.
252  
253 =cut
254 sub set_timeframe { $_[0]->{'timeframe'} = $_[1] }
255 sub timeframe { $_[0]->{'timeframe'} }
256  
257 =item C<< $p->set_marged() >>
258  
259 =item C<< $p->set_not_marged() >>
260  
261 =item C<< $p->is_marged() >>
262  
263 A marged position is constitued of shares that have been "rented" (or
264 bought with rented cash).
265  
266 =cut
267 sub set_marged { $_[0]->{'marged'} = 1 }
268 sub set_not_marged { $_[0]->{'marged'} = 0 }
269 sub is_marged { defined($_[0]->{'marged'}) && $_[0]->{'marged'} }
270  
271 =item C<< $p->apply_order($order, $price, $date) >>
272  
273 Update the position with the corresponding order. The order has been executed at the given date
274 and at the given price.
275  
276 =cut
277 sub apply_order {
278 my ($self, $order, $price, $date) = @_;
279  
280 #WAR# WARN "order quantity is positive" unless ($order->quantity > 0);
281  
282 if (! defined($price)) {
283 $price = $order->price();
284 }
285  
286 #ERR# ERROR "applying an order without price" unless (defined($price));
287  
288 # Store the modification to the position
289 my $history = Finance::GeniusTrader::Portfolio::Order->new;
290 if ($order->is_buy_order) {
291 $history->set_buy_order;
292 } else {
293 $history->set_sell_order;
294 }
295 $history->set_submission_date($date);
296 $history->set_price($price);
297 $history->set_quantity($order->quantity);
298 $history->set_type($order->type);
299 $history->set_timeframe($self->timeframe);
300
301 push @{$self->{'details'}}, $history;
302  
303 # Update the position
304 if ($self->quantity) {
305 if ($self->is_long) {
306 if ($order->is_buy_order) {
307 $self->{'quantity'} += $order->quantity;
308 } else {
309 $self->{'quantity'} -= $order->quantity;
310 }
311 } else {
312 if ($order->is_sell_order) {
313 $self->{'quantity'} += $order->quantity;
314 } else {
315 $self->{'quantity'} -= $order->quantity;
316 }
317 }
318 } else {
319 $self->{'long'} = ($order->is_buy_order) ? 1 : 0;
320 $self->set_quantity($order->quantity);
321 $self->set_initial_quantity($order->quantity);
322 $self->set_open_price($price);
323 $self->set_open_date($date);
324 }
325  
326 #ERR# ERROR "position quantity must stay above zero" unless ($self->quantity >= 0);
327  
328 # If position is closed, remove it of open_positions and add to
329 # history, and clear the orders on that position
330 if ($self->quantity == 0)
331 {
332 $self->set_close_date($date);
333 $self->{'pending_orders'} = [];
334 }
335
336 return 1;
337 }
338  
339 =item C<< $p->apply_pending_orders($calc, $i) >>
340  
341  
342 =cut
343 sub apply_pending_orders {
344 my ($self, $calc, $i) = @_ ;
345
346 # Try to apply stop defined as orders
347 foreach (grep { $_->is_type_stop &&
348 (($self->is_long && $_->is_sell_order) ||
349 ($self->is_short && $_->is_buy_order))
350 } ($self->list_pending_orders))
351 {
352 next if (! defined($_));
353  
354 my $price = $_->is_executed($calc, $i);
355 if ($price)
356 {
357 $self->apply_order($_, $price, $calc->prices->at($i)->[$DATE]);
358 $self->delete_order($_);
359 } else {
360 if ($_->discardable) {
361 $self->discard_order($_);
362 } else {
363 # Do not discard
364 }
365 }
366 }
367
368 # Try to apply the stop if there's something left to stop
369 if (defined($self->stop) && $self->stop && $self->quantity)
370 {
371 my $order = Finance::GeniusTrader::Portfolio::Order->new;
372 if ($self->is_long)
373 {
374 $order->set_sell_order;
375 } else {
376 $order->set_buy_order;
377 }
378 $order->set_type_stop;
379 $order->set_price($self->stop);
380 $order->set_quantity($self->quantity);
381  
382 my $apply_stop = 1;
383
384 if ($self->open_date eq $calc->prices->at($i)->[$DATE])
385 {
386 # First day, we can't always be sure how the stop has
387 # been managed
388  
389 # We're sure that the stop has been executed if the close
390 # price has broken the stop level (because the position has
391 # been opened before...)
392 if ($self->is_long)
393 {
394 if ($self->stop < $calc->prices->at($i)->[$LAST])
395 {
396 $apply_stop = 0;
397 }
398 } else {
399 if ($self->stop > $calc->prices->at($i)->[$LAST])
400 {
401 $apply_stop = 0;
402 }
403 }
404 }
405
406 if ($apply_stop && $order->is_executed($calc, $i))
407 {
408 $self->apply_order($order, $self->stop, $calc->prices->at($i)->[$DATE]);
409 return;
410 }
411 }
412
413 # Apply the other orders
414 foreach ($self->list_pending_orders)
415 {
416 next if (! defined($_));
417
418 my $price;
419 if ($price = $_->is_executed($calc, $i))
420 {
421 $self->apply_order($_, $price, $calc->prices->at($i)->[$DATE]);
422 $self->delete_order($_);
423 } else {
424 if ($_->discardable) {
425 $self->discard_order($_);
426 } else {
427 # Do not discard
428 }
429 }
430 }
431 return;
432 }
433  
434 =item C<< $p->add_order($order) >>
435  
436 Add a new pending order to the position.
437  
438 =cut
439 sub add_order {
440 my ($self, $order) = @_;
441  
442 $order->set_id(scalar @{$self->{'pending_orders'}});
443 push @{$self->{'pending_orders'}}, $order;
444
445 return $order->{'id'};
446 }
447  
448 =item C<< $p->delete_order($order) >>
449  
450  
451 =cut
452 sub delete_order {
453 my ($self, $order) = @_;
454  
455 $self->{'pending_orders'}[$order->id] = undef;
456 return;
457 }
458  
459 =item C<< $p->discard_order($order) >>
460  
461  
462 =cut
463 sub discard_order {
464 delete_order(@_);
465 }
466  
467 =item C<< $p->list_pending_orders() >>
468  
469 Returns the list of pending orders on the position. Those orders have not
470 yet been executed.
471  
472 =cut
473 sub list_pending_orders {
474 my ($self) = @_;
475  
476 return grep { defined($_) } @{$self->{'pending_orders'}};
477 }
478  
479 =item C<< $p->list_detailed_orders() >>
480  
481 Returns the list of detailed orders on the position. Those orders
482 have already been executed.
483  
484 =cut
485 sub list_detailed_orders {
486 my ($self) = @_;
487  
488 return grep { defined($_) } @{$self->{'details'}};
489 }
490  
491  
492 =item C<< $p->is_open() >>
493  
494 Returns true if the position is still open (ie if quantity != 0)
495  
496 =cut
497 sub is_open {
498 my ($self) = @_;
499  
500 my $bool = ($self->quantity > 0) ? 1 : 0;
501 return $bool;
502 }
503  
504 =item C<< $p->set_intent_to_close() >>
505  
506 Mark the position as being in the process of being closed. This will
507 let the system detect new opportunities as soon as possible.
508  
509 =cut
510 sub set_intent_to_close {
511 my ($self) = @_;
512 $self->{'intend_to_close'} = 1;
513 return;
514 }
515  
516 =item C<< $p->set_no_intent_to_close() >>
517  
518 DeMark the position as being in the process of being closed. This will
519 let the system manage the position without knowing that some orders
520 have been placed to close the position (ie those orders may or may not
521 be executed).
522  
523 =cut
524 sub set_no_intent_to_close {
525 my ($self) = @_;
526 $self->{'intend_to_close'} = 0;
527 return;
528 }
529  
530 =item C<< $p->being_closed() >>
531  
532 Returns true if the position is in the process of beeing closed.
533  
534 =cut
535 sub being_closed {
536 my ($self) = @_;
537 if (defined($self->{'intend_to_close'}) &&
538 $self->{'intend_to_close'})
539 {
540 return 1;
541 }
542 return 0;
543 }
544  
545 =item C<< $p->stats($portfolio, [ $quantity_factor ]) >>
546  
547 Calculate some statistics about the position.
548  
549 =cut
550 sub stats {
551 my ($self, $portfolio, $quantity_factor) = @_;
552
553 #WAR# WARN "first arg of position->stats must be a portfolio" unless (ref($portfolio) =~ /Finance::GeniusTrader::Portfolio/);
554
555 if (! defined($quantity_factor))
556 {
557 $quantity_factor = 1;
558 }
559
560 my ($quantity_sold, $quantity_bought, $bought, $sold, $nb_sell_orders,
561 $nb_buy_orders, $cost) = (0, 0, 0, 0, 0, 0, 0);
562 foreach (@{$self->{'details'}})
563 {
564 next if (! defined($_));
565
566
567 my $sum = $quantity_factor * $_->quantity * $_->price;
568 if ($_->is_buy_order) {
569 $nb_buy_orders++;
570 $bought += $sum;
571 $quantity_bought += $quantity_factor * $_->quantity;
572 } elsif ($_->is_sell_order) {
573 $nb_sell_orders++;
574 $sold += $sum;
575 $quantity_sold += $quantity_factor * $_->quantity;
576 }
577 $cost += $portfolio->get_order_cost($_);
578 }
579
580 return {
581 "bought" => $bought,
582 "sold" => $sold,
583 "quantity" => $quantity_bought,
584 "quantity_bought" => $quantity_bought,
585 "quantity_sold" => $quantity_sold,
586 "nb_orders" => $nb_buy_orders + $nb_sell_orders,
587 "nb_buy_orders" => $nb_buy_orders,
588 "nb_sell_orders" => $nb_sell_orders,
589 "cost" => $cost
590 };
591 }
592  
593 =pod
594  
595 =back
596  
597 =cut
598 1;