[英]Setting variables to Interactive Brokers API responses in Python
我有一些代碼,我使用盈透證券 API 和 Python 請求期貨合約的實時市場數據,在這種情況下是 VIX 合約。 我收到了通過修補包裝器打印的數據流。 這是使用來自 IB 的實際 Python API,而不是第三方庫。
我想做的是兩方面:首先,將變量設置為最后一個價格,即響應中的 tickType 4 (13.0)。 其次,我想停止當前合同的流數據並請求另一個合同的數據(例如下一個到期日,20170816。)否則,如果我可以同時請求兩組數據並將它們分別設置為一個變量,那么停止流媒體也將是驚人的。 這是迄今為止我所擁有的代碼,它向 IB 發出了成功的請求。 假設 API 已啟用並且您可以訪問 VIX 期貨市場數據(CFE 交易所),響應如下:
from ibapi.wrapper import EWrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper
from ibapi.common import *
from ibapi.contract import *
from ibapi.ticktype import *
# Request IB Data in less than 50 lines of code
class BasicApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
def error(self, reqId: TickerId, errorCode:int, errorString:str):
print('Error:', reqId, " ", errorCode, " ", errorString)
@iswrapper
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
super().tickPrice(reqId, tickType, price, attrib)
print("Tick Price. Ticker Id:", reqId, "tickType:", tickType, "Price:", price, "CanAutoExecute:", attrib.canAutoExecute, "PastLimit", attrib.pastLimit)
@iswrapper
def tickSize(self, reqId: TickerId, tickType: TickType, size: int):
super().tickSize(reqId, tickType, size)
print("Tick Size. Ticker Id:", reqId, "tickType:", tickType, "Size:", size)
@iswrapper
def tickString(self, reqId: TickerId, tickType: TickType, value: str):
super().tickString(reqId, tickType, value)
print("Tick string. Ticker Id:", reqId, "Type:", tickType, "Value:", value)
@iswrapper
def tickGeneric(self, reqId: TickerId, tickType: TickType, value: float):
super().tickGeneric(reqId, tickType, value)
print("Tick Generic. Ticker Id:", reqId, "tickType:", tickType, "Value:", value)
def main():
app = BasicApp()
app.connect("127.0.0.1", 4001, 0)
contract = Contract();
contract.symbol = "VIX";
contract.secType = "FUT";
contract.exchange = "CFE";
contract.currency = "USD";
contract.lastTradeDateOrContractMonth = "20170719";
app.reqMktData(1001, contract, "", False, False, [])
app.run()
if __name__ == '__main__':
main()
這是上面打印 IB 響應的包裝器:
Error: -1 2119 Market data farm is connecting:usfuture.us
Error: -1 2104 Market data farm connection is OK:usfuture.us
Tick string. Ticker Id: 1001 Type: 45 Value: 1499398651
Tick Price. Ticker Id: 1001 tickType: 4 Price: 13.0 CanAutoExecute: False PastLimit False
Tick Size. Ticker Id: 1001 tickType: 5 Size: 1
Tick Size. Ticker Id: 1001 tickType: 5 Size: 1
Tick Size. Ticker Id: 1001 tickType: 8 Size: 3072
Tick Price. Ticker Id: 1001 tickType: 6 Price: 13.15 CanAutoExecute: False PastLimit False
Tick Price. Ticker Id: 1001 tickType: 7 Price: 12.95 CanAutoExecute: False PastLimit False
Tick Price. Ticker Id: 1001 tickType: 9 Price: 13.0 CanAutoExecute: False PastLimit False
Tick Price. Ticker Id: 1001 tickType: 14 Price: 12.3 CanAutoExecute: False PastLimit False
Tick Price. Ticker Id: 1001 tickType: 1 Price: 12.95 CanAutoExecute: True PastLimit False
Tick Size. Ticker Id: 1001 tickType: 0 Size: 140
Tick Price. Ticker Id: 1001 tickType: 2 Price: 13.0 CanAutoExecute: True PastLimit False
Tick Size. Ticker Id: 1001 tickType: 3 Size: 138
Tick Size. Ticker Id: 1001 tickType: 0 Size: 140
Tick Size. Ticker Id: 1001 tickType: 3 Size: 138
要停止當前合約的流數據,請調用
app.cancelMktData(tickerId);
對於tickerId,請使用與 app.reqMktData 中相同的值(在您的示例中為 1001)。
保持最后的價格應該不是問題。 插入
lastPrice = price;
在 tickPrice 方法中。
您可以通過使用不同的tickerId 調用app.reqMktData 來並行接收多個工具的市場數據。 在這種情況下,lastprice 可以存儲在一個集合中(例如,使用tickerId 作為鍵的字典)。
目標 1:可以通過以下代碼實現:
class BasicApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
self.reqId_last_price_dict = {} # This dictionary uses reqId as key, last price as value
@iswrapper
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
super().tickPrice(reqId, tickType, price, attrib)
print("Tick Price. Ticker Id:", reqId, "tickType:", tickType, "Price:", price, "CanAutoExecute:", attrib.canAutoExecute, "PastLimit", attrib.pastLimit)
self.reqId_last_price_dict[reqId] = price # This line updates the dictionary value with the same reqId whenever a new price returned.
目標 2:
如果您訂閱了他們的其中一個實時訂閱源,IB 允許 3 個同時的市場報價數據請求,如下面的文檔所示:
限制考慮到潛在的大量數據被發送,市場深度請求的限制要大得多。 與歷史數據請求一樣,活躍深度請求的數量與市場數據線的數量有關,最少3條,最多60條:
沒有技術困難,只需同時請求3個不同reqIds的3個合約的報價數據。 我也喜歡初始化一個字典來存儲合同信息,以便在將數據寫入數據庫時,但不要忘記在請求數據之前將 reqId 和合同添加到字典中。
class BasicApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
self.reqId_last_price_dict = {} # This dictionary uses reqId as key, last price as value
self.reqId_contract_dict = {} # This dictionary uses reqId as key, contract as value
@iswrapper
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib):
super().tickPrice(reqId, tickType, price, attrib)
print("Tick Price. Ticker Id:", reqId, "tickType:", tickType, "Price:", price, "CanAutoExecute:", attrib.canAutoExecute, "PastLimit", attrib.pastLimit)
self.reqId_last_price_dict[reqId] = price # This line updates the dictionary value with the same reqId whenever a new price returned.
print('last price for {} is {}.'.format(self.reqId_contract_dict[reqId].symbol, price))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.