bugfixing
This commit is contained in:
parent
00010b7ab4
commit
5dc5e30730
@ -887,7 +887,7 @@ async def get_highest_option_iv_rank():
|
|||||||
total_oi = stock_screener_data_dict[symbol].get('totalOI',0)
|
total_oi = stock_screener_data_dict[symbol].get('totalOI',0)
|
||||||
change_oi = stock_screener_data_dict[symbol].get('changeOI',0)
|
change_oi = stock_screener_data_dict[symbol].get('changeOI',0)
|
||||||
|
|
||||||
if total_oi > 1E6 and iv_rank > 0:
|
if total_oi > 1E6 and iv_rank > 0 and iv_rank < 100:
|
||||||
quote_data = await get_quote_data(symbol)
|
quote_data = await get_quote_data(symbol)
|
||||||
# Assign price and volume, and check if they meet the penny stock criteria
|
# Assign price and volume, and check if they meet the penny stock criteria
|
||||||
if quote_data:
|
if quote_data:
|
||||||
@ -966,7 +966,7 @@ async def get_highest_option_premium():
|
|||||||
if res_list:
|
if res_list:
|
||||||
# Sort by market cap in descending order
|
# Sort by market cap in descending order
|
||||||
res_list = sorted(res_list, key=lambda x: x['totalPrem'], reverse=True)[:50]
|
res_list = sorted(res_list, key=lambda x: x['totalPrem'], reverse=True)[:50]
|
||||||
|
|
||||||
# Assign rank to each stock
|
# Assign rank to each stock
|
||||||
for rank, item in enumerate(res_list, start=1):
|
for rank, item in enumerate(res_list, start=1):
|
||||||
item['rank'] = rank
|
item['rank'] = rank
|
||||||
|
|||||||
@ -22,7 +22,6 @@ query_template = """
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def save_json(data, symbol):
|
def save_json(data, symbol):
|
||||||
directory_path = f"json/options-historical-data/companies"
|
directory_path = f"json/options-historical-data/companies"
|
||||||
os.makedirs(directory_path, exist_ok=True) # Ensure the directory exists
|
os.makedirs(directory_path, exist_ok=True) # Ensure the directory exists
|
||||||
@ -36,29 +35,141 @@ def safe_round(value, decimals=2):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def calculate_iv_rank_for_all(data):
|
def aggregate_data_by_date(symbol):
|
||||||
# Extract all IV values
|
data_by_date = defaultdict(lambda: {
|
||||||
iv_values = [entry['iv'] for entry in data if 'iv' in entry]
|
"date": "",
|
||||||
|
"call_volume": 0,
|
||||||
|
"put_volume": 0,
|
||||||
|
"call_open_interest": 0,
|
||||||
|
"put_open_interest": 0,
|
||||||
|
"call_premium": 0,
|
||||||
|
"put_premium": 0,
|
||||||
|
"iv": 0.0, # Sum of implied volatilities
|
||||||
|
"iv_count": 0, # Count of entries for IV
|
||||||
|
})
|
||||||
|
|
||||||
|
# Calculate cutoff date (1 year ago)
|
||||||
|
today = datetime.today().date()
|
||||||
|
one_year_ago = today - timedelta(days=365)
|
||||||
|
one_year_ago_str = one_year_ago.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
|
contract_dir = f"json/all-options-contracts/{symbol}"
|
||||||
|
contract_list = get_contracts_from_directory(contract_dir)
|
||||||
|
|
||||||
if not iv_values:
|
if len(contract_list) > 0:
|
||||||
return None # No IV data available
|
for item in contract_list:
|
||||||
|
try:
|
||||||
# Compute highest and lowest IV
|
file_path = os.path.join(contract_dir, f"{item}.json")
|
||||||
highest_iv = max(iv_values)
|
with open(file_path, "r") as file:
|
||||||
lowest_iv = min(iv_values)
|
data = orjson.loads(file.read())
|
||||||
|
|
||||||
# Calculate IV Rank for each entry
|
option_type = data.get('optionType', None)
|
||||||
for entry in data:
|
if option_type not in ['call', 'put']:
|
||||||
if 'iv' in entry:
|
continue
|
||||||
iv = entry['iv']
|
|
||||||
if highest_iv == lowest_iv:
|
for entry in data.get('history', []):
|
||||||
entry['iv_rank'] = 100.0 # If all IVs are the same, rank is 100%
|
date = entry.get('date')
|
||||||
|
# Skip entries older than one year
|
||||||
|
if date < one_year_ago_str:
|
||||||
|
continue
|
||||||
|
|
||||||
|
volume = entry.get('volume', 0) or 0
|
||||||
|
open_interest = entry.get('open_interest', 0) or 0
|
||||||
|
total_premium = entry.get('total_premium', 0) or 0
|
||||||
|
implied_volatility = entry.get('implied_volatility', 0) or 0
|
||||||
|
|
||||||
|
daily_data = data_by_date[date]
|
||||||
|
daily_data["date"] = date
|
||||||
|
|
||||||
|
if option_type == 'call':
|
||||||
|
daily_data["call_volume"] += int(volume)
|
||||||
|
daily_data["call_open_interest"] += int(open_interest)
|
||||||
|
daily_data["call_premium"] += int(total_premium)
|
||||||
|
elif option_type == 'put':
|
||||||
|
daily_data["put_volume"] += int(volume)
|
||||||
|
daily_data["put_open_interest"] += int(open_interest)
|
||||||
|
daily_data["put_premium"] += int(total_premium)
|
||||||
|
|
||||||
|
# Aggregate IV for both calls and puts
|
||||||
|
daily_data["iv"] += round(implied_volatility, 2)
|
||||||
|
daily_data["iv_count"] += 1
|
||||||
|
|
||||||
|
# Calculate put/call ratio
|
||||||
|
try:
|
||||||
|
daily_data["putCallRatio"] = round(daily_data["put_volume"] / daily_data["call_volume"], 2)
|
||||||
|
except ZeroDivisionError:
|
||||||
|
daily_data["putCallRatio"] = None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing {item}: {e}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Convert to list and calculate average IV
|
||||||
|
data = []
|
||||||
|
for date, daily in data_by_date.items():
|
||||||
|
if daily['iv_count'] > 0:
|
||||||
|
daily['iv'] = round(daily['iv'] / daily['iv_count'], 2)
|
||||||
else:
|
else:
|
||||||
entry['iv_rank'] = round(((iv - lowest_iv) / (highest_iv - lowest_iv)) * 100,2)
|
daily['iv'] = None
|
||||||
else:
|
data.append(daily)
|
||||||
entry['iv_rank'] = None # Handle missing IV
|
|
||||||
|
# Sort and calculate IV Rank
|
||||||
|
data = sorted(data, key=lambda x: x['date'])
|
||||||
|
data = calculate_iv_rank_for_all(data)
|
||||||
|
data = sorted(data, key=lambda x: x['date'], reverse=True)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
else:
|
||||||
|
return []
|
||||||
|
|
||||||
|
def calculate_iv_rank_for_all(data):
|
||||||
|
if not data:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Convert to DataFrame
|
||||||
|
df = pd.DataFrame(data)
|
||||||
|
|
||||||
|
# Check if 'iv' exists and filter out entries without IV
|
||||||
|
if 'iv' not in df.columns or df['iv'].isnull().all():
|
||||||
|
for entry in data:
|
||||||
|
entry['iv_rank'] = None
|
||||||
|
return data
|
||||||
|
|
||||||
|
# Convert date to datetime and sort
|
||||||
|
df['date'] = pd.to_datetime(df['date'])
|
||||||
|
df.sort_values('date', inplace=True)
|
||||||
|
|
||||||
|
# Calculate rolling 365-day min and max for IV
|
||||||
|
df.set_index('date', inplace=True)
|
||||||
|
rolling_min = df['iv'].rolling('365D', min_periods=1).min()
|
||||||
|
rolling_max = df['iv'].rolling('365D', min_periods=1).max()
|
||||||
|
|
||||||
|
# Merge back into DataFrame
|
||||||
|
df['rolling_min'] = rolling_min
|
||||||
|
df['rolling_max'] = rolling_max
|
||||||
|
|
||||||
|
# Calculate IV Rank
|
||||||
|
df['iv_rank'] = ((df['iv'] - df['rolling_min']) / (df['rolling_max'] - df['rolling_min'])) * 100
|
||||||
|
df['iv_rank'] = df['iv_rank'].round(2)
|
||||||
|
|
||||||
|
# Handle cases where max == min
|
||||||
|
df.loc[df['rolling_max'] == df['rolling_min'], 'iv_rank'] = 100.0
|
||||||
|
|
||||||
|
# Replace NaN with None
|
||||||
|
df['iv_rank'] = df['iv_rank'].where(pd.notnull(df['iv_rank']), None)
|
||||||
|
|
||||||
|
# Drop temporary columns
|
||||||
|
df.drop(['rolling_min', 'rolling_max'], axis=1, inplace=True)
|
||||||
|
|
||||||
|
# Convert back to list of dicts
|
||||||
|
df.reset_index(inplace=True)
|
||||||
|
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
|
||||||
|
result = df.to_dict('records')
|
||||||
|
|
||||||
|
# Sort in reverse chronological order
|
||||||
|
result = sorted(result, key=lambda x: x['date'], reverse=True)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def prepare_data(data, symbol):
|
def prepare_data(data, symbol):
|
||||||
@ -155,84 +266,6 @@ def get_contracts_from_directory(directory: str):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
def aggregate_data_by_date(symbol):
|
|
||||||
data_by_date = defaultdict(lambda: {
|
|
||||||
"date": "",
|
|
||||||
"call_volume": 0,
|
|
||||||
"put_volume": 0,
|
|
||||||
"call_open_interest": 0,
|
|
||||||
"put_open_interest": 0,
|
|
||||||
"call_premium": 0,
|
|
||||||
"call_net_premium": 0,
|
|
||||||
"put_premium": 0,
|
|
||||||
"put_net_premium": 0,
|
|
||||||
"iv": 0, # Sum of implied volatilities
|
|
||||||
"iv_count": 0, # Count of entries for IV
|
|
||||||
})
|
|
||||||
|
|
||||||
contract_dir = f"json/all-options-contracts/{symbol}"
|
|
||||||
contract_list = get_contracts_from_directory(contract_dir)
|
|
||||||
|
|
||||||
if len(contract_list) > 0:
|
|
||||||
|
|
||||||
for item in contract_list:
|
|
||||||
try:
|
|
||||||
file_path = os.path.join(contract_dir, f"{item}.json")
|
|
||||||
with open(file_path, "r") as file:
|
|
||||||
data = orjson.loads(file.read())
|
|
||||||
|
|
||||||
option_type = data.get('optionType', None)
|
|
||||||
if option_type not in ['call', 'put']:
|
|
||||||
continue
|
|
||||||
|
|
||||||
for entry in data.get('history', []):
|
|
||||||
date = entry.get('date')
|
|
||||||
volume = entry.get('volume', 0) or 0
|
|
||||||
open_interest = entry.get('open_interest', 0) or 0
|
|
||||||
total_premium = entry.get('total_premium', 0) or 0
|
|
||||||
implied_volatility = entry.get('implied_volatility', 0) or 0
|
|
||||||
|
|
||||||
if date:
|
|
||||||
daily_data = data_by_date[date]
|
|
||||||
daily_data["date"] = date
|
|
||||||
|
|
||||||
if option_type == 'call':
|
|
||||||
daily_data["call_volume"] += int(volume)
|
|
||||||
daily_data["call_open_interest"] += int(open_interest)
|
|
||||||
daily_data["call_premium"] += int(total_premium)
|
|
||||||
elif option_type == 'put':
|
|
||||||
daily_data["put_volume"] += int(volume)
|
|
||||||
daily_data["put_open_interest"] += int(open_interest)
|
|
||||||
daily_data["put_premium"] += int(total_premium)
|
|
||||||
daily_data["iv"] += round(implied_volatility, 2)
|
|
||||||
daily_data["iv_count"] += 1
|
|
||||||
|
|
||||||
try:
|
|
||||||
daily_data["putCallRatio"] = round(daily_data["put_volume"] / daily_data["call_volume"], 2)
|
|
||||||
except ZeroDivisionError:
|
|
||||||
daily_data["putCallRatio"] = None
|
|
||||||
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Convert to list of dictionaries and sort by date
|
|
||||||
data = list(data_by_date.values())
|
|
||||||
for daily_data in data:
|
|
||||||
try:
|
|
||||||
if daily_data["iv_count"] > 0:
|
|
||||||
daily_data["iv"] = round(daily_data["iv"] / daily_data["iv_count"], 2)
|
|
||||||
else:
|
|
||||||
daily_data["iv"] = None # Or set it to 0 if you prefer
|
|
||||||
except:
|
|
||||||
daily_data["iv"] = None
|
|
||||||
|
|
||||||
data = sorted(data, key=lambda x: x['date'], reverse=True)
|
|
||||||
data = calculate_iv_rank_for_all(data)
|
|
||||||
|
|
||||||
return data
|
|
||||||
else:
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -253,7 +286,7 @@ etf_symbols = [row[0] for row in etf_cursor.fetchall()]
|
|||||||
total_symbols = stocks_symbols + etf_symbols
|
total_symbols = stocks_symbols + etf_symbols
|
||||||
|
|
||||||
|
|
||||||
for symbol in tqdm(total_symbols):
|
for symbol in tqdm(['AAPL']):
|
||||||
try:
|
try:
|
||||||
data = aggregate_data_by_date(symbol)
|
data = aggregate_data_by_date(symbol)
|
||||||
data = prepare_data(data, symbol)
|
data = prepare_data(data, symbol)
|
||||||
|
|||||||
@ -916,27 +916,21 @@ async def get_stock_screener(con):
|
|||||||
item['shortFloatPercent'] = None
|
item['shortFloatPercent'] = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(f"json/options-stats/companies/{symbol}.json", "r") as file:
|
with open(f"json/options-historical-data/companies/{symbol}.json", "r") as file:
|
||||||
res = orjson.loads(file.read())
|
res = orjson.loads(file.read())[0]
|
||||||
item['gexRatio'] = res['gex_ratio']
|
|
||||||
item['ivRank'] = res['iv_rank']
|
item['ivRank'] = res['iv_rank']
|
||||||
item['iv30d'] = res['iv30d']
|
item['iv30d'] = res['iv']
|
||||||
item['totalOI'] = res['total_open_interest']
|
item['totalOI'] = res['total_open_interest']
|
||||||
item['changeOI'] = res['open_interest_change']
|
item['changeOI'] = res['changeOI']
|
||||||
item['netCallPrem'] = res['net_call_premium']
|
|
||||||
item['netPutPrem'] = res['net_put_premium']
|
|
||||||
item['callVolume'] = res['call_volume']
|
item['callVolume'] = res['call_volume']
|
||||||
item['putVolume'] = res['put_volume']
|
item['putVolume'] = res['put_volume']
|
||||||
item['pcRatio'] = res['put_call_ratio']
|
item['pcRatio'] = res['putCallRatio']
|
||||||
item['totalPrem'] = res['call_premium']+res['put_premium']
|
item['totalPrem'] = res['total_premium']
|
||||||
except:
|
except:
|
||||||
item['gexRatio'] = None
|
|
||||||
item['ivRank'] = None
|
item['ivRank'] = None
|
||||||
item['iv30d'] = None
|
item['iv30d'] = None
|
||||||
item['totalOI'] = None
|
item['totalOI'] = None
|
||||||
item['changeOI'] = None
|
item['changeOI'] = None
|
||||||
item['netCallPrem'] = None
|
|
||||||
item['netPutPrem'] = None
|
|
||||||
item['callVolume'] = None
|
item['callVolume'] = None
|
||||||
item['putVolume'] = None
|
item['putVolume'] = None
|
||||||
item['pcRatio'] = None
|
item['pcRatio'] = None
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user