【问题标题】:Interactive Brokers time-in-force order through python api盈透证券通过 python api 的有效时间订单
【发布时间】:2020-08-27 21:09:56
【问题描述】:

使用盈透证券 Python API,我正在尝试提交一个在一天中的某个时间有效的限价单。

基于此处的文档https://interactivebrokers.github.io/tws-api/classIBApi_1_1Order.html#a04f61266450f61c36fae22946c74a8f3

看起来相关字段是tifGoodTillDate

从这里修改一个示例: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()

【问题讨论】:

  • 对我来说看起来不错。我明天去测试。注意 EST 在 IB 语言中是正确的,但它在 TZ 语言中表示美国/纽约,因此对于 EDT 也可以。
  • 我无法让它工作,我尝试了所有我能想到的选项。请注意,其他订单类型(GTC、DAY)工作正常,因此订单是合法的。一定是个bug。这些文档在某些地方没有帮助且自相矛盾。
  • 感谢查看,我开始认为这也是一个错误。
  • 我知道这曾经有效,我搜索了groups.io/g/twsapi,发现过去有人使用它(包括我自己)。我学到(记住)了一些新东西。进入TWS->配置->显示->设置->显示夏令时。应该取消选中 EST 全年工作。从技术上讲,它现在是 EDT。但是它对我不起作用,甚至没有时区(我在纽约时间)。
  • 对,我没有检查这个设置,而且没有时区也对我不起作用。我和你在同一个时区。

标签: python api interactive-brokers


【解决方案1】:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2017-11-14
    • 1970-01-01
    • 2015-07-14
    • 2021-12-07
    相关资源
    最近更新 更多