我正在使用Interactive Brokers Python API提交一份有效的限价单,直到一天中的特定时间。
看起来相关字段为tif
和GoodTillDate
。
从此处修改示例: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()
答案 0 :(得分:0)
goodTillDate
中的g必须小写。
示例:
contract = Contract()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
order = Order()
order.goodTillDate = "20200923 15:13:20 EST"
order.tif = "GTD"
order.totalQuantity = 1
order.orderType = "LMT"
order.lmtPrice = 100
order.action = "BUY"
self.placeOrder(self._next_order_id, contract=contract, order=order)