From 50b9bc72a98d6f61941500be02d54559983cae9f Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 24 Oct 2024 13:36:17 +0200 Subject: [PATCH] update cron job && hedge fund latest quarter --- app/create_institute_db.py | 2 +- app/create_stock_db.py | 2 +- app/support.py | 38 ++++++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/app/create_institute_db.py b/app/create_institute_db.py index 2e5264d..de113b9 100755 --- a/app/create_institute_db.py +++ b/app/create_institute_db.py @@ -62,7 +62,7 @@ crypto_con.close() load_dotenv() api_key = os.getenv('FMP_API_KEY') -quarter_date = '2024-06-30' +quarter_date = '2024-09-30' if os.path.exists("backup_db/institute.db"): diff --git a/app/create_stock_db.py b/app/create_stock_db.py index 37ace3b..b294879 100755 --- a/app/create_stock_db.py +++ b/app/create_stock_db.py @@ -27,7 +27,7 @@ warnings.filterwarnings("ignore", category=RuntimeWarning, message="invalid valu start_date = datetime(2015, 1, 1).strftime("%Y-%m-%d") end_date = datetime.today().strftime("%Y-%m-%d") -quarter_date = '2024-6-30' +quarter_date = '2024-09-30' if os.path.exists("backup_db/stocks.db"): diff --git a/app/support.py b/app/support.py index 6c0d80a..727e931 100644 --- a/app/support.py +++ b/app/support.py @@ -16,11 +16,24 @@ def calculate_volatility(prices_df): returns = prices_df['return'].dropna() return returns.std() * np.sqrt(252) + # Load API key from environment load_dotenv() api_key = os.getenv('BENZINGA_API_KEY') fin = financial_data.Benzinga(api_key) +# Connect to SQLite database stock_con = sqlite3.connect('stocks.db') +etf_con = sqlite3.connect('etf.db') + +stock_cursor = stock_con.cursor() +stock_cursor.execute("PRAGMA journal_mode = wal") +stock_cursor.execute("SELECT DISTINCT symbol FROM stocks WHERE symbol NOT LIKE '%.%' AND marketCap >= 500E6") +stock_symbols = [row[0] for row in stock_cursor.fetchall()] + +etf_cursor = etf_con.cursor() +etf_cursor.execute("PRAGMA journal_mode = wal") +etf_cursor.execute("SELECT DISTINCT symbol FROM etfs") +etf_symbols = [row[0] for row in etf_cursor.fetchall()] query_template = """ @@ -30,20 +43,22 @@ query_template = """ """ -ticker = 'NVDA' +ticker = 'SPY' end_date = date.today() start_date = end_date - timedelta(1) end_date_str = end_date.strftime('%Y-%m-%d') start_date_str = start_date.strftime('%Y-%m-%d') query = query_template.format(ticker=ticker) -df_price = pd.read_sql_query(query, stock_con, params=('2024-01-01', end_date_str)).round(2) +df_price = pd.read_sql_query(query, stock_con if ticker in stock_symbols else etf_con, params=('2024-01-01', end_date_str)).round(2) df_price = df_price.rename(columns={"change_percent": "changesPercentage"}) volatility = calculate_volatility(df_price) print(start_date, end_date) print('volatility', volatility) +stock_con.close() +etf_con.close() def get_data(ticker): res_list = [] @@ -80,14 +95,15 @@ def calculate_option_greeks(S, K, T, r, sigma, option_type='CALL'): return 0, 0 d1 = (np.log(S / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T)) - d2 = d1 - sigma * np.sqrt(T) + #d2 = d1 - sigma * np.sqrt(T) if option_type == 'CALL': delta = norm.cdf(d1) - gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) + #gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) else: # PUT - delta = -norm.cdf(-d1) - gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) + delta = norm.cdf(d1) - 1 #-norm.cdf(-d1) + #gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) + gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) if S > 0 and sigma > 0 and np.sqrt(T) > 0 else 0 return delta, gamma @@ -98,6 +114,7 @@ def process_options_data(df): # Convert data types df['strike_price'] = pd.to_numeric(df['strike_price']) df['volume'] = pd.to_numeric(df['volume']) + df['open_interest'] = pd.to_numeric(df['open_interest']) df['underlying_price'] = pd.to_numeric(df['underlying_price']) df['date_expiration'] = pd.to_datetime(df['date_expiration']) df['date'] = pd.to_datetime(df['date']) @@ -124,18 +141,19 @@ def process_options_data(df): # Calculate DEX (Delta Exposure) and GEX (Gamma Exposure) # Convert volume to float if it's not already - df['volume'] = df['volume'].astype(float) + df['volume'] = df['volume'].astype(int) + df['open_interest'] = df['open_interest'].astype(float) # Get price per contract df['price'] = pd.to_numeric(df['price']) # Calculate position values contract_multiplier = 100 # Standard option contract multiplier - df['position_value'] = df['price'] * df['volume'] * contract_multiplier + df['position_value'] = df['price'] * df['open_interest'] * contract_multiplier # Calculate exposures - df['dex'] = df['delta'] * df['volume'] * contract_multiplier - df['gex'] = df['gamma'] * df['volume'] * contract_multiplier * df['underlying_price'] * 0.01 + df['gex'] = df['gamma'] * df['volume'] * contract_multiplier #df['gamma'] * df['open_interest'] * df['volume'] * df['underlying_price'] + df['dex'] = df['delta'] * df['volume'] * contract_multiplier * df['underlying_price'] * 0.01 #df['delta'] * df['volume'] * df['underlying_price'] *0.01 return df