def _getContract(self, ibContract): """ Create contract that corresponds to the given ibapi contract. """ contract = Contract.create(**ibContract.__dict__) if ibContract.comboLegs: contract.comboLegs = [ ComboLeg(**leg.__dict__) for leg in ibContract.comboLegs ] return contract
def open_strategy(self, strategy: Strategy) -> None: """Place a new strategy at the market """ ownership = "BUY" if strategy.ownership == OwnershipType.Buyer else "SELL" reverse_ownership = "BUY" if ownership == "SELL" else "SELL" contract = Contract() contract.symbol = strategy.code contract.secType = "BAG" contract.exchange = "SMART" contract.currency = strategy.currency.value order_comboLegs = [] tp_sl_comboLegs = [] for leg in strategy.legs.values(): leg_order = ComboLeg() leg_order.conId = leg.option.contract.conId leg_order.ratio = leg.ratio leg_order.action = "BUY" if leg.ownership == OwnershipType.Buyer else "SELL" # contract.comboLegs.append(leg_order) order_comboLegs.append(leg_order) for leg in strategy.legs.values(): leg_tp_sl = ComboLeg() leg_tp_sl.conId = leg.option.contract.conId leg_tp_sl.ratio = leg.ratio leg_tp_sl.action = ( "SELL" if leg.ownership == OwnershipType.Buyer else "BUY" ) # reverse # contract.comboLegs.append(leg_order) tp_sl_comboLegs.append(leg_tp_sl) contract.comboLegs = order_comboLegs order = LimitOrder( action="BUY" if strategy.ownership == OwnershipType.Buyer else "SELL", totalQuantity=strategy.quantity, lmtPrice=strategy.entry_price, orderRef=strategy.strategy_id, orderId=self._broker.client.getReqId(), tif="GTC", transmit=True, ) # Must be False print("ORDER SENDED") self._broker.placeOrder(contract, order) # contract.comboLegs = tp_sl_comboLegs # print('take_profit_order', strategy.take_profit_price) take_profit = LimitOrder( action="SELL" if strategy.ownership == OwnershipType.Buyer else "BUY", # reverse totalQuantity=strategy.quantity, lmtPrice=strategy.take_profit_price, # lmtPrice = -0.1, orderRef=strategy.strategy_id + "_TP", orderId=self._broker.client.getReqId(), tif="GTC", transmit=True, parentId=order.orderId, ) self._broker.placeOrder(contract, take_profit)