Add strikeLimit parameter and refresh docs
This commit is contained in:
@@ -299,7 +299,17 @@ def wait_for_tables(page):
|
||||
return []
|
||||
|
||||
|
||||
def scrape_yahoo_options(symbol, expiration=None):
|
||||
def parse_strike_limit(value, default=25):
|
||||
if value is None:
|
||||
return default
|
||||
try:
|
||||
limit = int(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
return limit if limit > 0 else default
|
||||
|
||||
|
||||
def scrape_yahoo_options(symbol, expiration=None, strike_limit=25):
|
||||
def parse_table(table_html, side):
|
||||
if not table_html:
|
||||
app.logger.warning("No %s table HTML for %s", side, symbol)
|
||||
@@ -544,7 +554,7 @@ def scrape_yahoo_options(symbol, expiration=None):
|
||||
# ----------------------------------------------------------------------
|
||||
# Pruning logic
|
||||
# ----------------------------------------------------------------------
|
||||
def prune_nearest(options, price_value, limit=26, side=""):
|
||||
def prune_nearest(options, price_value, limit=25, side=""):
|
||||
if price_value is None:
|
||||
return options, 0
|
||||
|
||||
@@ -558,8 +568,18 @@ def scrape_yahoo_options(symbol, expiration=None):
|
||||
pruned_count = len(options) - len(pruned)
|
||||
return pruned, pruned_count
|
||||
|
||||
calls, pruned_calls = prune_nearest(calls_full, price, side="calls")
|
||||
puts, pruned_puts = prune_nearest(puts_full, price, side="puts")
|
||||
calls, pruned_calls = prune_nearest(
|
||||
calls_full,
|
||||
price,
|
||||
limit=strike_limit,
|
||||
side="calls",
|
||||
)
|
||||
puts, pruned_puts = prune_nearest(
|
||||
puts_full,
|
||||
price,
|
||||
limit=strike_limit,
|
||||
side="puts",
|
||||
)
|
||||
|
||||
def strike_range(opts):
|
||||
strikes = [o["Strike"] for o in opts if isinstance(o.get("Strike"), (int, float))]
|
||||
@@ -593,12 +613,14 @@ def scrape_sync():
|
||||
or request.args.get("expiry")
|
||||
or request.args.get("date")
|
||||
)
|
||||
strike_limit = parse_strike_limit(request.args.get("strikeLimit"), default=25)
|
||||
app.logger.info(
|
||||
"Received /scrape_sync request for symbol=%s expiration=%s",
|
||||
"Received /scrape_sync request for symbol=%s expiration=%s strike_limit=%s",
|
||||
symbol,
|
||||
expiration,
|
||||
strike_limit,
|
||||
)
|
||||
return jsonify(scrape_yahoo_options(symbol, expiration))
|
||||
return jsonify(scrape_yahoo_options(symbol, expiration, strike_limit))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user