python - IBapi想读取和使用IBapi类生成的错误码
问题描述
我是 python 和 TWS API 的新手,我的问题是我想使用由 Error 生成的错误 ID 。Id: 441 Code: 200 Msg: No security definition has found for the request虽然它显示在终端中,但我无法在变量中获取它以便在我的代码中进一步使用
这是我的代码
[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 error(self, reqId, errorCode, errorString):
super().error(reqId, errorCode, errorString)
print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString)
def run_loop():
app.run()
# Function to create STOCK Order contract
def STK_order(stock_ticker,):
contract = Contract()
contract.symbol = stock_ticker
contract.secType = "STK"
contract.exchange = "NSE"
contract.currency = "INR"
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')
break
else:
print('waiting for connection')
time.sleep(1)
# Create order object
def place_order(place_order_action, place_order_quantity, place_order_order_type):
order = Order()
order.action = place_order_action
order.totalQuantity = place_order_quantity
order.trailingPercent = 0.5
order.trailStopPrice = 1612
order.lmtPrice = 1613
order.orderType = place_order_order_type
order.trailingPercent = 5
return order
stocks_list = [['ICICINIFTY', '628.00', 'NSE', 'BUY', 1]]
oid = app.nextorderId
for stock_ticker, open_price, stock_market, stock_action, stock_quantity in stocks_list:
app.placeOrder(oid, STK_order(stock_ticker), place_order(stock_action, stock_quantity, 'LMT'))
oid = oid + 1
# app.nextorderId += 1
time.sleep(1)
app.disconnect()
这是输出:
The next valid order id is: 443
Error. Id: -1 Code: 2104 Msg: Market data farm connection is OK:hfarm
Error. Id: -1 Code: 2104 Msg: Market data farm connection is OK:usfarm
Error. Id: -1 Code: 2106 Msg: HMDS data farm connection is OK:hkhmds
Error. Id: -1 Code: 2106 Msg: HMDS data farm connection is OK:ushmds
Error. Id: -1 Code: 2158 Msg: Sec-def data farm connection is OK:secdefhk
ERROR -1 2104 Market data farm connection is OK:hfarm
ERROR -1 2104 Market data farm connection is OK:usfarm
ERROR -1 2106 HMDS data farm connection is OK:hkhmds
ERROR -1 2106 HMDS data farm connection is OK:ushmds
ERROR -1 2158 Sec-def data farm connection is OK:secdefhk
connected
ERROR 443 200 No security definition has been found for the request
Error. Id: 443 Code: 200 Msg: No security definition has been found for the request
我想抓住 错误。id: 443 转换成变量供进一步使用
解决方案
要存储错误,您可以在 IBApi 类中创建一个数据框成员变量。
import pandas as pd
def __init__(self):
...
self.errors_received = pd.DataFrame(columns=['reqId', 'errorCode', 'errorString'])
def error(self, reqId, errorCode, errorString):
...
self.errors_received.loc[len(self.errors_received)] = [reqId, errorCode, errorString]
推荐阅读
- android - 将 Cordova 迁移到电容器 - 错误:包 android.support.v4.content 不存在
- node.js - 如何从 MongoDB 显示
- node.js - Ubuntu | 节点 | SSL 证书的安全位置?
- java - 无法从 START_OBJECT 令牌中反序列化“java.util.date”的实例
- r - 为什么我的 FASTA 文件以空 > 符号开头?
- javascript - 仅使用函数来引用 DOM 元素
- sql - 检查 postgresql 中的相同范围
- python - 我如何根据已经创建的对象创建怪物列表,玩家可以与之交互
- http - Flutter dart:io 的令牌发布请求无效
- upgrade - Wixsharp 升级不保留以前的设置,如数据库数据和其他配置