def c_order(self, buy_sell): '''click buy button''' if self.check_login(): return prod = self.box_product.currentText()[0:3] prodM = self.box_month.currentText()[0:6] price = self.box_price.value() qty = self.box_qty.value() hsi = Future(prod, prodM) if web_trade.ib.qualifyContracts(hsi): if buy_sell == 'B': order = LimitOrder('BUY', qty, price, outsideRth=self.c_t1.isChecked(), orderRef=f"PY_{self.pwd_id[-1]}") else: order = LimitOrder('SELL', qty, price, outsideRth=self.c_t1.isChecked(), orderRef=f"PY_{self.pwd_id[-1]}") web_trade.ib.placeOrder(hsi, order) print("%s%s %d@%d" % (prod, prodM, qty if buy_sell == 'B' else -qty, price)) if web_trade.status == 2: w.statusBar().showMessage(web_trade.error)
def cb_close1(self): if self.check_login(): return getLots = self.box_lot2.value() if getLots == 0: return Lots, Price = self.calc_close() if Lots is None or Price is None: return prod = self.set1['合约'] web_trade.ib.waitOnUpdate(0.1) able_lots = web_trade.get_lots('LMT') if prod not in able_lots or able_lots[prod] == 0: return if abs(Lots) > abs(able_lots[prod]): Lots = able_lots[prod] # prod_code=prod[0:3] # prod_month=prod[3:5] # web_trade.orderdata['prod_code'] = prod_code # web_trade.orderdata['contract_mth'] = prod_month lotsList = self.getList(abs(int(Lots))) # print(getLots,prod,Lots,Price,lotsList,self.c_close1.isChecked()) hsi = Future(localSymbol=prod) if web_trade.ib.qualifyContracts(hsi): if Lots > 0: for x in lotsList: # web_trade.order_buy(Price, x, prod_code=prod_code) zy, thereAre = self.zszyGroup('zy') order = LimitOrder('BUY', x, Price, ocaGroup=zy, ocaType=2, outsideRth=self.c_close1.isChecked(), orderRef=f"PY_{self.pwd_id[-1]}") web_trade.ib.placeOrder(hsi, order) print("%s %d@%d" % (prod, x, Price)) else: for x in lotsList: # web_trade.order_sell(Price, x, prod_code=prod_code) zy, thereAre = self.zszyGroup('zy') order = LimitOrder('SELL', x, Price, ocaGroup=zy, ocaType=2, outsideRth=self.c_close1.isChecked(), orderRef=f"PY_{self.pwd_id[-1]}") web_trade.ib.placeOrder(hsi, order) print("%s -%d@%d" % (prod, x, Price)) self.box_lot2.setValue(0) self.c_orderlist()
def order(self): tp_price = round_tick( self.price - self.sl_points * self.direction * self.tp_multiple, self.min_tick) log.info(f'TAKE PROFIT PRICE: {tp_price}') return LimitOrder(self.reverseAction, self.amount, tp_price, tif='GTC')
def test_place_order(self): # Arrange instrument = IBTestStubs.instrument("AAPL") contract_details = IBTestStubs.contract_details("AAPL") self.instrument_setup(instrument=instrument, contract_details=contract_details) order = TestExecStubs.limit_order(instrument_id=instrument.id, ) command = TestCommandStubs.submit_order_command(order=order) # Act with patch.object(self.exec_client._client, "placeOrder") as mock: self.exec_client.submit_order(command=command) # Assert expected = { "contract": Contract( secType="STK", conId=265598, symbol="AAPL", exchange="SMART", primaryExchange="NASDAQ", currency="USD", localSymbol="AAPL", tradingClass="NMS", ), "order": LimitOrder(action="BUY", totalQuantity=100.0, lmtPrice=55.0), } name, args, kwargs = mock.mock_calls[0] # Can't directly compare kwargs for some reason? assert kwargs["contract"] == expected["contract"] assert kwargs["order"].action == expected["order"].action assert kwargs["order"].totalQuantity == expected["order"].totalQuantity assert kwargs["order"].lmtPrice == expected["order"].lmtPrice
def test_cancel_order(self): # Arrange instrument = IBTestStubs.instrument("AAPL") contract_details = IBTestStubs.contract_details("AAPL") contract = contract_details.contract order = IBTestStubs.create_order() self.instrument_setup(instrument=instrument, contract_details=contract_details) self.exec_client._ib_insync_orders[ TestIdStubs.client_order_id()] = Trade(contract=contract, order=order) # Act command = TestCommandStubs.cancel_order_command( instrument_id=instrument.id) with patch.object(self.exec_client._client, "cancelOrder") as mock: self.exec_client.cancel_order(command=command) # Assert expected = { "contract": Contract( secType="STK", conId=265598, symbol="AAPL", exchange="SMART", primaryExchange="NASDAQ", currency="USD", localSymbol="AAPL", tradingClass="NMS", ), "order": LimitOrder(action="BUY", totalQuantity=100_000, lmtPrice=105.0), }
def _place_order(self, contract: Contract, side: str, size: int, price: float) -> Trade: side = side.upper() if side not in ('SELL', 'BUY'): return [f'invalid order type: {side}'] price = float(f'{round(float(price), 3):.3f}') order = LimitOrder(side, size, price, tif='GTC') trade = self._ib.placeOrder(contract, order) return trade
def test_update_order(self): # Arrange instrument = IBTestStubs.instrument("AAPL") contract_details = IBTestStubs.contract_details("AAPL") contract = contract_details.contract order = IBTestStubs.create_order() self.instrument_setup(instrument=instrument, contract_details=contract_details) self.exec_client._ib_insync_orders[ TestIdStubs.client_order_id()] = Trade(contract=contract, order=order) # Act command = TestCommandStubs.modify_order_command( instrument_id=instrument.id, price=Price.from_int(10), quantity=Quantity.from_str("100"), ) with patch.object(self.exec_client._client, "placeOrder") as mock: self.exec_client.modify_order(command=command) # Assert expected = { "contract": Contract( secType="STK", conId=265598, symbol="AAPL", exchange="SMART", primaryExchange="NASDAQ", currency="USD", localSymbol="AAPL", tradingClass="NMS", ), "order": LimitOrder(action="BUY", totalQuantity=100, lmtPrice=10.0), } name, args, kwargs = mock.mock_calls[0] # Can't directly compare kwargs for some reason? assert kwargs["contract"] == expected["contract"] assert kwargs["order"].action == expected["order"].action assert kwargs["order"].totalQuantity == expected["order"].totalQuantity assert kwargs["order"].lmtPrice == expected["order"].lmtPrice
def xiadan(price, vb): if vb > 0: order = LimitOrder('BUY', abs(vb), price, orderRef='PY_WH_3') else: order = LimitOrder('SELL', abs(vb), price, orderRef='PY_WH_3') ib.placeOrder(hsi, order)
def take_profit(action: str, quantity: int, price: float, **kwargs ) -> Order: return LimitOrder(action, quantity, price, tif='GTC')
def entry_order(action: str, quantity: int, price: float, **kwargs ) -> Order: return LimitOrder(action, quantity, price, **kwargs)