我怎么知道我从ib_api reqMktData中检索了数据?

时间:2019-10-04 12:01:29

标签: python interactive-brokers ib-api

我是Python的新手,并开始使用Interactive Brokers API。我的书面账户中有一组期权,并希望检索与每种工具相关的风险。

我仅从代码(DIS ...)中定义的一个期权合约开始。 我了解到我缺少一些权限,但是我应该能够使用ticktype = 83(基于期权和相关价格的最后收盘价)的tickOptionComputation。

我在哪里将ticktype指定为83?它应该在覆盖的tickPrice方法内(并且我需要在此处添加它) 在取消订阅之前,如何确定已填充all_risk数据框?我需要以某种方式告诉reqMktData停止(通过cancelMktData),否则我的数据框将永远无法填充,而我只是打印更新。

import datetime
from ibapi.ticktype import *
path = '/Volumes/'
filename_Risk = path + 'Risk/Risk_'

def read_risk():
    from ibapi.client import EClient
    from ibapi.wrapper import EWrapper
    from ibapi.common import TickerId
    from ibapi.contract import Contract
    import pandas as pd

    class ib_class(EWrapper, EClient):
        def __init__(self): EClient.__init__(self, self)
            self.all_risk = pd.DataFrame([], columns=['TickerId', 
            'tickType', 'ImpliedVolatility', 'Delta', 'OptionPrice',                                                    
            'pvDividend', 'Gamma', 'Vega', 'Theta', 
            'UnderlyingPrice'])

        def error(self, reqId: TickerId, errorCode: int, errorString: str):
            if reqId > -1:
                print("Error. Id: ", reqId, " Code: ", errorCode, " Msg: ", errorString)

        def tickOptionComputation(self, reqId: TickerId, tickType: TickType, impliedVol: float, delta: float,
                              optPrice: float, pvDividend: float,
                              gamma: float, vega: float, theta: float, undPrice: float):
            super().tickOptionComputation(reqId, tickType, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta,
                                      undPrice)
            print("TickOptionComputation. TickerId:", reqId, "tickType:", tickType, "ImpliedVolatility:", impliedVol,
              "Delta:", delta, "OptionPrice:", optPrice, "pvDividend:", pvDividend, "Gamma: ", gamma, "Vega:", vega,
              "Theta:", theta, "UnderlyingPrice:", undPrice)
            self.all_risk.loc[str(TickerId)] = reqId, tickType, impliedVol, delta, optPrice, pvDividend, gamma, vega, theta, undPrice

    ib_api = ib_class()
    ib_api.connect("127.0.0.1", 7497, 1)

    #Option Contract example
    contract = Contract()
    contract.symbol = "DIS"
    contract.secType = "OPT"
    contract.exchange = "BOX"
    contract.currency = "USD"
    contract.lastTradeDateOrContractMonth = "20191101"
    contract.strike = 126
    contract.right = "P"
    contract.multiplier = "100"

    ib_api.reqMktData(104, contract, "", False, False, [])
    ib_api.tickSnapshotEnd(104)
    current_risk = ib_api.all_risk
    ib_api.run()

    return (current_risk)

if __name__ == '__main__':
    print("List of Risk")
    all_risk = read_risk()
    all_risk.to_csv(filename_Risk + datetime.date.fromordinal(datetime.date.today().toordinal()).strftime("%Y-%m-%d") + ".csv", index=None, header=True)
    print(all_risk)

目前这就是我得到的:

List of Risk

Error. Id:  104  Code:  10090  Msg:  Part of requested market data is not subscribed. Subscription-independent ticks are still active.Delayed market data is available.DIS NYSE/TOP/ALL

TickOptionComputation. TickerId: 104 tickType: 83 ImpliedVolatility: 0.22862237443433486 Delta: -0.37556209487070186 OptionPrice: 2.208775770605327 Dividend: 0.0 Gamma:  0.04671156123062818 Vega: 0.13482689929523772 Theta: -0.052288197357214475 UnderlyingPrice: 128.16

TickOptionComputation. TickerId: 104 tickType: 80 ImpliedVolatility: None Delta: None OptionPrice: None Dividend: 0.0 Gamma:  None Vega: None Theta: None UnderlyingPrice: 128.16

TickOptionComputation. TickerId: 104 tickType: 81 ImpliedVolatility: None Delta: None OptionPrice: None Dividend: 0.0 Gamma:  None Vega: None Theta: None UnderlyingPrice: 128.16

TickOptionComputation. TickerId: 104 tickType: 82 ImpliedVolatility: None Delta: None OptionPrice: None Dividend: 0.0 Gamma:  None Vega: None Theta: None UnderlyingPrice: 128.16

TickOptionComputation. TickerId: 104 tickType: 83 ImpliedVolatility: 0.22862237443433486 Delta: -0.37556109977905566 OptionPrice: 2.2087398478046607 Dividend: 0.0 Gamma:  0.04671208125635446 Vega: 0.13482525175105353 Theta: -0.05228881750759203 UnderlyingPrice: 128.16

TickOptionComputation. TickerId: 104 tickType: 83 ImpliedVolatility: 0.22862237443433486 Delta: -0.3755601046450712 OptionPrice: 2.2087039245384745 pvDividend: 0.0 Gamma:  0.0467126012999343 Vega: 0.13482360418650208 Theta: -0.05228943767924362 UnderlyingPrice: 128.16

还有一个问题,当市场关闭时,我不能从BOX交易所得到任何东西,我需要切换到另一个吗? (NYSE / TOP / ALL或ARCA,但我缺少那里的市场订阅)

0 个答案:

没有答案