程式

盈透證券 API 錯誤 321?

  • July 10, 2020

您好我在使用 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.run()

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

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

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'
fut_contract.exchange = '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
app.disconnect()

錯誤:

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

我看不出我做錯了什麼。我找不到與到期相關的任何其他 Contact() 欄位。

謝謝

嘗試更改LocalSymboltradingClass並更改Last..Monthlast..Month

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

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

我將第四個參數切換為 True 因為我不想請求一個流,只是一個快照。小心你的大寫和小寫!

這是我建議的更改執行腳本時返回的內容:

The current ask price is:  10765.5

引用自:https://quant.stackexchange.com/questions/55569