I am hoping somebody here may be able to help clarify how to structure the IB-Insync Contract Format for a Futures order in a python API link with Interactive Brokers API. I am trying to develop a autobot API link into the Interactive Brokers API using IB-Insync. I have the system working perfectly now automatically placing orders with a 'Stock' contract format; as follows:
stock = Stock(message_data['ticker'], 'SMART', 'USD')
ib.qualifyContracts(stock)
order = MarketOrder(message_data['strategy']['order_action'], message_data['strategy']['order_contracts'])
trade = ib.placeOrder(stock, order)
but when i apply the same python script to my understanding of the required IB-Insync contract format for a Futures order as per the documentation, nothing happens and a error shows in the API log.
The IB-Insync Futures contract format used is below:
contract = Future(symbol='MES', lastTradeDateOrContractMonth='20211217', exchange='GLOBEX',
localSymbol='MESZ1', multiplier='5', currency='USD')
ib.qualifyContracts(contract)
order = MarketOrder(message_data['strategy']['order_action'], message_data['strategy']['order_contracts'])
trade = ib.placeOrder(contract, order)
API Log Error message below:
22:28:49:791 <- 9-8-59-0-MES-STK--0.0---SMART--USD---0--- 22:28:50:029 -> ---A4-2-59-200-No security definition has been found for the request- 22:28:50:030 <- 3-60-0-MES-STK--0.0---SMART--USD-----BUY-1-MKT------O-0--1-0-0-0-0-0-0-0--0.0--------0---1-0---0---0-0--0------0-----0-----------0---0-0---0--0-0-0-0-------0---------0-0-0-0- 22:28:50:264 -> ---A4-2-60-200-No security definition has been found for the request-
I have tried numerous different ways to structure the Futures Contract Format; including:
contract=Future('MES', '20121217', 'GLOBEX')
But the same problem occurs - the Interactive Brokers TWS API is recording the order as a STK order routing it through the SMART exchange protocol; which clearly it is not. I can manually place a Futures order on TWS for MES; so it cannot be due to a privilege's setting.
I am hoping somebody here may know of a resolution or has come across this problem before?
I am stumped by it
Thankyou for your help
The entire code is below:
import redis, json
from ib_insync import *
import asyncio, time, random
from dataclasses import dataclass, field
from typing import List, NamedTuple, Optional
import ib_insync.util as util
# connect to Interactive Brokers
util.startLoop()
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
# connect to Redis and subscribe to tradingview messages
r = redis.Redis(host='localhost', port=6379, db=0)
p = r.pubsub()
p.subscribe('tradingview')
async def check_messages():
print(f"{time.time()} - checking for tradingview webhook messages")
message = p.get_message()
if message is not None and message['type'] == 'message':
print(message)
message_data = json.loads(message['data'])
contract = Future(symbol='MES', lastTradeDateOrContractMonth='20211217', exchange='GLOBEX', localSymbol='MESZ1', multiplier='5', currency='USD')
ib.qualifyContracts(contract)
order = MarketOrder(message_data['strategy']['order_action'], message_data['strategy']['order_contracts'])
trade = ib.placeOrder(contract, order)
async def run_periodically(interval, periodic_function):
while True:
await asyncio.gather(asyncio.sleep(interval), periodic_function())
asyncio.run(run_periodically(1, check_messages))
ib.run()