forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimal_positions.py
260 lines (197 loc) · 9.4 KB
/
optimal_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
253
254
255
256
257
258
259
260
from copy import copy
import pandas as pd
from sysdata.data_blob import dataBlob
from sysdata.production.optimal_positions import optimalPositionData
from sysobjects.production.optimal_positions import (
listOfOptimalPositionsAcrossInstrumentStrategies,
baseOptimalPosition,
listOfOptimalAndCurrentPositionsAcrossInstrumentStrategies,
instrumentStrategyAndOptimalPosition,
)
from sysobjects.production.tradeable_object import instrumentStrategy
from sysproduction.data.generic_production_data import productionDataLayerGeneric
from sysproduction.data.positions import diagPositions
from sysproduction.data.config import (
get_list_of_stale_instruments,
get_list_of_stale_strategies,
)
from sysproduction.data.production_data_objects import (
get_class_for_data_type,
OPTIMAL_POSITION_DATA,
)
class dataOptimalPositions(productionDataLayerGeneric):
def _add_required_classes_to_data(self, data) -> dataBlob:
data.add_class_object(get_class_for_data_type(OPTIMAL_POSITION_DATA))
return data
def get_list_of_current_optimal_positions_for_strategy_name(
self, strategy_name: str
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
all_optimal_positions = self.get_list_of_optimal_positions()
optimal_positions_for_strategy = all_optimal_positions.filter_by_strategy(
strategy_name
)
return optimal_positions_for_strategy
def get_list_of_instruments_for_strategy_with_optimal_position(
self, strategy_name: str, raw_positions=False
) -> list:
if raw_positions:
use_strategy_name = strategy_name_with_raw_tag(strategy_name)
else:
use_strategy_name = strategy_name
list_of_instruments = self.db_optimal_position_data.get_list_of_instruments_for_strategy_with_optimal_position(
use_strategy_name
)
return list_of_instruments
def get_list_of_strategies_with_optimal_position(self) -> list:
list_of_strategies = (
self.db_optimal_position_data.list_of_strategies_with_optimal_position()
)
list_of_strategies = remove_raw_strategies(list_of_strategies)
return list_of_strategies
def get_current_optimal_position_for_instrument_strategy(
self, instrument_strategy: instrumentStrategy, raw_positions=False
) -> baseOptimalPosition:
if raw_positions:
use_instrument_strategy = instrument_strategy_with_raw_tag(
instrument_strategy
)
else:
use_instrument_strategy = instrument_strategy
current_optimal_position_entry = self.db_optimal_position_data.get_current_optimal_position_for_instrument_strategy(
use_instrument_strategy
)
return current_optimal_position_entry
def get_optimal_position_as_df_for_instrument_strategy(
self, instrument_strategy: instrumentStrategy
) -> pd.DataFrame:
df_object = self.db_optimal_position_data.get_optimal_position_as_df_for_instrument_strategy(
instrument_strategy
)
return df_object
def update_optimal_position_for_instrument_strategy(
self,
instrument_strategy: instrumentStrategy,
position_entry: baseOptimalPosition,
raw_positions=False,
):
if raw_positions:
use_instrument_strategy = instrument_strategy_with_raw_tag(
instrument_strategy
)
else:
use_instrument_strategy = instrument_strategy
self.db_optimal_position_data.update_optimal_position_for_instrument_strategy(
use_instrument_strategy, position_entry
)
def get_list_of_optimal_positions(
self,
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
## drop stale markets
list_of_optimal_positions_and_instrument_strategies = (
self.db_optimal_position_data.get_list_of_optimal_positions()
)
list_of_optimal_positions_and_instrument_strategies = (
remove_raw_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies
)
)
list_of_optimal_positions_and_instrument_strategies = remove_stale_strategies_and_instruments_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies=list_of_optimal_positions_and_instrument_strategies,
)
return list_of_optimal_positions_and_instrument_strategies
def get_pd_of_position_breaks(self) -> pd.DataFrame:
optimal_and_current = self.get_list_of_optimal_and_current_positions()
optimal_and_current_as_pd = optimal_and_current.as_pd_with_breaks()
return optimal_and_current_as_pd
def get_list_of_optimal_position_breaks(self) -> list:
opt_positions = self.get_pd_of_position_breaks()
with_breaks = opt_positions[opt_positions.breaks]
items_with_breaks = list(with_breaks.index)
return items_with_breaks
def get_list_of_optimal_and_current_positions(
self,
) -> listOfOptimalAndCurrentPositionsAcrossInstrumentStrategies:
optimal_positions = self.get_list_of_optimal_positions()
position_data = diagPositions(self.data)
current_positions = (
position_data.get_all_current_strategy_instrument_positions()
)
optimal_and_current = optimal_positions.add_positions(current_positions)
return optimal_and_current
@property
def db_optimal_position_data(self) -> optimalPositionData:
return self.data.db_optimal_position
POST_TAG_FOR_RAW_OPTIMAL_POSITION = "_raw"
def remove_raw_strategies(list_of_strategies: list) -> list:
list_of_strategies = [
strategy_name
for strategy_name in list_of_strategies
if is_not_raw_strategy(strategy_name)
]
return list_of_strategies
def is_not_raw_strategy(strategy_name: str) -> bool:
return not is_raw_strategy(strategy_name)
def is_raw_strategy(strategy_name: str) -> bool:
return strategy_name.endswith(POST_TAG_FOR_RAW_OPTIMAL_POSITION)
def remove_raw_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies: listOfOptimalPositionsAcrossInstrumentStrategies,
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
list_of_optimal_positions_and_instrument_strategies = [
optimal_position_and_instrument_strategy
for optimal_position_and_instrument_strategy in list_of_optimal_positions_and_instrument_strategies
if is_not_raw_optimal_position_and_instrument_strategy(
optimal_position_and_instrument_strategy
)
]
return listOfOptimalPositionsAcrossInstrumentStrategies(
list_of_optimal_positions_and_instrument_strategies
)
def is_not_raw_optimal_position_and_instrument_strategy(
optimal_position_and_instrument_strategy: instrumentStrategyAndOptimalPosition,
) -> bool:
return is_not_raw_instrument_strategy(
optimal_position_and_instrument_strategy.instrument_strategy
)
def is_not_raw_instrument_strategy(instrument_strategy: instrumentStrategy) -> bool:
return is_not_raw_strategy(instrument_strategy.strategy_name)
def instrument_strategy_with_raw_tag(
instrument_strategy: instrumentStrategy,
) -> instrumentStrategy:
original_strategy_name = copy(instrument_strategy.strategy_name)
strategy_name = strategy_name_with_raw_tag(original_strategy_name)
new_instrument_strategy = instrumentStrategy(
strategy_name=strategy_name, instrument_code=instrument_strategy.instrument_code
)
return new_instrument_strategy
def strategy_name_with_raw_tag(strategy_name: str) -> str:
return strategy_name + POST_TAG_FOR_RAW_OPTIMAL_POSITION
def remove_stale_strategies_and_instruments_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies: listOfOptimalPositionsAcrossInstrumentStrategies,
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
filtered_list = remove_stale_strategies_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies=list_of_optimal_positions_and_instrument_strategies,
)
twice_filtered_list = remove_stale_instruments_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies=filtered_list
)
return twice_filtered_list
def remove_stale_strategies_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies: listOfOptimalPositionsAcrossInstrumentStrategies,
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
list_of_stale_strategies = get_list_of_stale_strategies()
new_list = (
list_of_optimal_positions_and_instrument_strategies.filter_removing_strategies(
list_of_stale_strategies
)
)
return new_list
def remove_stale_instruments_from_list_of_optimal_positions_and_instrument_strategies(
list_of_optimal_positions_and_instrument_strategies: listOfOptimalPositionsAcrossInstrumentStrategies,
) -> listOfOptimalPositionsAcrossInstrumentStrategies:
list_of_stale_instruments = get_list_of_stale_instruments()
new_list = (
list_of_optimal_positions_and_instrument_strategies.filter_removing_instruments(
list_of_stale_instruments
)
)
return new_list