盈透证券通过 python api 的有效时间订单

2024-02-14

使用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()

中的 ggoodTillDate必须是小写。

Example:

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

盈透证券通过 python api 的有效时间订单 的相关文章

随机推荐