From 2591edd8026cbeffb7e9265e96909e05bba9786f Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 5 Feb 2025 14:59:22 +0100 Subject: [PATCH] bugfixing option stats --- app/cron_options_stats.py | 103 +++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 24 deletions(-) diff --git a/app/cron_options_stats.py b/app/cron_options_stats.py index 9431d39..8a538b7 100644 --- a/app/cron_options_stats.py +++ b/app/cron_options_stats.py @@ -28,20 +28,6 @@ def get_total_symbols(): return stocks_symbols + etf_symbols -def get_tickers_from_directory(): - directory = "json/options-historical-data/companies" - try: - # Ensure the directory exists - if not os.path.exists(directory): - raise FileNotFoundError(f"The directory '{directory}' does not exist.") - - # Get all tickers from filenames - return [file.replace(".json", "") for file in os.listdir(directory) if file.endswith(".json")] - - except Exception as e: - print(f"An error occurred: {e}") - return [] - def save_json(data, symbol): directory = "json/options-stats/companies" os.makedirs(directory, exist_ok=True) @@ -57,27 +43,96 @@ def safe_round(value): async def main(): + + with open(f"json/options-flow/feed/data.json", "r") as file: + data = orjson.loads(file.read()) + total_symbols = get_total_symbols() for symbol in tqdm(total_symbols): try: - # Load previous data and calculate changes + call_premium = 0 + put_premium = 0 + call_open_interest = 0 + put_open_interest = 0 + call_volume = 0 + put_volume = 0 + bearish_premium = 0 + bullish_premium = 0 + neutral_premium = 0 + + net_call_premium = 0 + net_put_premium = 0 + net_premium = 0 + + for item in data: + if item['ticker'] == symbol: + if item['put_call'] == 'Calls': + call_premium += item['cost_basis'] + call_open_interest += int(item['open_interest']) + call_volume += int(item['volume']) + elif item['put_call'] == 'Puts': + put_premium += item['cost_basis'] + put_open_interest += int(item['open_interest']) + put_volume += int(item['volume']) + + if item['sentiment'] == 'Bullish': + bullish_premium +=item['cost_basis'] + if item['put_call'] == 'Calls': + net_call_premium +=item['cost_basis'] + elif item['put_call'] == 'Puts': + net_put_premium +=item['cost_basis'] + + if item['sentiment'] == 'Bearish': + bearish_premium +=item['cost_basis'] + if item['put_call'] == 'Calls': + net_call_premium -=item['cost_basis'] + elif item['put_call'] == 'Puts': + net_put_premium -=item['cost_basis'] + + if item['sentiment'] == 'Neutral': + neutral_premium +=item['cost_basis'] + with open(f"json/options-historical-data/companies/{symbol}.json", "r") as file: - data = orjson.loads(file.read()) + past_data = orjson.loads(file.read())[0] + previous_open_interest = past_data['total_open_interest'] + iv_rank = past_data['iv_rank'] + iv = past_data['iv'] - # Keys to compute the average for - keys_to_average = [key for key in data[0] if key != "date"] + total_open_interest = call_open_interest+put_open_interest - # Compute averages and round to 2 decimal places - averages = { - key: round(mean(d[key] for d in data if d.get(key) is not None), 2) - for key in keys_to_average + changesPercentageOI = round((total_open_interest/previous_open_interest-1)*100, 2) if previous_open_interest > 0 else 0 + changeOI = total_open_interest - previous_open_interest + put_call_ratio = round(put_volume/call_volume,2) if call_volume > 0 else 0 + + net_premium = net_call_premium + net_put_premium + premium_ratio = [ + safe_round(bearish_premium), + safe_round(neutral_premium), + safe_round(bullish_premium) + ] + aggregate = { + "call_premium": round(call_premium,0), + "call_open_interest": round(call_open_interest,0), + "call_volume": round(call_volume,0), + "put_premium": round(put_premium,0), + "put_open_interest": round(put_open_interest,0), + "put_volume": round(put_volume,0), + "putCallRatio": round(put_volume/call_volume,0), + "total_open_interest": round(total_open_interest,0), + "changeOI": round(changeOI,0), + "changesPercentageOI": changesPercentageOI, + "iv": round(iv,2), + "iv_rank": round(iv_rank,2), + "putCallRatio": put_call_ratio, + "premium_ratio": premium_ratio, + "net_premium": round(net_premium), } - save_json(averages, symbol) + if aggregate: + save_json(aggregate, symbol) except: pass - if __name__ == "__main__": asyncio.run(main())