update cron job

This commit is contained in:
MuslemRahimi 2024-08-27 10:34:51 +02:00
parent 4e58058631
commit 7669f2ac4b

View File

@ -4,9 +4,11 @@ import numpy as np
import ujson
import asyncio
import sqlite3
import os
from tqdm import tqdm
async def save_json(symbol, data):
os.makedirs("json/var", exist_ok=True) # Ensure directory exists
with open(f"json/var/{symbol}.json", 'w') as file:
ujson.dump(data, file)
@ -39,10 +41,10 @@ def compute_var(df):
df = df.dropna()
# Calculate VaR at 95% confidence level
confidence_level = 0.95
var = abs(np.percentile(df['Returns'], 100 * (1 - confidence_level)))
var_N_days = round(var * np.sqrt(5)*100,2) # N days
var = np.percentile(df['Returns'], 100 * (1 - confidence_level))
var_N_days = round(var * np.sqrt(len(df)) * 100, 2) # N days: the length of df represents the N days
return -var_N_days #{'rating': risk_rating, 'var': -var_N_days, 'outlook': outlook}
return var_N_days # Positive value represents a loss
async def run():
start_date = "2015-01-01"
@ -70,12 +72,15 @@ async def run():
total_symbols = stocks_symbols + etf_symbols + crypto_symbols
for symbol in tqdm(total_symbols):
try:
if symbol in etf_symbols:
query_con = etf_con
elif symbol in crypto_symbols:
query_con = crypto_con
elif symbol in stocks_symbols:
query_con = con
else:
continue
query_template = """
SELECT
@ -93,10 +98,10 @@ async def run():
# Group by year and month
monthly_groups = df.groupby(df['date'].dt.to_period('M'))
history = []
try:
for period, group in monthly_groups:
if len(group) >=19: # Check if the month has at least 19 data points
var_data = compute_var(group)
history.append({'date': str(period), 'var': var_data})