简体   繁体   中英

Error 201 interactive brokers tws while placing options order

I am trying to place an option order through TWS on a demo account. I followed the tutorial, but when I run the code, it gives me error 201, "The account number is invalid or is missing". I have spent some time trying to fix it but can't seem to tackle it. Any help would be appreciated.

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer

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 orderStatus(self, orderId , status, filled,
                    remaining, avgFillPrice, permId,
                    parentId, lastFillPrice, clientId,
                    whyHeld, mktCapPrice):
        print("Order Status ID: ", orderId, ", Status", status, ", Fiilled", filled, ", Remaining: ", remaining, ", Last Fill price: ", lastFillPrice)

    def openOrder(self, orderId, contract, order,
                  orderState):
        print("OpenOrder ID: ", orderId, contract.symbol, contract.secType, "@", contract.exchange, ":", order.action, order.orderType, order.totalQuantity, orderState.status)

    def execDetails(self, reqId, contract, execution):
        print("ExecDetails. ", reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)

    def start(self):

        contract = Contract()
        contract.symbol = "VMW" ##self.symbol
        contract.secType = "OPT"
        contract.exchange = "SMART"
        contract.currency = "USD"
        contract.lastTradeDateOrContractMonth = "20200619" #self.expirationDate
        contract.strike = 152.5 #self.strikeP
        contract.right = "C" #self.callorput
        contract.multiplier = "100"

        order = Order()
        order.action = "BUY" #self.buyorsell
        order.totalQuantity = 10
        order.orderType = "MKT"

        self.placeOrder(self.nextOrderId, contract, order)

    def stop(self):
        self.done = True
        self.disconnect()

def main():
    app = TestApp()
    app.nextOrderId = 0

    app.connect(host = 'XXXX', port=XXXX, clientId=X)

    Timer(3, app.stop).start()
    app.run()

if __name__ == '__main__':
    main()

IB demo accounts often have a partitioned account structure to demo that capability. In a partitioned setup, one part of the account is 'partitioned' for use by a financial advisor while the rest of the account is traded by the account owner. Each part has a unique identifying number used when querying positions or placing trades.

When placing orders to a partitioned account, you would just need to specify the account number where you are sending the order in the IBApi Order class with the other order attributes. Eg

order.account = "DU12345"

if the partition available for trading is labelled "DU12345". It may have a 'C' at the end. You should be able to find the account number in the top right of TWS, or through the API managedaccts() callback which is invoked automatically after the connection is made.

http://interactivebrokers.github.io/tws-api/financial_advisor_methods_and_orders.html#api_orders

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