Manus AI
Fix: Final runtime errors (backtest_output NameError and TA-Lib TSI attribute error).
a743675
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, sentiment_analysis
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'
)
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)
sentiment_text = "Sentiment analysis not included."
if include_sentiment and news_api_key:
df, sentiment_text = add_sentiment(df, ticker, news_api_key, start_date, end_date)
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_output = plot_metrics_r2(result)
error_output = plot_metrics_errors(result)
precision_recall_output = plot_metrics_precision_recall(result)
risk_output = plot_metrics_risk(result)
loss_output = plot_loss_curve(result)
architecture_output = 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_output, 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(AVAILABLE_MODELS, 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_plot = 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_plot, future_forecast_output, future_forecast_table,
r2_output, error_output, precision_recall_output, 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
]
)
return app
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()