如何从盈透证券API获取合约详情?
How to obtain Contract Details from the Interactive Brokers API?
在 Interactive Brokers documentation 之后,我正在尝试使用以下代码获取合同详细信息:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class MyWrapper(EWrapper):
def contractDetails(self, reqId, contractDetails):
super().contractDetails(reqId, contractDetails)
print("ContractDetails. ReqId:", reqId,
contractDetails.summary.symbol,
contractDetails.summary.secType,
"ConId:", contractDetails.summary.conId,
"@", contractDetails.summary.exchange)
def contractDetailsEnd(self, reqId):
super().contractDetailsEnd(reqId)
print("ContractDetailsEnd. ", reqId, "\n")
wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))
from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"
app.reqContractDetails(4444, contract)
app.run()
返回的输出是:
serverVersion:148 connectionTime:b'20190117 17:11:38 AEST'
An exception has occurred, use %tb to see the full traceback.
SystemExit
C:\Users\Greg\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
如何从盈透证券获取合约详情API?我尝试使用 %tb
但我认为我没有把它放在正确的行上。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class MyWrapper(EWrapper):
def nextValidId(self, orderId:int):
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
# start program here or use threading
app.reqContractDetails(4444, contract)
def contractDetails(self, reqId, contractDetails):
print(reqId, contractDetails.contract)# my version doesnt use summary
def contractDetailsEnd(self, reqId):
print("ContractDetailsEnd. ", reqId)
# this is the logical end of your program
app.disconnect() # delete if threading and you want to stay connected
def error(self, reqId, errorCode, errorString):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))
from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"
app.run() # delete this line if threading
# def runMe():
# app.run()
# import threading
# thread = threading.Thread(target = runMe)
# thread.start()
# input('enter to disconnect')
# app.disconnect()
您在开始发送消息之前请求数据 reader。也许你在它开始之前就得到了数据。
IB 建议您在收到 nextValidId 后启动该程序,以便您知道一切 运行ning 正常。由于消息读取循环中的 python API 块,您需要实现线程或将程序结构化为 运行 异步。
我已经展示了如何做到这一点,因此它 运行 无需用户输入,并且它是事件驱动的或异步的。这意味着程序会一直等到它应该做某事,然后它才去做。
我已经包括线程选项,只需更改注释即可。
ContractDetails.summary 已更改为合同。我不确定 python 中是否有摘要,不知道你从哪里得到的。
使用 ib_insync 包和 Python 3,如果你想获取所有存储在 df 中的合同列表的详细信息,你也可以执行以下操作:
from ib_insync import *
import pandas as pd
def add_contract_details(ib_client, ib_contract, df):
list_of_contract_details = ib_client.reqContractDetails(contract=ib_contract)
if list_of_contract_details:
print(
f"Found {len(list_of_contract_details)} contract{'s' if len(list_of_contract_details) > 1 else ''} for {ib_contract.symbol}: "
)
for contract_details in list_of_contract_details:
data = {}
for k, v in contract_details.contract.__dict__.items():
data[k] = v
for k, v in contract_details.__dict__.items():
if k != "contract":
data[k] = v
df = pd.DataFrame([data]) if df is None else df.append(pd.DataFrame([data]))
else:
print(f"No details found for contract {ib_contract.symbol}.")
return df
ib = IB()
ib.connect(host="127.0.0.1", port=7497, clientId=1)
btc_fut_cont_contract = ContFuture("BRR", "CMECRYPTO")
ib.qualifyContracts(btc_fut_cont_contract)
fx_contract_usdjpy = Forex('USDJPY')
ib.qualifyContracts(fx_contract_usdjpy)
df_contract_details = None
for c in [btc_fut_cont_contract, fx_contract_usdjpy]:
df_contract_details = add_contract_details(ib, c, df_contract_details)
print(df_contract_details)
在 Interactive Brokers documentation 之后,我正在尝试使用以下代码获取合同详细信息:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class MyWrapper(EWrapper):
def contractDetails(self, reqId, contractDetails):
super().contractDetails(reqId, contractDetails)
print("ContractDetails. ReqId:", reqId,
contractDetails.summary.symbol,
contractDetails.summary.secType,
"ConId:", contractDetails.summary.conId,
"@", contractDetails.summary.exchange)
def contractDetailsEnd(self, reqId):
super().contractDetailsEnd(reqId)
print("ContractDetailsEnd. ", reqId, "\n")
wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))
from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"
app.reqContractDetails(4444, contract)
app.run()
返回的输出是:
serverVersion:148 connectionTime:b'20190117 17:11:38 AEST'
An exception has occurred, use %tb to see the full traceback.
SystemExit
C:\Users\Greg\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
如何从盈透证券获取合约详情API?我尝试使用 %tb
但我认为我没有把它放在正确的行上。
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
class MyWrapper(EWrapper):
def nextValidId(self, orderId:int):
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
# start program here or use threading
app.reqContractDetails(4444, contract)
def contractDetails(self, reqId, contractDetails):
print(reqId, contractDetails.contract)# my version doesnt use summary
def contractDetailsEnd(self, reqId):
print("ContractDetailsEnd. ", reqId)
# this is the logical end of your program
app.disconnect() # delete if threading and you want to stay connected
def error(self, reqId, errorCode, errorString):
print("Error. Id: " , reqId, " Code: " , errorCode , " Msg: " , errorString)
wrapper = MyWrapper()
app = EClient(wrapper)
app.connect("127.0.0.1", 7497, clientId=123)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(), app.twsConnectionTime()))
from ibapi.contract import Contract
contract = Contract()
contract.symbol = "XAUUSD"
contract.secType = "CMDTY"
contract.exchange = "SMART"
contract.currency = "USD"
app.run() # delete this line if threading
# def runMe():
# app.run()
# import threading
# thread = threading.Thread(target = runMe)
# thread.start()
# input('enter to disconnect')
# app.disconnect()
您在开始发送消息之前请求数据 reader。也许你在它开始之前就得到了数据。
IB 建议您在收到 nextValidId 后启动该程序,以便您知道一切 运行ning 正常。由于消息读取循环中的 python API 块,您需要实现线程或将程序结构化为 运行 异步。
我已经展示了如何做到这一点,因此它 运行 无需用户输入,并且它是事件驱动的或异步的。这意味着程序会一直等到它应该做某事,然后它才去做。
我已经包括线程选项,只需更改注释即可。
ContractDetails.summary 已更改为合同。我不确定 python 中是否有摘要,不知道你从哪里得到的。
使用 ib_insync 包和 Python 3,如果你想获取所有存储在 df 中的合同列表的详细信息,你也可以执行以下操作:
from ib_insync import *
import pandas as pd
def add_contract_details(ib_client, ib_contract, df):
list_of_contract_details = ib_client.reqContractDetails(contract=ib_contract)
if list_of_contract_details:
print(
f"Found {len(list_of_contract_details)} contract{'s' if len(list_of_contract_details) > 1 else ''} for {ib_contract.symbol}: "
)
for contract_details in list_of_contract_details:
data = {}
for k, v in contract_details.contract.__dict__.items():
data[k] = v
for k, v in contract_details.__dict__.items():
if k != "contract":
data[k] = v
df = pd.DataFrame([data]) if df is None else df.append(pd.DataFrame([data]))
else:
print(f"No details found for contract {ib_contract.symbol}.")
return df
ib = IB()
ib.connect(host="127.0.0.1", port=7497, clientId=1)
btc_fut_cont_contract = ContFuture("BRR", "CMECRYPTO")
ib.qualifyContracts(btc_fut_cont_contract)
fx_contract_usdjpy = Forex('USDJPY')
ib.qualifyContracts(fx_contract_usdjpy)
df_contract_details = None
for c in [btc_fut_cont_contract, fx_contract_usdjpy]:
df_contract_details = add_contract_details(ib, c, df_contract_details)
print(df_contract_details)