mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
fix(cboe): term structure skips a side with no out-of-the-money strike (#7846)
An expiry that lists a single strike has no OTM strike on one side. The strike lookup returned None and the subtraction raised a TypeError for the whole chart. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: dlee-bq <dlee@brightquery.com>
This commit is contained in:
@@ -102,7 +102,8 @@ def create_term_structure(
|
||||
& (df[price_col] > 0)
|
||||
]
|
||||
|
||||
if not calls_filtered.empty:
|
||||
# An expiry with no out-of-the-money strike on a side has no point on that side.
|
||||
if call_strike is not None and not calls_filtered.empty:
|
||||
calls = concat(
|
||||
[
|
||||
calls,
|
||||
@@ -118,7 +119,7 @@ def create_term_structure(
|
||||
& (df[price_col] > 0)
|
||||
]
|
||||
|
||||
if not puts_filtered.empty:
|
||||
if put_strike is not None and not puts_filtered.empty:
|
||||
puts = concat(
|
||||
[
|
||||
puts,
|
||||
|
||||
@@ -260,6 +260,64 @@ class TestTermStructure:
|
||||
|
||||
assert len(output.chart.content["data"]) == 1
|
||||
|
||||
def test_expiry_without_an_otm_strike_on_one_side(self):
|
||||
"""An expiry listing one strike has no OTM put; that side is skipped."""
|
||||
import time_machine
|
||||
|
||||
from openbb_cboe.models.options_chains import CboeOptionsChainsFetcher
|
||||
|
||||
quote = {
|
||||
"bid": 1.0,
|
||||
"ask": 1.1,
|
||||
"bid_size": 1,
|
||||
"ask_size": 1,
|
||||
"iv": 0.2,
|
||||
"open_interest": 5,
|
||||
"volume": 2,
|
||||
"delta": 0.5,
|
||||
"gamma": 0.1,
|
||||
"theta": -0.1,
|
||||
"vega": 0.1,
|
||||
"rho": 0.0,
|
||||
"theo": 1.05,
|
||||
"change": 0.0,
|
||||
"open": 1.0,
|
||||
"high": 1.0,
|
||||
"low": 1.0,
|
||||
"tick": "up",
|
||||
"last_trade_price": 1.0,
|
||||
"last_trade_time": "2026-11-17T15:59:59",
|
||||
"percent_change": 0.0,
|
||||
"prev_day_close": 1.0,
|
||||
}
|
||||
# The second expiry lists a single strike, above the price.
|
||||
symbols = [
|
||||
"SPY271119C00710000",
|
||||
"SPY271119P00690000",
|
||||
"SPY271202C00825000",
|
||||
"SPY271202P00825000",
|
||||
]
|
||||
payload = {
|
||||
"data": {
|
||||
"symbol": "SPY",
|
||||
"current_price": 700.0,
|
||||
"options": [{**quote, "option": symbol} for symbol in symbols],
|
||||
}
|
||||
}
|
||||
with time_machine.travel("2026-11-17", tick=True):
|
||||
chain = CboeOptionsChainsFetcher.transform_data(
|
||||
CboeOptionsChainsFetcher.transform_query({"symbol": "SPY"}), payload
|
||||
).result
|
||||
rows = create_term_structure(chain, metric="price").results
|
||||
|
||||
assert sorted(
|
||||
(str(r.expiration)[:10], r.option_type, r.strike) for r in rows
|
||||
) == [
|
||||
("2027-11-19", "call", 710.0),
|
||||
("2027-11-19", "put", 690.0),
|
||||
("2027-12-02", "call", 825.0),
|
||||
]
|
||||
|
||||
def test_requires_iv(self, options_chain):
|
||||
"""Requesting IV on a chain without it is rejected."""
|
||||
stub = type("Stub", (), {"has_iv": False})()
|
||||
|
||||
Reference in New Issue
Block a user