Spaces:
Sleeping
Sleeping
File size: 13,302 Bytes
c95ea9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | import gradio as gr
import pandas as pd
import numpy as np
import os
from datetime import datetime, timedelta
import logging
from core.data import load_data, add_technical_indicators, add_sentiment
from core.model_runner import get_model
from core.plot import plot_forecast, plot_metrics_r2, plot_metrics_errors, plot_metrics_precision_recall, plot_metrics_risk, plot_loss_curve, plot_model_architecture, plot_future_forecast, plot_indicators, plot_signals, plot_backtest
import plotly.io as pio
from core.signals import generate_signals
from config import AVAILABLE_MODELS, DEFAULT_TICKERS, AVAILABLE_TIMEFRAMES, AVAILABLE_INDICATORS
from newsapi import NewsApiClient
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
log_path = "/tmp/app_log.txt"
os.makedirs("/tmp", exist_ok=True)
logging.basicConfig(
level=logging.DEBUG,
handlers=[
logging.FileHandler(log_path),
logging.StreamHandler()
],
format=\'%(asctime)s - %(levelname)s - %(message)s\'
)
analyzer = SentimentIntensityAnalyzer()
def sentiment_analysis(ticker, start_date, end_date, api_key):
try:
if not api_key:
return "No API key provided", None
newsapi = NewsApiClient(api_key=api_key)
start = pd.to_datetime(start_date)
end = pd.to_datetime(end_date)
articles = newsapi.get_everything(
q=ticker, from_param=start.strftime("%Y-%m-%d"), to=end.strftime("%Y-%m-%m"),
language=\'en\', sort_by=\'relevancy\'
)
sentiments = [analyzer.polarity_scores(article["title"]["compound"]) for article in articles["articles"]]
avg_sentiment = np.mean(sentiments) if sentiments else 0.0
sentiment_text = f"Average sentiment for {ticker}: {avg_sentiment:.2f}"
return sentiment_text, avg_sentiment
except Exception as e:
logging.error(f"Sentiment analysis failed: {str(e)}")
return f"Sentiment analysis failed: {str(e)}", None
def update_horizon_label(timeframe):
units = {\'1m\': \'minutes\', \'5m\': \'minutes\', \'15m\': \'minutes\', \'30m\': \'minutes\',
\'1h\': \'hours\', \'4h\': \'hours\', \'1d\': \'days\', \'1wk\': \'weeks\'}
return gr.update(label=f"Horizon ({units.get(timeframe, \'days\')})")
def run_dashboard(data_src, ticker, file_upload, timeframe, start_date, end_date, horizon, indicators,
include_sentiment, news_api_key, alpha_api_key, finnhub_api_key, twelvedata_api_key, account_size, risk_percent, model,
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
feat_selector, feat_threshold):
try:
logging.info(f"Running dashboard for {ticker}, timeframe: {timeframe}, model: {model}")
start_date = pd.to_datetime(start_date).strftime("%Y-%m-%d")
end_date = pd.to_datetime(end_date).strftime("%Y-%m-%d")
df = load_data(data_src=data_src, ticker=ticker, start=start_date, end=end_date,
interval=timeframe, file_upload=file_upload, alpha_api_key=alpha_api_key,
finnhub_api_key=finnhub_api_key, twelvedata_api_key=twelvedata_api_key)
if df.empty:
logging.error("Failed to load data")
return [None] * 12 + ["Failed to load data", None, None, None, None, "Failed to load data"]
df, valid_indicators = add_technical_indicators(df, indicators)
if include_sentiment and news_api_key:
df = add_sentiment(df, ticker, news_api_key, start_date, end_date)
sentiment_text, sentiment_score = sentiment_analysis(ticker, start_date, end_date, news_api_key)
features = valid_indicators # Use valid_indicators for feature
target = \'value\'
result = get_model(
df=df,
features=features,
target=target,
model_name=model,
horizon=horizon,
hidden_units=hidden_units,
n_layers=n_layers,
epochs=epochs,
learning_rate=learning_rate,
beta1=beta1,
beta2=beta2,
weight_decay=weight_decay,
dropout=dropout,
window_size=window_size,
test_split=test_split,
selector_method=feat_selector,
importance_threshold=feat_threshold
)
if isinstance(result, dict) and result.get("error"):
logging.error(f"Model training failed: {result[\'error\']})")
return [None] * 12 + [f"Model training failed: {result[\'error\']}", None, None, None, None, f"Model training failed: {result[\'error\']}"]
signals_df, trades_df, equity_df = generate_signals(df, result)
if signals_df.empty:
logging.error("Failed to generate signals")
return [None] * 12 + ["Failed to generate signals", None, None, None, None, "Failed to generate signals"]
chart_plot = plot_indicators(df, ticker)
signals_plot = plot_signals(signals_df, ticker)
backtest_plot = plot_backtest(equity_df, trades_df, ticker)
future_plot = plot_future_forecast(df, result, indicators)
future_table = pd.DataFrame({
"Date": [df.index[-1] + timedelta(days=i+1) for i in range(horizon)],
"Prediction": result["latest_prediction"]
})
signals_table = signals_df.reset_index()[["Date", "Price", "Signal", "Position_Size", "Stop_Loss", "Take_Profit", "Equity"]]
r2_plot = plot_metrics_r2(result)
error_plot = plot_metrics_errors(result)
precision_recall_plot = plot_metrics_precision_recall(result)
risk_plot = plot_metrics_risk(result)
loss_plot = plot_loss_curve(result)
architecture_plot = plot_model_architecture(result)
signals_csv = f"signals_{ticker}.csv"
signals_df.to_csv(signals_csv)
predictions_csv = f"predictions_{ticker}.csv"
pd.DataFrame({
"Actual": result["actual"],
"Forecast": result["forecast"]
}).to_csv(predictions_csv)
chart_png = f"chart_{ticker}.png"
pio.write_image(chart_plot, chart_png, format=\'png\')
with open(log_path, \'r\') as log_file:
log_output = log_file.read()
logging.info("Dashboard run completed successfully")
return [
chart_plot, sentiment_text, signals_table, backtest_plot, future_plot, future_table,
r2_output, error_output, precision_recall_plot, risk_output, loss_output, architecture_output,
"Dashboard generated successfully", chart_png, signals_csv, predictions_csv, signals_plot, log_output
]
except Exception as e:
logging.error(f"Dashboard error: {str(e)}")
return [None] * 12 + [f"Error: {str(e)}", None, None, None, None, f"Error: {str(e)}"]
def main_interface():
try:
with gr.Blocks(title="Market Prediction Pro", theme=gr.themes.Default()) as app:
gr.Markdown("# Market Prediction Pro")
with gr.Row():
with gr.Column(scale=1):
data_src = gr.Dropdown(["yahoo", "csv", "alpha_vantage", "finnhub", "twelvedata"], label="Data Source", value="yahoo")
ticker = gr.Dropdown(DEFAULT_TICKERS, label="Ticker", value="AAPL")
file_upload = gr.File(label="Upload CSV", visible=False)
timeframe = gr.Dropdown(AVAILABLE_TIMEFRAMES, label="Timeframe", value="1d")
start_date = gr.Textbox("2020-01-01", label="Start Date (YYYY-MM-DD)")
end_date = gr.Textbox("2023-01-01", label="End Date (YYYY-MM-DD)")
horizon = gr.Slider(1, 30, step=1, label="Horizon (days)", value=1)
indicators = gr.CheckboxGroup(AVAILABLE_INDICATORS, label="Technical Indicators", value=["rsi", "macd", "bbands"])
include_sentiment = gr.Checkbox(label="Include Sentiment Analysis", value=False)
news_api_key = gr.Textbox("a1018b32215d4c29bdfa1beae97e1f5c", label="News API Key (Optional)", type="password")
alpha_api_key = gr.Textbox("IUCYQSZIPF3QIB3Q", label="Alpha Vantage API Key (Optional)", type="password")
finnhub_api_key = gr.Textbox(label="Finnhub API Key (Optional)", type="password")
twelvedata_api_key = gr.Textbox(label="Twelve Data API Key (Optional)", type="password")
gr.Markdown("### Model Parameters")
model = gr.Dropdown(list(AVAILABLE_MODELS.keys()), label="Model", value="LSTM")
hidden_units = gr.Slider(10, 200, step=10, label="Hidden Units", value=50)
n_layers = gr.Slider(1, 5, step=1, label="Number of Layers", value=2)
epochs = gr.Slider(1, 100, step=1, label="Epochs", value=10)
learning_rate = gr.Slider(0.0001, 0.1, step=0.0001, label="Learning Rate", value=0.001)
beta1 = gr.Slider(0.1, 0.999, step=0.001, label="Adam Beta1", value=0.9)
beta2 = gr.Slider(0.1, 0.999, step=0.001, label="Adam Beta2", value=0.999)
weight_decay = gr.Slider(0.0, 0.1, step=0.001, label="Weight Decay", value=0.0001)
dropout = gr.Slider(0.0, 0.5, step=0.05, label="Dropout", value=0.2)
window_size = gr.Slider(5, 50, step=1, label="Window Size", value=20)
test_split = gr.Slider(0.1, 0.5, step=0.05, label="Test Split Ratio", value=0.2)
gr.Markdown("### Signal Generation Parameters")
rsi_mid = gr.Slider(30, 70, step=1, label="RSI Midpoint", value=50)
macd_sens = gr.Slider(5, 30, step=1, label="MACD Sensitivity", value=12)
adx_thr = gr.Slider(10, 50, step=1, label="ADX Threshold", value=25)
sent_thr = gr.Slider(-1.0, 1.0, step=0.01, label="Sentiment Threshold", value=0.05)
vote_buy = gr.Slider(1, 5, step=1, label="Votes to Buy", value=3)
vote_sell = gr.Slider(1, 5, step=1, label="Votes to Sell", value=3)
gr.Markdown("### Feature Selection Parameters")
feat_selector = gr.Dropdown(["none", "rfe", "sfm"], label="Feature Selector", value="none")
feat_threshold = gr.Slider(0.0, 1.0, step=0.01, label="Feature Importance Threshold", value=0.01)
run_button = gr.Button("Run Dashboard")
with gr.Column(scale=2):
output_text = gr.Textbox(label="Status", interactive=False)
chart_output = gr.Plot(label="Price Chart with Indicators")
sentiment_output = gr.Textbox(label="Sentiment Analysis", interactive=False)
signals_output = gr.DataFrame(label="Generated Signals")
backtest_output = gr.Plot(label="Backtesting Results")
future_forecast_output = gr.Plot(label="Future Forecast")
future_forecast_table = gr.DataFrame(label="Future Forecast Data")
r2_output = gr.Plot(label="R2 Score")
error_output = gr.Plot(label="Prediction Errors")
precision_recall_output = gr.Plot(label="Precision-Recall Curve")
risk_output = gr.Plot(label="Risk Metrics")
loss_output = gr.Plot(label="Loss Curve")
architecture_output = gr.Plot(label="Model Architecture")
log_output = gr.Textbox(label="Application Log", interactive=False, lines=10)
data_src.change(lambda x: gr.update(visible=x=="csv"), inputs=data_src, outputs=file_upload)
timeframe.change(update_horizon_label, inputs=timeframe, outputs=horizon)
run_button.click(
run_dashboard,
inputs=[
data_src, ticker, file_upload, timeframe, start_date, end_date, horizon, indicators,
include_sentiment, news_api_key, alpha_api_key, finnhub_api_key, twelvedata_api_key, gr.Number(value=10000, visible=False), gr.Number(value=0.01, visible=False), model,
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
feat_selector, feat_threshold
],
outputs=[
chart_output, sentiment_output, signals_output, backtest_output, future_forecast_output, future_forecast_table,
r2_output, error_output, precision_recall_plot, risk_output, loss_output, architecture_output,
output_text, gr.File(label="Chart PNG"), gr.File(label="Signals CSV"), gr.File(label="Predictions CSV"), gr.Plot(label="Signals Plot"), log_output
]
)
except Exception as e:
logging.error(f"Main interface creation failed: {str(e)}")
return gr.Blocks().queue().launch()
if __name__ == "__main__":
main_interface().queue().launch()
|