Interactive Broker API錯誤321?


嗨,我在使用Interactive Broker python API時遇到了麻煩。我正在使用python 3.8並連接到IB TWS版本。979


from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract

import threading
import time

class IBapi(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self, self)
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 2 and reqId == 1:
            print('The current ask price is: ', price)

def run_loop():

app = IBapi()
app.connect('', 7497, 123)

#Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)

time.sleep(1) #Sleep interval to allow time for connection to server

fut_contract = Contract()
fut_contract.symbol = "MNQU0" #MNQ SEP'20"
fut_contract.secType = 'FUT' = 'GLOBEX'
fut_contract.currency = 'USD'
fut_contract.LocalSymbol = 'MNQU0'
fut_contract.LastTradeDateOrContractMonth = "202009";

#Request Market Data
app.reqMktData(1, fut_contract, '', False, False, [])

time.sleep(100) #Sleep interval to allow time for incoming price data


Error 1 321 error validating request:-'bW' : cause - Please enter a local symbol or expiry




Try changing LocalSymbol to tradingClass and changing Last..Month to last..Month:

fut_contract = Contract()
fut_contract.symbol = 'MNQU0' #MNQ SEP'20
fut_contract.secType = 'FUT' = 'GLOBEX'
fut_contract.currency = 'USD'
fut_contract.tradingClass = 'MNQ'
fut_contract.lastTradeDateOrContractMonth = '202009'

#Request Market Data
app.reqMktData(1, fut_contract, '', True, False, [])

I switched the 4th argument to True because I didn't want to request a stream, just a snapshot. Be careful with your upper and lower cases!

This is what is returned for me when running your script with the changes I suggest:

The current ask price is:  10765.5