Spaces:
Sleeping
Sleeping
Add Jared read-only token OHLCV ingestion and manual contract lookup
Browse files
app.py
CHANGED
|
@@ -52,17 +52,18 @@ def update_horizon_label(timeframe):
|
|
| 52 |
return gr.update(label=f"Horizon ({units.get(timeframe, 'days')})")
|
| 53 |
|
| 54 |
@spaces.GPU
|
| 55 |
-
def run_dashboard(data_src, ticker, file_upload, timeframe, start_date, end_date, horizon, indicators,
|
| 56 |
include_sentiment, news_api_key, alpha_api_key, account_size, risk_percent, model,
|
| 57 |
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
|
| 58 |
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
|
| 59 |
feat_selector, feat_threshold):
|
| 60 |
try:
|
| 61 |
-
|
|
|
|
| 62 |
start_date = pd.to_datetime(start_date).strftime("%Y-%m-%d")
|
| 63 |
end_date = pd.to_datetime(end_date).strftime("%Y-%m-%d")
|
| 64 |
|
| 65 |
-
df = load_data(data_src=data_src, ticker=
|
| 66 |
interval=timeframe, file_upload=file_upload, alpha_api_key=alpha_api_key)
|
| 67 |
if df.empty:
|
| 68 |
logging.error("Failed to load data")
|
|
@@ -103,9 +104,10 @@ def run_dashboard(data_src, ticker, file_upload, timeframe, start_date, end_date
|
|
| 103 |
logging.error("Failed to generate signals")
|
| 104 |
return [None] * 12 + ["Failed to generate signals", None, None, None, None, "Failed to generate signals"]
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
|
| 111 |
future_plot = plot_future_forecast(df, result, indicators)
|
|
@@ -121,17 +123,18 @@ def run_dashboard(data_src, ticker, file_upload, timeframe, start_date, end_date
|
|
| 121 |
loss_plot = plot_loss_curve(result)
|
| 122 |
architecture_plot = plot_model_architecture(result)
|
| 123 |
|
| 124 |
-
|
|
|
|
| 125 |
signals_df.to_csv(signals_csv)
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
-
predictions_csv = f"predictions_{
|
| 130 |
pd.DataFrame({
|
| 131 |
"Actual": result["actual"],
|
| 132 |
"Forecast": result["forecast"]
|
| 133 |
}).to_csv(predictions_csv)
|
| 134 |
-
chart_png = f"chart_{
|
| 135 |
pio.write_image(chart_plot, chart_png, format='png')
|
| 136 |
|
| 137 |
with open(log_path, 'r') as log_file:
|
|
@@ -141,7 +144,8 @@ def run_dashboard(data_src, ticker, file_upload, timeframe, start_date, end_date
|
|
| 141 |
return [
|
| 142 |
chart_plot, sentiment_text, signals_table, backtest_plot, future_plot, future_table,
|
| 143 |
r2_plot, error_plot, precision_recall_plot, risk_plot, loss_plot, architecture_plot,
|
| 144 |
-
"Dashboard generated successfully
|
|
|
|
| 145 |
]
|
| 146 |
except Exception as e:
|
| 147 |
logging.error(f"Dashboard error: {str(e)}")
|
|
@@ -153,8 +157,9 @@ def main_interface():
|
|
| 153 |
gr.Markdown("# Market Prediction Pro")
|
| 154 |
with gr.Row():
|
| 155 |
with gr.Column(scale=1):
|
| 156 |
-
data_src = gr.Dropdown(["yahoo", "csv"], label="Data Source", value="yahoo")
|
| 157 |
-
ticker = gr.Dropdown(DEFAULT_TICKERS, label="Ticker", value="AAPL")
|
|
|
|
| 158 |
file_upload = gr.File(label="Upload CSV", visible=False)
|
| 159 |
timeframe = gr.Dropdown(AVAILABLE_TIMEFRAMES, label="Timeframe", value="1d")
|
| 160 |
start_date = gr.Textbox("2020-01-01", label="Start Date (YYYY-MM-DD)")
|
|
@@ -215,13 +220,13 @@ def main_interface():
|
|
| 215 |
export_predictions = gr.File(label="Export Predictions (CSV)")
|
| 216 |
log_output = gr.Textbox(label="Debug Logs", lines=10)
|
| 217 |
|
| 218 |
-
data_src.change(fn=lambda src: gr.update(visible=(src == "csv")), inputs=[data_src], outputs=[file_upload])
|
| 219 |
include_sentiment.change(fn=lambda sent: gr.update(visible=sent), inputs=[include_sentiment], outputs=[news_api_key])
|
| 220 |
timeframe.change(fn=update_horizon_label, inputs=[timeframe], outputs=[horizon])
|
| 221 |
|
| 222 |
run_btn.click(
|
| 223 |
fn=run_dashboard,
|
| 224 |
-
inputs=[data_src, ticker, file_upload, timeframe, start_date, end_date, horizon, indicators,
|
| 225 |
include_sentiment, news_api_key, alpha_api_key, account_size, risk_percent, model,
|
| 226 |
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
|
| 227 |
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
|
|
@@ -237,5 +242,3 @@ def main_interface():
|
|
| 237 |
|
| 238 |
if __name__ == "__main__":
|
| 239 |
main_interface().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 52 |
return gr.update(label=f"Horizon ({units.get(timeframe, 'days')})")
|
| 53 |
|
| 54 |
@spaces.GPU
|
| 55 |
+
def run_dashboard(data_src, ticker, manual_token, file_upload, timeframe, start_date, end_date, horizon, indicators,
|
| 56 |
include_sentiment, news_api_key, alpha_api_key, account_size, risk_percent, model,
|
| 57 |
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
|
| 58 |
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
|
| 59 |
feat_selector, feat_threshold):
|
| 60 |
try:
|
| 61 |
+
requested_ticker = (manual_token or (ticker if ticker in {"JARED-LATEST", "CON"} else "JARED-LATEST")).strip() if data_src == "jared" else ticker
|
| 62 |
+
logging.info(f"Running dashboard for {requested_ticker}, timeframe: {timeframe}, model: {model}")
|
| 63 |
start_date = pd.to_datetime(start_date).strftime("%Y-%m-%d")
|
| 64 |
end_date = pd.to_datetime(end_date).strftime("%Y-%m-%d")
|
| 65 |
|
| 66 |
+
df = load_data(data_src=data_src, ticker=requested_ticker, start=start_date, end=end_date,
|
| 67 |
interval=timeframe, file_upload=file_upload, alpha_api_key=alpha_api_key)
|
| 68 |
if df.empty:
|
| 69 |
logging.error("Failed to load data")
|
|
|
|
| 104 |
logging.error("Failed to generate signals")
|
| 105 |
return [None] * 12 + ["Failed to generate signals", None, None, None, None, "Failed to generate signals"]
|
| 106 |
|
| 107 |
+
display_ticker = df.attrs.get("token_symbol", requested_ticker)
|
| 108 |
+
chart_plot = plot_indicators(df, display_ticker)
|
| 109 |
+
signals_plot = plot_signals(signals_df, display_ticker)
|
| 110 |
+
backtest_plot = plot_backtest(equity_df, trades_df, display_ticker)
|
| 111 |
|
| 112 |
|
| 113 |
future_plot = plot_future_forecast(df, result, indicators)
|
|
|
|
| 123 |
loss_plot = plot_loss_curve(result)
|
| 124 |
architecture_plot = plot_model_architecture(result)
|
| 125 |
|
| 126 |
+
safe_name = "".join(ch if ch.isalnum() or ch in "-_" else "_" for ch in str(display_ticker))
|
| 127 |
+
signals_csv = f"signals_{safe_name}.csv"
|
| 128 |
signals_df.to_csv(signals_csv)
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
+
predictions_csv = f"predictions_{safe_name}.csv"
|
| 133 |
pd.DataFrame({
|
| 134 |
"Actual": result["actual"],
|
| 135 |
"Forecast": result["forecast"]
|
| 136 |
}).to_csv(predictions_csv)
|
| 137 |
+
chart_png = f"chart_{safe_name}.png"
|
| 138 |
pio.write_image(chart_plot, chart_png, format='png')
|
| 139 |
|
| 140 |
with open(log_path, 'r') as log_file:
|
|
|
|
| 144 |
return [
|
| 145 |
chart_plot, sentiment_text, signals_table, backtest_plot, future_plot, future_table,
|
| 146 |
r2_plot, error_plot, precision_recall_plot, risk_plot, loss_plot, architecture_plot,
|
| 147 |
+
f"Dashboard generated successfully | source={df.attrs.get('source', data_src)} | token={df.attrs.get('token_address', display_ticker)}",
|
| 148 |
+
chart_png, signals_csv, predictions_csv, signals_plot, log_output
|
| 149 |
]
|
| 150 |
except Exception as e:
|
| 151 |
logging.error(f"Dashboard error: {str(e)}")
|
|
|
|
| 157 |
gr.Markdown("# Market Prediction Pro")
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
+
data_src = gr.Dropdown(["yahoo", "jared", "csv"], label="Data Source", value="yahoo")
|
| 161 |
+
ticker = gr.Dropdown(DEFAULT_TICKERS, label="Ticker", value="AAPL", allow_custom_value=True)
|
| 162 |
+
manual_token = gr.Textbox(label="Manual token / contract address (Jared mode)", placeholder="CON or 0x…", visible=False)
|
| 163 |
file_upload = gr.File(label="Upload CSV", visible=False)
|
| 164 |
timeframe = gr.Dropdown(AVAILABLE_TIMEFRAMES, label="Timeframe", value="1d")
|
| 165 |
start_date = gr.Textbox("2020-01-01", label="Start Date (YYYY-MM-DD)")
|
|
|
|
| 220 |
export_predictions = gr.File(label="Export Predictions (CSV)")
|
| 221 |
log_output = gr.Textbox(label="Debug Logs", lines=10)
|
| 222 |
|
| 223 |
+
data_src.change(fn=lambda src: (gr.update(visible=(src == "csv")), gr.update(visible=(src == "jared"))), inputs=[data_src], outputs=[file_upload, manual_token])
|
| 224 |
include_sentiment.change(fn=lambda sent: gr.update(visible=sent), inputs=[include_sentiment], outputs=[news_api_key])
|
| 225 |
timeframe.change(fn=update_horizon_label, inputs=[timeframe], outputs=[horizon])
|
| 226 |
|
| 227 |
run_btn.click(
|
| 228 |
fn=run_dashboard,
|
| 229 |
+
inputs=[data_src, ticker, manual_token, file_upload, timeframe, start_date, end_date, horizon, indicators,
|
| 230 |
include_sentiment, news_api_key, alpha_api_key, account_size, risk_percent, model,
|
| 231 |
hidden_units, n_layers, epochs, learning_rate, beta1, beta2, weight_decay, dropout,
|
| 232 |
window_size, test_split, rsi_mid, macd_sens, adx_thr, sent_thr, vote_buy, vote_sell,
|
|
|
|
| 242 |
|
| 243 |
if __name__ == "__main__":
|
| 244 |
main_interface().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
|
|