forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoric_strategy_positions.py
252 lines (214 loc) · 8.95 KB
/
historic_strategy_positions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from sysdata.base_data import baseData
import pandas as pd
from syscore.exceptions import missingData
from syscore.constants import arg_not_supplied
from sysobjects.production.positions import (
instrumentStrategyPosition,
listOfInstrumentStrategyPositions,
)
from sysobjects.production.tradeable_object import (
listOfInstrumentStrategies,
instrumentStrategy,
)
import datetime
class strategyPositionData(baseData):
"""
Store and retrieve the instrument positions assigned to a particular strategy
We store the type of list in the data
"""
def __repr__(self):
return "straegyPositionData object"
def get_current_position_for_instrument_strategy_object(
self, instrument_strategy: instrumentStrategy
) -> int:
try:
position_series = (
self.get_position_as_series_for_instrument_strategy_object(
instrument_strategy
)
)
except missingData:
return 0
if len(position_series) == 0:
return 0
return position_series.iloc[-1]
def update_position_for_instrument_strategy_object(
self,
instrument_strategy: instrumentStrategy,
position: int,
date: datetime.datetime = arg_not_supplied,
):
if date is arg_not_supplied:
date = datetime.datetime.now()
try:
self._update_position_for_instrument_strategy_object_with_date(
instrument_strategy=instrument_strategy,
position=position,
date_index=date,
)
except Exception as e:
self.log.critical(
"Error %s when updating position for %s with %s"
% (str(e), str(instrument_strategy), str(position))
)
def get_list_of_strategies_and_instruments_with_positions(
self, ignore_zero_positions: bool = True
) -> listOfInstrumentStrategies:
list_of_instrument_strategies = self.get_list_of_instrument_strategies()
if ignore_zero_positions:
list_of_instrument_strategies = [
instrument_strategy
for instrument_strategy in list_of_instrument_strategies
if self.get_current_position_for_instrument_strategy_object(
instrument_strategy
)
!= 0
]
list_of_instrument_strategies = listOfInstrumentStrategies(
list_of_instrument_strategies
)
return list_of_instrument_strategies
def get_list_of_instruments_for_strategy_with_position(
self, strategy_name, ignore_zero_positions=True
) -> list:
list_of_instrument_strategies = (
self.get_list_of_strategies_and_instruments_with_positions(
ignore_zero_positions=ignore_zero_positions
)
)
list_of_instruments = (
list_of_instrument_strategies.get_list_of_instruments_for_strategy(
strategy_name
)
)
return list_of_instruments
def get_list_of_strategies_with_positions(self) -> list:
list_of_instrument_strategies = (
self.get_list_of_strategies_and_instruments_with_positions(
ignore_zero_positions=True
)
)
list_of_strategies = list_of_instrument_strategies.get_list_of_strategies()
return list_of_strategies
def delete_last_position_for_instrument_strategy_object(
self, instrument_strategy: instrumentStrategy, are_you_sure: bool = False
):
if are_you_sure:
self._delete_last_position_for_instrument_strategy_object_without_checking(
instrument_strategy=instrument_strategy
)
else:
self.log.warning("Have to be sure to delete last position")
def get_all_current_positions_as_df(self) -> pd.DataFrame:
return (
self.get_all_current_positions_as_list_with_instrument_objects().as_pd_df()
)
def get_all_current_positions_as_list_with_instrument_objects(
self,
) -> listOfInstrumentStrategyPositions:
"""
Current positions are returned in a different class
:return: listOfInstrumentStrategyPositions
"""
list_of_instrument_strategies = self.get_list_of_instrument_strategies()
current_positions = []
for instrument_strategy in list_of_instrument_strategies:
position = self.get_current_position_for_instrument_strategy_object(
instrument_strategy
)
if position == 0:
continue
position_object = instrumentStrategyPosition(position, instrument_strategy)
current_positions.append(position_object)
list_of_current_position_objects = listOfInstrumentStrategyPositions(
current_positions
)
return list_of_current_position_objects
def _delete_last_position_for_instrument_strategy_object_without_checking(
self, instrument_strategy: instrumentStrategy
):
try:
current_series = self.get_position_as_series_for_instrument_strategy_object(
instrument_strategy
)
self._delete_last_position_for_instrument_strategy_object_without_checking_with_current_data(
instrument_strategy=instrument_strategy, current_series=current_series
)
except missingData:
## no existing data can't delete
self.log.warning(
"Can't delete last position for %s, as none present"
% str(instrument_strategy)
)
def _delete_last_position_for_instrument_strategy_object_without_checking_with_current_data(
self, instrument_strategy: instrumentStrategy, current_series: pd.Series
):
updated_series = current_series.drop(current_series.index[-1])
if len(updated_series) == 0:
self._delete_position_series_for_instrument_strategy_object_without_checking(
instrument_strategy
)
else:
self._write_updated_position_series_for_instrument_strategy_object(
instrument_strategy=instrument_strategy,
updated_series=updated_series,
)
def _update_position_for_instrument_strategy_object_with_date(
self,
instrument_strategy: instrumentStrategy,
position: int,
date_index: datetime.datetime,
):
new_position_series = pd.Series([position], index=[date_index])
try:
current_series = self.get_position_as_series_for_instrument_strategy_object(
instrument_strategy
)
self._update_position_for_instrument_strategy_object_with_date_and_existing_data(
instrument_strategy=instrument_strategy,
current_series=current_series,
new_position_series=new_position_series,
)
except missingData:
## no existing data
## no need to update, just write the new series
self._write_updated_position_series_for_instrument_strategy_object(
instrument_strategy=instrument_strategy,
updated_series=new_position_series,
)
def _update_position_for_instrument_strategy_object_with_date_and_existing_data(
self,
instrument_strategy: instrumentStrategy,
current_series: pd.Series,
new_position_series: pd.Series,
):
try:
assert new_position_series.index[0] > current_series.index[-1]
except:
error_msg = "Adding a position which is older than the last position!"
self.log.critical(error_msg)
raise Exception(error_msg)
updated_series = current_series._append(new_position_series)
self._write_updated_position_series_for_instrument_strategy_object(
instrument_strategy=instrument_strategy, updated_series=updated_series
)
def overwrite_position_series_for_instrument_strategy_without_checking(
self, instrument_strategy: instrumentStrategy, updated_series: pd.Series
):
self._write_updated_position_series_for_instrument_strategy_object(
instrument_strategy=instrument_strategy, updated_series=updated_series
)
def _write_updated_position_series_for_instrument_strategy_object(
self, instrument_strategy: instrumentStrategy, updated_series: pd.Series
):
raise NotImplementedError
def _delete_position_series_for_instrument_strategy_object_without_checking(
self, instrument_strategy: instrumentStrategy
):
raise NotImplementedError
def get_list_of_instrument_strategies(self) -> listOfInstrumentStrategies:
raise NotImplementedError
def get_position_as_series_for_instrument_strategy_object(
self, instrument_strategy: instrumentStrategy
) -> pd.Series:
raise NotImplementedError