def test_Option_midpoint(): id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None) opt_id = OptionId( underlying_id=id, asset_type=AssetType.Option, expiration=datetime.date(2018, 9, 21), strike=100, right=RightType.Call, multiplier=100, contract=None, ) time = datetime.datetime.now() opt = Option(id=opt_id, high=10.0, low=5.0, close=8.0, bid=6.0, bid_size=100, ask=7.0, ask_size=130, last=7.5, last_size=67.0, option_price=2.1, volume=1000, delta=0.98, gamma=0.12, theta=0.34, vega=0.78, iv=0.8, underlying_price=102.0, underlying_dividends=2.1, time=time) assert opt.midpoint == 6.5
def create_options(self, asset: Asset, q_contracts: List[Contract]) -> Dict[str, Option]: tickers = [] for q in chunks(q_contracts, 50): tickers += self._broker.reqTickers(*q) self._broker.sleep(1) # options = [] options = {} for t in tickers: expiration = parse_ib_date(t.contract.lastTradeDateOrContractMonth) strike = float(t.contract.strike) right = RightType.Call if t.contract.right == "C" else RightType.Put delta = gamma = theta = vega = None option_price = (implied_volatility ) = underlying_price = underlying_dividends = None if t.modelGreeks: delta = t.modelGreeks.delta gamma = t.modelGreeks.gamma theta = t.modelGreeks.theta vega = t.modelGreeks.vega option_price = t.modelGreeks.optPrice implied_volatility = t.modelGreeks.impliedVol underlying_price = t.modelGreeks.undPrice underlying_dividends = t.modelGreeks.pvDividend opt_id = OptionId( underlying_id=asset.id, asset_type=AssetType.Option, expiration=expiration, strike=strike, right=right, multiplier=t.contract.multiplier, contract=t.contract, ) opt = Option( id=opt_id, high=t.high, low=t.low, close=t.close, bid=t.bid if not t.bid == -1 else None, bid_size=t.bidSize, ask=t.ask if not t.ask == -1 else None, ask_size=t.askSize, last=t.last, last_size=t.lastSize, option_price=option_price, volume=t.volume, delta=delta, gamma=gamma, theta=theta, vega=vega, iv=implied_volatility, underlying_price=underlying_price, underlying_dividends=underlying_dividends, time=t.time, ) # options.append(opt) options[f"{strike}{right.value}"] = opt return options
def test_Option_init(): id = AssetId("SPY", AssetType.Stock, Currency.USDollar, None) opt_id = OptionId( underlying_id=id, asset_type=AssetType.Option, expiration=datetime.date(2018, 9, 21), strike=100, right=RightType.Call, multiplier=100, contract=None, ) time = datetime.datetime.now() opt = Option(id=opt_id, high=10.0, low=5.0, close=8.0, bid=6.0, bid_size=100, ask=7.0, ask_size=130, last=7.5, last_size=67.0, option_price=2.1, volume=1000, delta=0.98, gamma=0.12, theta=0.34, vega=0.78, iv=0.8, underlying_price=102.0, underlying_dividends=2.1, time=time) assert opt.id.underlying_id.code == 'SPY' assert opt.high == 10.0 assert opt.low == 5.0 assert opt.close == 8.0 assert opt.bid == 6.0 assert opt.bid_size == 100 assert opt.ask == 7.0 assert opt.ask_size == 130 assert opt.last == 7.5 assert opt.last_size == 67.0 assert opt.option_price == 2.1 assert opt.volume == 1000 assert opt.delta == 0.98 assert opt.gamma == 0.12 assert opt.theta == 0.34 assert opt.vega == 0.78 assert opt.iv == 0.8 assert opt.underlying_price == 102.0 assert opt.underlying_dividends == 2.1 assert opt.time == time