bugfixing

This commit is contained in:
MuslemRahimi 2025-02-20 15:04:01 +01:00
parent 19c3ee4d06
commit 1049780b1f
2 changed files with 35 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import pandas as pd
from tqdm import tqdm
import concurrent.futures
import orjson
import os
def convert_symbols(symbol_list):
converted_symbols = []
@ -50,10 +51,13 @@ async def download_data(ticker: str, start_date: str, end_date: str):
# Apply filtering logic if enough data exists
if len(df) > 252 * 2: # At least 2 years of history is necessary
q_high = df["y"].quantile(0.99)
q_low = df["y"].quantile(0.01)
q_low = df["y"].quantile(0.1)
df = df[(df["y"] > q_low) & (df["y"] < q_high)]
return df
# Calculate Simple Moving Average (SMA)
#df["y"] = df["y"].rolling(window=50).mean() # 50-day SMA
return df.dropna() # Drop initial NaN values due to rolling window
except Exception as e:
print(f"Error processing {ticker}: {e}")
return None
@ -62,10 +66,19 @@ async def process_symbol(ticker, start_date, end_date):
try:
df = await download_data(ticker, start_date, end_date)
data = PricePredictor().run(df)
file_path = f"json/price-analysis/{ticker}.json"
if data and data['lowPriceTarget'] > 0:
print(data)
await save_json(ticker, data)
else:
await asyncio.to_thread(os.remove, file_path)
except Exception as e:
print(e)
try:
await asyncio.to_thread(os.remove, file_path)
except FileNotFoundError:
pass # The file might not exist, so we ignore the error
async def run():
@ -79,12 +92,16 @@ async def run():
total_symbols = stock_symbols
print(f"Total tickers: {len(total_symbols)}")
start_date = datetime(2017, 1, 1).strftime("%Y-%m-%d")
start_date = datetime(2015, 1, 1).strftime("%Y-%m-%d")
end_date = datetime.today().strftime("%Y-%m-%d")
df_sp500 = await download_data('SPY', start_date, end_date)
df_sp500 = df_sp500.rename(columns={"y": "sp500"})
#print(df_sp500)
chunk_size = len(total_symbols) // 70 # Divide the list into N chunks
chunks = [total_symbols[i:i + chunk_size] for i in range(0, len(total_symbols), chunk_size)]
#chunks = [['NVDA','GME','TSLA','AAPL']]
#chunks = [['GME']]
for chunk in chunks:
tasks = []
for ticker in tqdm(chunk):

View File

@ -55,18 +55,24 @@ class PricePredictor:
interval_width=0.8,
daily_seasonality=True,
yearly_seasonality=True,
changepoint_prior_scale= 0.1,
seasonality_prior_scale=0.1,
)
#self.model.add_regressor('volume')
def run(self, df):
df = df.copy()
self.model.fit(df)
future = self.model.make_future_dataframe(periods=self.predict_ndays)
forecast = self.model.predict(future)
# Apply rolling average to smooth the forecast intervals
rolling_window = 200
forecast['smoothed_upper'] = forecast['yhat_upper'].round(2)#.rolling(window=rolling_window, min_periods=1).mean().round(2)
forecast['smoothed_lower'] = forecast['yhat_lower'].round(2)#.rolling(window=rolling_window, min_periods=1).mean().round(2)
forecast['smoothed_mean'] = forecast['yhat'].round(2)#.rolling(window=rolling_window, min_periods=1).mean().round(2)
forecast['smoothed_upper'] = forecast['yhat_upper'].rolling(window=rolling_window, min_periods=1).mean().round(2)
forecast['smoothed_lower'] = forecast['yhat_lower'].rolling(window=rolling_window, min_periods=1).mean().round(2)
forecast['smoothed_mean'] = forecast['yhat'].rolling(window=rolling_window, min_periods=1).mean().round(2)
# Actual and predicted values for evaluation (optional)
actual_values = df['y'].values