使用Interactive Brokers Python API,我尝试提交在一天中的特定时间之前有效的限价订单。
基于此处的文档https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a04f61266450f61c36fae22946c74a8f3 https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a04f61266450f61c36fae22946c74a8f3
看起来相关字段是tif
and GoodTillDate
.
从这里修改一个例子:https://algotrading101.com/learn/interactive-brokers-python-api-native-guide/ https://algotrading101.com/learn/interactive-brokers-python-api-native-guide/
我添加两行
order.tif = "GTD"
order.GoodTillDate = "20200827 16:48:00 EST"
当我使用此提交订单时,TWS 中出现一个错误窗口,并显示以下消息:
“输入的时间或时区无效。
正确的格式是 hh:mm:ss xxx
其中 xxx 是可选指定的时区。
例如:15:59:00 美国东部时间
请注意,时间和时区之间有一个空格。
如果未指定时区,则采用当地时间。”
我的日期和时间格式有问题吗?
我的完整代码:
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()