Fixing IB API Futures Contract Validation with Correct Fields
Summary
The example concerns an Interactive Brokers API validation error when requesting market data for a Micro E-mini Nasdaq futures contract. The submitted contract uses a local symbol field and capitalized expiry attribute, and TWS reports that a local symbol or expiry is required. The accepted response suggests supplying the trading class and using the correctly cased last-trade-date-or-contract-month field, along with the contract’s symbol, security type, exchange, and currency.
The response reports that this field change returned an ask price in the author’s run. It also changes the market-data request to snapshot mode, explaining that this avoids requesting a continuing stream. The result is a targeted troubleshooting example rather than a general API guide: behavior may depend on contract details and API version, and the cited output does not establish that the same settings apply to all futures or accounts.
Key ideas
- IB contract property names are case-sensitive, so the expiry field must use the API’s expected spelling.
- The answer supplies a trading class and expiry to resolve the reported futures contract validation error.
- A snapshot market-data request can be used when a continuing stream is not wanted.
- The example reports a returned ask price but does not generalize across contracts or API versions.
Tags
Full text
# Interactive Broker API error 321?
# Interactive Broker API error 321?
Hi I'm having trouble with the Interactive Broker python API. I'm using python 3.8 and connecting to IB TWS ver. 979
when I run this symbol script I get the following error:
```
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:
`Error 1 321 error validating request:-'bW' : cause - Please enter a local symbol or expiry`
I can't see what I'm doing wrong. I can't find any other Contact() field relating to expiry.
Thanks
## Answer by amdopt (score 5, accepted)
https://quant.stackexchange.com/a/55572
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'
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, [])
```
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
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.