简体   繁体   中英

How to create a conditional order using the native Interactive Brokers Python API?

The following code is what IB provided. How can I expand on this to create a conditional order, where the prices of two different contracts can be used in a conditional statement to execute a market order of said contracts?

Ex: if AAPL>SPY, "BUY" AAPL, else: "BUY" SPY...

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
from ibapi.utils import iswrapper #just for decorator
from ibapi.order import Order
from threading import Timer
import time

class TestApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)

    def error(self, reqId, errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def nextValidId(self, orderId):
        self.nextOrderId = orderId
        self.start()

    def nextOrderId(self):
        oid = self.nextValidOrderId
        self.nextValidOrderId +=1
        return oid

    def orderStatus(self, orderId , status:str, filled:float,
                    remaining:float, avgFillPrice:float, permId:int,
                    parentId:int, lastFillPrice:float, clientId:int,
                    whyHeld:str, mktCapPrice: float):
        print(orderId, status, filled, remaining, lastFillPrice)

    def openOrder(self, orderId, contract: Contract, order: Order,
                  orderState):
        print("OpenOrder. ID:", orderId, "Symbol:", contract.symbol, "SecType:", contract.secType,
              "Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,
              "TotalQuantity:", order.totalQuantity, "Status:", orderState.status)
    def execDetails(self, reqId: int, contract: Contract, execution):
        print("ExecDetails. ReqId:", reqId, "Symbol:", contract.symbol, "SecType:", contract.secType, "Currency:", contract.currency, execution)

    def tickPrice(self, reqId, tickType, price, attrib):
        print(price)




def main():

    app = TestApp()
    app.connect("port", 7497, id)

    app.run()


if __name__ == "__main__":
    main()

Native conditional orders (ie those for which the logic which triggers order placement is performed on the IBKR server) don't support a direct comparison betwen the price of two different instruments, such as AAPL and SPY. Instead the price of a security has to be compared to a constant hard-coded value, for example

"If last price of AAPL > 200, submit order to buy SPY"

To become familiar with the types of conditional statements allowed, its recommended to try creating the order first in Trader Workstation before creating it from the TWS API.

You could though request streaming market data for multiple instruments in your API program, and then implement logic there to compare prices of different instruments and submit orders when certain conditions are met.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM