[英]Need help printing execution details when I manually place a trade using IBKR API (Interactive Brokers)
我正在尝试在订单完成时打印执行详细信息。 我使用 execDetails 方法在代码中工作,但是它只在机器人进行交易时打印执行细节。 当我在 TWS 平台上手动进行交易时,我试图让它打印执行细节。 这有可能实现吗?
这是我的代码,仅在机器人进行交易时才有效:
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 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()
# Function to create FX Order contract
def spy_order():
contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.currency = "USD"
contract.exchange = "SMART"
contract.primaryExchange = "ARCA"
return contract
app = IBapi()
app.connect('127.0.0.1', 7497, 123)
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 = 1
order.orderType = 'MKT'
# Place order
app.placeOrder(app.nextorderId, spy_order(), order)
app.nextorderId += 1
好吧,我想通了。 要让它通过手动订单回复您,您需要转到您的 TWS API 设置并将 Master API 客户端 ID 设置为 0,然后将其与连接客户端 ID 匹配:
app.connect('127.0.0.1', 7497, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.