Using the Interactive Brokers Python API, I am trying to submit a limit order that is in-force till a certain time of day.
Based on the documentation here https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a04f61266450f61c36fae22946c74a8f3
it looks like the relevant fields are tif and GoodTillDate.
Modifying an example from here: https://algotrading101.com/learn/interactive-brokers-python-api-native-guide/
I add the two lines
order.tif = "GTD"
order.GoodTillDate = "20200827 16:48:00 EST"
When I submit an order using this, I get an error window appear in TWS with this message:
"The time or time-zone entered is invalid.
The correct format is hh:mm:ss xxx where xxx is an optionally specified time-zone. E.g.: 15:59:00 EST
Note that there is a space between the time and the time zone.
If no time zone is specified, local time is assumed."
Is there an issue with my date and time format?
My full code:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
import threading
import time
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.nextorderId = orderId
print('The next valid order id is: ', self.nextorderId)
def orderStatus(self, orderId, status, filled, remaining, avgFullPrice, permId, parentId, lastFillPrice, clientId,
whyHeld, mktCapPrice):
print('orderStatus - orderid:', orderId, 'status:', status, 'filled', filled, 'remaining', remaining,
'lastFillPrice', 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('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
execution.orderId, execution.shares, execution.lastLiquidity)
def run_loop():
app.run()
def FX_order(symbol):
contract = Contract()
contract.symbol = symbol[:3]
contract.secType = 'CASH'
contract.exchange = 'IDEALPRO'
contract.currency = symbol[3:]
return contract
app = IBapi()
app.connect('127.0.0.1', 7496, 0)
app.nextorderId = None
# Start the socket in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
# Check if the API is connected via orderid
while True:
if isinstance(app.nextorderId, int):
print('connected')
print()
break
else:
print('waiting for connection')
time.sleep(1)
# Create order object
order = Order()
order.action = 'BUY'
order.totalQuantity = 100000
order.orderType = 'LMT'
order.lmtPrice = '1.10'
order.tif = "GTD"
order.GoodTillDate = "20200827 16:48:00 EST" # format "YYYYMMDD hh:mm:ss (optional time zone)"
order.orderId = app.nextorderId
app.nextorderId += 1
order.transmit = False
# Create stop loss order object
stop_order = Order()
stop_order.action = 'SELL'
stop_order.totalQuantity = 100000
stop_order.orderType = 'STP'
stop_order.auxPrice = '1.09'
stop_order.orderId = app.nextorderId
app.nextorderId += 1
stop_order.parentId = order.orderId
order.transmit = True
# Place orders
app.placeOrder(order.orderId, FX_order('EURUSD'), order)
app.placeOrder(stop_order.orderId, FX_order('EURUSD'), stop_order)
app.disconnect()