1

我是 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,但我错过了那里的市场订阅)

4

0 回答 0