|
| 1 | +from copy import copy |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from sysdata.data_blob import dataBlob |
| 6 | +from sysdata.mongodb.mongo_optimal_position_TO_DEPRECATE import mongoOptimalPositionData |
| 7 | +from sysdata.production.optimal_positions_TO_DEPRECATE import optimalPositionData |
| 8 | +from sysobjects.production.optimal_positions_TO_DEPRECATE import ( |
| 9 | + listOfOptimalPositionsAcrossInstrumentStrategies, |
| 10 | + simpleOptimalPosition, |
| 11 | + listOfOptimalAndCurrentPositionsAcrossInstrumentStrategies, |
| 12 | + instrumentStrategyAndOptimalPosition, |
| 13 | +) |
| 14 | +from sysobjects.production.tradeable_object import instrumentStrategy |
| 15 | +from sysproduction.data.generic_production_data import productionDataLayerGeneric |
| 16 | +from sysproduction.data.positions import diagPositions |
| 17 | + |
| 18 | + |
| 19 | +class dataOptimalPositions(productionDataLayerGeneric): |
| 20 | + def _add_required_classes_to_data(self, data) -> dataBlob: |
| 21 | + data.add_class_object(mongoOptimalPositionData) |
| 22 | + |
| 23 | + return data |
| 24 | + |
| 25 | + def get_list_of_current_optimal_positions_for_strategy_name( |
| 26 | + self, strategy_name: str |
| 27 | + ) -> listOfOptimalPositionsAcrossInstrumentStrategies: |
| 28 | + |
| 29 | + all_optimal_positions = self.get_list_of_optimal_positions() |
| 30 | + optimal_positions_for_strategy = all_optimal_positions.filter_by_strategy( |
| 31 | + strategy_name |
| 32 | + ) |
| 33 | + |
| 34 | + return optimal_positions_for_strategy |
| 35 | + |
| 36 | + def get_list_of_instruments_for_strategy_with_optimal_position( |
| 37 | + self, strategy_name: str, raw_positions=False |
| 38 | + ) -> list: |
| 39 | + if raw_positions: |
| 40 | + use_strategy_name = strategy_name_with_raw_tag(strategy_name) |
| 41 | + else: |
| 42 | + use_strategy_name = strategy_name |
| 43 | + |
| 44 | + list_of_instruments = self.db_optimal_position_data.get_list_of_instruments_for_strategy_with_optimal_position( |
| 45 | + use_strategy_name |
| 46 | + ) |
| 47 | + |
| 48 | + return list_of_instruments |
| 49 | + |
| 50 | + def get_list_of_strategies_with_optimal_position(self) -> list: |
| 51 | + |
| 52 | + list_of_strategies = ( |
| 53 | + self.db_optimal_position_data.list_of_strategies_with_optimal_position() |
| 54 | + ) |
| 55 | + list_of_strategies = remove_raw_strategies(list_of_strategies) |
| 56 | + |
| 57 | + return list_of_strategies |
| 58 | + |
| 59 | + def get_current_optimal_position_for_instrument_strategy( |
| 60 | + self, instrument_strategy: instrumentStrategy, raw_positions=False |
| 61 | + ) -> simpleOptimalPosition: |
| 62 | + |
| 63 | + if raw_positions: |
| 64 | + use_instrument_strategy = instrument_strategy_with_raw_tag( |
| 65 | + instrument_strategy |
| 66 | + ) |
| 67 | + else: |
| 68 | + use_instrument_strategy = instrument_strategy |
| 69 | + |
| 70 | + current_optimal_position_entry = self.db_optimal_position_data.get_current_optimal_position_for_instrument_strategy( |
| 71 | + use_instrument_strategy |
| 72 | + ) |
| 73 | + |
| 74 | + return current_optimal_position_entry |
| 75 | + |
| 76 | + def get_optimal_position_as_df_for_instrument_strategy( |
| 77 | + self, instrument_strategy: instrumentStrategy |
| 78 | + ) -> pd.DataFrame: |
| 79 | + |
| 80 | + df_object = self.db_optimal_position_data.get_optimal_position_as_df_for_instrument_strategy( |
| 81 | + instrument_strategy |
| 82 | + ) |
| 83 | + |
| 84 | + return df_object |
| 85 | + |
| 86 | + def update_optimal_position_for_instrument_strategy( |
| 87 | + self, |
| 88 | + instrument_strategy: instrumentStrategy, |
| 89 | + position_entry: simpleOptimalPosition, |
| 90 | + raw_positions=False, |
| 91 | + ): |
| 92 | + if raw_positions: |
| 93 | + use_instrument_strategy = instrument_strategy_with_raw_tag( |
| 94 | + instrument_strategy |
| 95 | + ) |
| 96 | + else: |
| 97 | + use_instrument_strategy = instrument_strategy |
| 98 | + |
| 99 | + self.db_optimal_position_data.update_optimal_position_for_instrument_strategy( |
| 100 | + use_instrument_strategy, position_entry |
| 101 | + ) |
| 102 | + |
| 103 | + def get_list_of_optimal_positions( |
| 104 | + self, |
| 105 | + ) -> listOfOptimalPositionsAcrossInstrumentStrategies: |
| 106 | + |
| 107 | + list_of_optimal_positions_and_instrument_strategies = ( |
| 108 | + self.db_optimal_position_data.get_list_of_optimal_positions() |
| 109 | + ) |
| 110 | + |
| 111 | + list_of_optimal_positions_and_instrument_strategies = ( |
| 112 | + remove_raw_from_list_of_optimal_positions_and_instrument_strategies( |
| 113 | + list_of_optimal_positions_and_instrument_strategies |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + return list_of_optimal_positions_and_instrument_strategies |
| 118 | + |
| 119 | + def get_pd_of_position_breaks(self) -> pd.DataFrame: |
| 120 | + optimal_and_current = self.get_list_of_optimal_and_current_positions() |
| 121 | + optimal_and_current_as_pd = optimal_and_current.as_pd_with_breaks() |
| 122 | + |
| 123 | + return optimal_and_current_as_pd |
| 124 | + |
| 125 | + def get_list_of_optimal_position_breaks(self) -> list: |
| 126 | + opt_positions = self.get_pd_of_position_breaks() |
| 127 | + with_breaks = opt_positions[opt_positions.breaks] |
| 128 | + items_with_breaks = list(with_breaks.index) |
| 129 | + |
| 130 | + return items_with_breaks |
| 131 | + |
| 132 | + def get_list_of_optimal_and_current_positions( |
| 133 | + self, |
| 134 | + ) -> listOfOptimalAndCurrentPositionsAcrossInstrumentStrategies: |
| 135 | + |
| 136 | + optimal_positions = self.get_list_of_optimal_positions() |
| 137 | + position_data = diagPositions(self.data) |
| 138 | + current_positions = ( |
| 139 | + position_data.get_all_current_strategy_instrument_positions() |
| 140 | + ) |
| 141 | + optimal_and_current = optimal_positions.add_positions(current_positions) |
| 142 | + |
| 143 | + return optimal_and_current |
| 144 | + |
| 145 | + @property |
| 146 | + def db_optimal_position_data(self) -> optimalPositionData: |
| 147 | + return self.data.db_optimal_position |
| 148 | + |
| 149 | + |
| 150 | +POST_TAG_FOR_RAW_OPTIMAL_POSITION = "_raw" |
| 151 | + |
| 152 | + |
| 153 | +def remove_raw_strategies(list_of_strategies: list) -> list: |
| 154 | + list_of_strategies = [ |
| 155 | + strategy_name |
| 156 | + for strategy_name in list_of_strategies |
| 157 | + if is_not_raw_strategy(strategy_name) |
| 158 | + ] |
| 159 | + |
| 160 | + return list_of_strategies |
| 161 | + |
| 162 | + |
| 163 | +def is_not_raw_strategy(strategy_name: str) -> bool: |
| 164 | + return not is_raw_strategy(strategy_name) |
| 165 | + |
| 166 | + |
| 167 | +def is_raw_strategy(strategy_name: str) -> bool: |
| 168 | + return strategy_name.endswith(POST_TAG_FOR_RAW_OPTIMAL_POSITION) |
| 169 | + |
| 170 | + |
| 171 | +def remove_raw_from_list_of_optimal_positions_and_instrument_strategies( |
| 172 | + list_of_optimal_positions_and_instrument_strategies: listOfOptimalPositionsAcrossInstrumentStrategies, |
| 173 | +) -> listOfOptimalPositionsAcrossInstrumentStrategies: |
| 174 | + |
| 175 | + list_of_optimal_positions_and_instrument_strategies = [ |
| 176 | + optimal_position_and_instrument_strategy |
| 177 | + for optimal_position_and_instrument_strategy in list_of_optimal_positions_and_instrument_strategies |
| 178 | + if is_not_raw_optimal_position_and_instrument_strategy( |
| 179 | + optimal_position_and_instrument_strategy |
| 180 | + ) |
| 181 | + ] |
| 182 | + |
| 183 | + return listOfOptimalPositionsAcrossInstrumentStrategies( |
| 184 | + list_of_optimal_positions_and_instrument_strategies |
| 185 | + ) |
| 186 | + |
| 187 | + |
| 188 | +def is_not_raw_optimal_position_and_instrument_strategy( |
| 189 | + optimal_position_and_instrument_strategy: instrumentStrategyAndOptimalPosition, |
| 190 | +) -> bool: |
| 191 | + |
| 192 | + return is_not_raw_instrument_strategy( |
| 193 | + optimal_position_and_instrument_strategy.instrument_strategy |
| 194 | + ) |
| 195 | + |
| 196 | + |
| 197 | +def is_not_raw_instrument_strategy(instrument_strategy: instrumentStrategy) -> bool: |
| 198 | + return is_not_raw_strategy(instrument_strategy.strategy_name) |
| 199 | + |
| 200 | + |
| 201 | +def instrument_strategy_with_raw_tag( |
| 202 | + instrument_strategy: instrumentStrategy, |
| 203 | +) -> instrumentStrategy: |
| 204 | + original_strategy_name = copy(instrument_strategy.strategy_name) |
| 205 | + strategy_name = strategy_name_with_raw_tag(original_strategy_name) |
| 206 | + |
| 207 | + new_instrument_strategy = instrumentStrategy( |
| 208 | + strategy_name=strategy_name, instrument_code=instrument_strategy.instrument_code |
| 209 | + ) |
| 210 | + |
| 211 | + return new_instrument_strategy |
| 212 | + |
| 213 | + |
| 214 | +def strategy_name_with_raw_tag(strategy_name: str) -> str: |
| 215 | + return strategy_name + POST_TAG_FOR_RAW_OPTIMAL_POSITION |
0 commit comments