I am new to Python and started playing around with the Interactive Brokers API. I have a portfolio of options in my paper account and would like to retrieve the risks associated with each instrument.
I started with just a single option contract which I define within the code (DIS...). I understand that I am missing some permissions but I should be able to use tickOptionComputation with ticktype = 83 (based on last close for both option and underlying price).
Where do I specify the ticktype to be be 83? should it be inside the overwritten tickPrice method (and I would need to add it here) How can I be sure my all_risk dataframe is populated before canceling the subscription? I need to somehow tell the reqMktData to stop (via cancelMktData) otherwise my dataframe is never filled and I am just printing updates.
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)
Currently this is what I get:
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
As an extra question, when the market is closed, I can't get anything from the BOX exchange, do I need to switch to another one? (NYSE/TOP/ALL or ARCA but I am missing the market subscription there)