Skip to content

Commit af029da

Browse files
committed
refactored position
1 parent ad8d46b commit af029da

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

tia/analysis/model/pos.py

+32-44
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
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
88
from tia.util.decorator import lazy_property
99

1010

@@ -22,57 +22,44 @@ class State(object):
2222

2323

2424
class Position(object):
25-
def __init__(self, pid, dly_txn_pl_frame, trades, dly_txn_ret):
25+
def __init__(self, pid, pl, trades, performance):
2626
"""
2727
:param pid: position id
2828
:param dly_txn_pl: daily transaction level DataFrame
2929
:param trades: trade array
3030
"""
3131
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
3434
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
3939
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())
5758
self.ntxns = len(trades)
5859

5960
state = property(lambda self: self.is_closed and State.Closed or State.Open)
6061
side = property(lambda self: self.is_long and Side.Long or Side.Short)
6162

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-
7663
def __repr__(self):
7764
kwargs = {
7865
'class': self.__class__.__name__,
@@ -103,11 +90,12 @@ def __len__(self):
10390
def __getitem__(self, pid):
10491
if pid == 0:
10592
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)
11199

112100
def __iter__(self):
113101
for pid in self.pids:

0 commit comments

Comments
 (0)