|
| 1 | +import datetime |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Tuple, List |
| 4 | + |
| 5 | +from build.lib.sysexecution.algos.common_functions import cancel_order |
| 6 | +from private.projects.MR_2025.components import Order, Fill, FillAndOrder |
| 7 | +from private.projects.MR_2025.configuration import STRATEGY_NAME |
| 8 | +from sysdata.data_blob import dataBlob |
| 9 | +from sysexecution.algos.algo import Algo |
| 10 | +from sysexecution.order_stacks.broker_order_stack import orderWithControls |
| 11 | +from sysexecution.orders.broker_orders import create_new_broker_order_from_contract_order, stop_loss_order_type, \ |
| 12 | + limit_order_type |
| 13 | +from sysexecution.orders.contract_orders import contractOrder |
| 14 | +from sysexecution.orders.named_order_objects import missing_order |
| 15 | +from sysobjects.contracts import futuresContract |
| 16 | +from sysobjects.production.positions import listOfContractPositions, contractPosition |
| 17 | +from sysproduction.data.broker import dataBroker |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class OrderAndOrderWithControls: |
| 21 | + order: Order |
| 22 | + order_with_controls: orderWithControls |
| 23 | + |
| 24 | +class BrokerController(): |
| 25 | + def __init__(self, data: dataBlob, futures_contract: futuresContract): |
| 26 | + self.data =data |
| 27 | + self.futures_contract = futures_contract |
| 28 | + |
| 29 | + def create_limit_order(self, order: Order): |
| 30 | + ## We don't bother databasing orders until executed |
| 31 | + |
| 32 | + contract_order = get_contract_order_from_simple_order(order=order, futures_contract=self.futures_contract) |
| 33 | + algo_instance = StrippedDownAlgo(data=self.data, contract_order=contract_order) |
| 34 | + broker, broker_account, broker_clientid = self.get_broker_details() |
| 35 | + placed_broker_order_with_controls = algo_instance.submit_mr_trade( |
| 36 | + broker_account=broker_account, |
| 37 | + broker=broker, |
| 38 | + broker_clientid=broker_clientid |
| 39 | + ) |
| 40 | + |
| 41 | + if placed_broker_order_with_controls is missing_order: |
| 42 | + ## pass |
| 43 | + return missing_order |
| 44 | + |
| 45 | + ## store the placed broker order for future reference |
| 46 | + self.add_order_to_storage(OrderAndOrderWithControls( |
| 47 | + order=order, |
| 48 | + order_with_controls=placed_broker_order_with_controls |
| 49 | + )) |
| 50 | + |
| 51 | + return order |
| 52 | + |
| 53 | + def cancel_all_orders(self): |
| 54 | + while len(self.order_storage)>0: |
| 55 | + order_and_placed_broker_order_with_controls = self.order_storage.pop() |
| 56 | + cancel_order(self.data, order_and_placed_broker_order_with_controls.order_with_controls) |
| 57 | + |
| 58 | + |
| 59 | + def check_for_position_match(self, position: int, futures_contract: futuresContract) -> bool: |
| 60 | + broker_contract_positions = self.data_broker.get_all_current_contract_positions() |
| 61 | + broker_position = broker_contract_positions.position_in_contract(futures_contract=futures_contract) |
| 62 | + |
| 63 | + return int(broker_position)==int(position) |
| 64 | + |
| 65 | + def check_for_position_match_msg(self, position: int, futures_contract: futuresContract) -> str: |
| 66 | + broker_contract_positions = self.data_broker.get_all_current_contract_positions() |
| 67 | + broker_position = broker_contract_positions.position_in_contract(futures_contract=futures_contract) |
| 68 | + |
| 69 | + return "Break: Broker position %d, our position from state %d" % (broker_position, position) |
| 70 | + |
| 71 | + def get_fills_from_broker(self) -> List[FillAndOrder]: |
| 72 | + list_of_fills_and_orders = [] |
| 73 | + order_storage = self.order_storage |
| 74 | + for order_and_order_with_controls in order_storage: |
| 75 | + order_with_controls = order_and_order_with_controls.order_with_controls |
| 76 | + order_with_controls.update_order() |
| 77 | + if order_with_controls.completed(): |
| 78 | + filled_price = order_with_controls.order.filled_price |
| 79 | + filled_qty = order_with_controls.order.fill.as_single_trade_qty_or_error() |
| 80 | + fill = Fill(price=filled_price, size=filled_qty) |
| 81 | + |
| 82 | + order_storage.remove(order_and_order_with_controls) |
| 83 | + print("%s filled at %f" % (str(order_and_order_with_controls.order), filled_price)) |
| 84 | + list_of_fills_and_orders.append( |
| 85 | + FillAndOrder( |
| 86 | + fill=fill, |
| 87 | + order=order_and_order_with_controls.order) |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | + return list_of_fills_and_orders |
| 92 | + |
| 93 | + def add_order_to_storage(self, order_and_controls: OrderAndOrderWithControls): |
| 94 | + storage = self.order_storage |
| 95 | + storage.append(order_and_controls) |
| 96 | + self._order_storage = storage |
| 97 | + |
| 98 | + @property |
| 99 | + def order_storage(self) -> List[OrderAndOrderWithControls]: |
| 100 | + storage = getattr(self, "_order_storage", []) |
| 101 | + return storage |
| 102 | + |
| 103 | + @property |
| 104 | + def broker_details(self): |
| 105 | + details = getattr(self, "_broker_details", None) |
| 106 | + if details is None: |
| 107 | + self._broker_details = details = self.get_broker_details() |
| 108 | + |
| 109 | + return details |
| 110 | + |
| 111 | + def get_broker_details(self) -> Tuple[str,str,str]: |
| 112 | + broker = self.data_broker.get_broker_name() |
| 113 | + broker_account = self.data_broker.get_broker_account() |
| 114 | + broker_clientid = str(self.data_broker.get_broker_clientid()) |
| 115 | + |
| 116 | + return broker, broker_account, broker_clientid |
| 117 | + |
| 118 | + |
| 119 | + @property |
| 120 | + def data_broker(self): |
| 121 | + return dataBroker(self.data) |
| 122 | + |
| 123 | +def get_contract_order_from_simple_order(order: Order, futures_contract: futuresContract): |
| 124 | + if order.stop_loss: |
| 125 | + order_type = stop_loss_order_type |
| 126 | + else: |
| 127 | + order_type = limit_order_type |
| 128 | + |
| 129 | + contract_order = contractOrder(STRATEGY_NAME, futures_contract.instrument.instrument_code, |
| 130 | + futures_contract.contract_date.date_str, |
| 131 | + order.size, limit_price=order.level, |
| 132 | + order_type=order_type, |
| 133 | + order_id=1, ## pseduo get replaced later |
| 134 | + parent=1, |
| 135 | + reference_price=None, |
| 136 | + generated_datetime=datetime.datetime.now(), |
| 137 | + algo_to_use="MR_stripped_down_algo", |
| 138 | + reference_of_controlling_algo="MR_stripped_down_algo", |
| 139 | + |
| 140 | + ) |
| 141 | + |
| 142 | + return contract_order |
| 143 | + |
| 144 | +class StrippedDownAlgo(Algo): |
| 145 | + |
| 146 | + def submit_mr_trade(self, |
| 147 | + broker:str, broker_account:str, broker_clientid:str) -> orderWithControls: |
| 148 | + """ |
| 149 | +
|
| 150 | + :return: broker order with control or missing_order |
| 151 | + """ |
| 152 | + contract_order = self.contract_order |
| 153 | + broker_order = create_new_broker_order_from_contract_order( |
| 154 | + contract_order, |
| 155 | + order_type=contract_order.order_type, |
| 156 | + broker=broker, |
| 157 | + broker_account=broker_account, |
| 158 | + broker_clientid=broker_clientid, |
| 159 | + limit_price=contract_order.limit_price, |
| 160 | + ) |
| 161 | + |
| 162 | + self.data.log.debug( |
| 163 | + "Created a broker order %s (not yet submitted or written to local DB)" |
| 164 | + % str(broker_order), |
| 165 | + **contract_order.log_attributes(), |
| 166 | + method="temp", |
| 167 | + ) |
| 168 | + |
| 169 | + placed_broker_order_with_controls = self.data_broker.submit_broker_order( |
| 170 | + broker_order |
| 171 | + ) |
| 172 | + |
| 173 | + if placed_broker_order_with_controls is missing_order: |
| 174 | + self.data.log.warning( |
| 175 | + "Order could not be submitted", |
| 176 | + **contract_order.log_attributes(), |
| 177 | + method="temp", |
| 178 | + ) |
| 179 | + return missing_order |
| 180 | + |
| 181 | + self.data.log.debug( |
| 182 | + "Submitted order to IB %s" % str(placed_broker_order_with_controls.order), |
| 183 | + **placed_broker_order_with_controls.order.log_attributes(), |
| 184 | + method="temp", |
| 185 | + ) |
| 186 | + |
| 187 | + return placed_broker_order_with_controls |
| 188 | + |
0 commit comments