4
4
import numpy as np
5
5
6
6
from tia .analysis .model .txn import Intent , Action
7
- from tia .analysis .model .interface import PlColumns as PL , PositionColumns as PC
7
+ from tia .analysis .model .interface import TxnPlColumns as TPL , PositionColumns as PC
8
8
from tia .util .decorator import lazy_property
9
9
10
10
@@ -22,57 +22,44 @@ class State(object):
22
22
23
23
24
24
class Position (object ):
25
- def __init__ (self , pid , dly_txn_pl_frame , trades , dly_txn_ret ):
25
+ def __init__ (self , pid , pl , trades , performance ):
26
26
"""
27
27
:param pid: position id
28
28
:param dly_txn_pl: daily transaction level DataFrame
29
29
:param trades: trade array
30
30
"""
31
31
self .pid = pid
32
- self .dly_txn_pl_frame = dly_txn_pl_frame
33
- self .dly_txn_ret = dly_txn_ret
32
+ self .pl = pl
33
+ self .performance = performance
34
34
self .trades = trades
35
- self .first = first = dly_txn_pl_frame .iloc [0 ]
36
- self .last = last = dly_txn_pl_frame .iloc [- 1 ]
37
- self .is_closed = is_closed = last [PL .TXN_INTENT ] == Intent .Close
38
- self .is_long = is_long = first [PL .TXN_ACTION ] == Action .Buy
35
+ self .first = first = pl . txn_frame .iloc [0 ]
36
+ self .last = last = pl . txn_frame .iloc [- 1 ]
37
+ self .is_closed = last [TPL .TXN_INTENT ] == Intent .Close
38
+ self .is_long = is_long = first [TPL .TXN_ACTION ] == Action .Buy
39
39
self .is_short = not is_long
40
- self .pid = first [PL .PID ]
41
- self .open_dt = first [PL .DT ]
42
- self .open_premium = first [PL .TXN_PREMIUM ]
43
- self .open_qty = first [PL .TXN_QTY ]
44
- self .open_px = first [PL .TXN_PX ]
45
- self .close_px = last [PL .TXN_PX ]
46
- self .close_dt = last [PL .DT ]
47
- cumpl = dly_txn_pl_frame [PL .PL ].cumsum ()
48
- self .pl = cumpl .iloc [- 1 ]
49
- self .pl_min = cumpl .min ()
50
- self .pl_max = cumpl .max ()
51
-
52
- rpath = (1. + self .dly_ret ).cumprod () - 1.
53
- self .ret = rpath .iloc [- 1 ]
54
- self .ret_min = rpath .min ()
55
- self .ret_max = rpath .max ()
56
- self .duration = len (dly_txn_pl_frame [PL .DT ].drop_duplicates ())
40
+ self .pid = first [TPL .PID ]
41
+ self .open_dt = first [TPL .DT ]
42
+ self .open_premium = first [TPL .TXN_PREMIUM ]
43
+ self .open_qty = first [TPL .TXN_QTY ]
44
+ self .open_px = first [TPL .TXN_PX ]
45
+ self .close_px = last [TPL .TXN_PX ]
46
+ self .close_dt = last [TPL .DT ]
47
+ ltd = pl .ltd_txn
48
+ self .pl = ltd .iloc [- 1 ]
49
+ self .pl_min = ltd .min ()
50
+ self .pl_max = ltd .max ()
51
+
52
+ ltd = performance .ltd_txn
53
+ self .ret = ltd .iloc [- 1 ]
54
+ self .ret_min = ltd .min ()
55
+ self .ret_max = ltd .max ()
56
+ # TODO - should use a time delta and determine the number of days
57
+ self .duration = len (pl .ltd_txn_frame [TPL .DT ].drop_duplicates ())
57
58
self .ntxns = len (trades )
58
59
59
60
state = property (lambda self : self .is_closed and State .Closed or State .Open )
60
61
side = property (lambda self : self .is_long and Side .Long or Side .Short )
61
62
62
- @property
63
- def dly_ret (self ):
64
- ltd = (1. + self .dly_txn_ret ).cumprod ()
65
- ltd .index = self .dly_txn_pl_frame [PL .DT ]
66
- dly = ltd .groupby (lambda x : x ).apply (lambda x : x [- 1 ])
67
- dly .iloc [1 :] = dly .pct_change ()[1 :]
68
- dly .iloc [0 ] = dly .iloc [0 ] / 1. - 1.
69
- return dly
70
-
71
- @property
72
- def dly_pl (self ):
73
- return self .dly_txn_pl_frame [[PL .DT , PL .PL ]].set_index (PL .DT ).resample ('B' , how = 'sum' , kind = 'period' )[
74
- PL .PL ].dropna ()
75
-
76
63
def __repr__ (self ):
77
64
kwargs = {
78
65
'class' : self .__class__ .__name__ ,
@@ -103,11 +90,12 @@ def __len__(self):
103
90
def __getitem__ (self , pid ):
104
91
if pid == 0 :
105
92
raise ValueError ('pid must be non-zero' )
106
- trds = self .txns .get_pid_txns (pid )
107
- dly = self .txns .pl .dly_txn_frame
108
- sub = dly .ix [dly [PL .PID ] == pid ]
109
- rets = self .txns .rets .dly_txn [sub .index ]
110
- return Position (pid , sub , trds , rets )
93
+ txns = self .txns
94
+ trds = txns .get_pid_txns (pid )
95
+ pl = txns .pl .truncate (pid = pid )
96
+ mask = txns .pl .txn_details .get_pid_mask (pid )
97
+ performance = txns .performance .filter (mask )
98
+ return Position (pid , pl , trds , performance )
111
99
112
100
def __iter__ (self ):
113
101
for pid in self .pids :
0 commit comments