Spaces:
Running
on
Zero
Running
on
Zero
attempt more advanced predictions using model ensemble
Browse files
app.py
CHANGED
@@ -981,6 +981,64 @@ def make_prediction(symbol: str, timeframe: str = "1d", prediction_days: int = 5
|
|
981 |
if macd_pred is not None:
|
982 |
signals["predicted_macd"] = macd_pred.tolist()
|
983 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
984 |
return signals, fig
|
985 |
|
986 |
except Exception as e:
|
@@ -1791,11 +1849,24 @@ def create_interface():
|
|
1791 |
gr.Markdown("### Structured Product Metrics")
|
1792 |
hourly_metrics = gr.JSON(label="Product Metrics")
|
1793 |
|
1794 |
-
gr.Markdown("###
|
1795 |
hourly_risk_metrics = gr.JSON(label="Risk Metrics")
|
1796 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1797 |
gr.Markdown("### Sector & Financial Analysis")
|
1798 |
hourly_sector_metrics = gr.JSON(label="Sector Metrics")
|
|
|
|
|
|
|
|
|
|
|
|
|
1799 |
|
1800 |
# 15-Minute Analysis Tab
|
1801 |
with gr.TabItem("15-Minute Analysis"):
|
@@ -1843,11 +1914,24 @@ def create_interface():
|
|
1843 |
gr.Markdown("### Structured Product Metrics")
|
1844 |
min15_metrics = gr.JSON(label="Product Metrics")
|
1845 |
|
1846 |
-
gr.Markdown("### Risk Analysis")
|
1847 |
min15_risk_metrics = gr.JSON(label="Risk Metrics")
|
1848 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1849 |
gr.Markdown("### Sector & Financial Analysis")
|
1850 |
min15_sector_metrics = gr.JSON(label="Sector Metrics")
|
|
|
|
|
|
|
|
|
|
|
|
|
1851 |
|
1852 |
def analyze_stock(symbol, timeframe, prediction_days, lookback_days, strategy,
|
1853 |
use_ensemble, use_regime_detection, use_stress_testing,
|
@@ -1977,7 +2061,7 @@ def create_interface():
|
|
1977 |
|
1978 |
# Hourly analysis button click
|
1979 |
def hourly_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
|
1980 |
-
rfr: float, mi: str, cw: float, tw: float, sw: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict]:
|
1981 |
"""
|
1982 |
Process hourly timeframe stock analysis with advanced features.
|
1983 |
|
@@ -1996,7 +2080,7 @@ def create_interface():
|
|
1996 |
sw (float): Statistical weight
|
1997 |
|
1998 |
Returns:
|
1999 |
-
Tuple containing analysis results
|
2000 |
"""
|
2001 |
return analyze_stock(s, "1h", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
|
2002 |
|
@@ -2005,12 +2089,13 @@ def create_interface():
|
|
2005 |
inputs=[hourly_symbol, hourly_prediction_days, hourly_lookback_days, hourly_strategy,
|
2006 |
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
|
2007 |
chronos_weight, technical_weight, statistical_weight],
|
2008 |
-
outputs=[hourly_signals, hourly_plot, hourly_metrics, hourly_risk_metrics, hourly_sector_metrics
|
|
|
2009 |
)
|
2010 |
|
2011 |
# 15-minute analysis button click
|
2012 |
def min15_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
|
2013 |
-
rfr: float, mi: str, cw: float, tw: float, sw: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict]:
|
2014 |
"""
|
2015 |
Process 15-minute timeframe stock analysis with advanced features.
|
2016 |
|
@@ -2029,7 +2114,7 @@ def create_interface():
|
|
2029 |
sw (float): Statistical weight
|
2030 |
|
2031 |
Returns:
|
2032 |
-
Tuple containing analysis results
|
2033 |
"""
|
2034 |
return analyze_stock(s, "15m", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
|
2035 |
|
@@ -2038,7 +2123,8 @@ def create_interface():
|
|
2038 |
inputs=[min15_symbol, min15_prediction_days, min15_lookback_days, min15_strategy,
|
2039 |
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
|
2040 |
chronos_weight, technical_weight, statistical_weight],
|
2041 |
-
outputs=[min15_signals, min15_plot, min15_metrics, min15_risk_metrics, min15_sector_metrics
|
|
|
2042 |
)
|
2043 |
|
2044 |
return demo
|
|
|
981 |
if macd_pred is not None:
|
982 |
signals["predicted_macd"] = macd_pred.tolist()
|
983 |
|
984 |
+
# Implement advanced features
|
985 |
+
# 1. Market Regime Detection
|
986 |
+
if use_regime_detection:
|
987 |
+
try:
|
988 |
+
returns = df['Returns'].dropna()
|
989 |
+
regime_info = detect_market_regime(returns)
|
990 |
+
signals["regime_info"] = regime_info
|
991 |
+
except Exception as e:
|
992 |
+
print(f"Regime detection error: {str(e)}")
|
993 |
+
signals["regime_info"] = {"error": str(e)}
|
994 |
+
|
995 |
+
# 2. Advanced Trading Signals with Regime Awareness
|
996 |
+
try:
|
997 |
+
regime_info = signals.get("regime_info", {})
|
998 |
+
advanced_signals = advanced_trading_signals(df, regime_info)
|
999 |
+
signals["advanced_signals"] = advanced_signals
|
1000 |
+
except Exception as e:
|
1001 |
+
print(f"Advanced trading signals error: {str(e)}")
|
1002 |
+
signals["advanced_signals"] = {"error": str(e)}
|
1003 |
+
|
1004 |
+
# 3. Stress Testing
|
1005 |
+
if use_stress_testing:
|
1006 |
+
try:
|
1007 |
+
stress_results = stress_test_scenarios(df, mean_pred)
|
1008 |
+
signals["stress_test_results"] = stress_results
|
1009 |
+
except Exception as e:
|
1010 |
+
print(f"Stress testing error: {str(e)}")
|
1011 |
+
signals["stress_test_results"] = {"error": str(e)}
|
1012 |
+
|
1013 |
+
# 4. Ensemble Methods
|
1014 |
+
if use_ensemble and ensemble_weights:
|
1015 |
+
try:
|
1016 |
+
ensemble_mean, ensemble_uncertainty = create_ensemble_prediction(
|
1017 |
+
df, prediction_days, ensemble_weights
|
1018 |
+
)
|
1019 |
+
if len(ensemble_mean) > 0:
|
1020 |
+
signals["ensemble_used"] = True
|
1021 |
+
signals["ensemble_prediction"] = ensemble_mean.tolist()
|
1022 |
+
signals["ensemble_uncertainty"] = ensemble_uncertainty.tolist()
|
1023 |
+
# Update the main prediction with ensemble if available
|
1024 |
+
if len(ensemble_mean) == len(mean_pred):
|
1025 |
+
mean_pred = ensemble_mean
|
1026 |
+
std_pred = ensemble_uncertainty
|
1027 |
+
else:
|
1028 |
+
signals["ensemble_used"] = False
|
1029 |
+
except Exception as e:
|
1030 |
+
print(f"Ensemble prediction error: {str(e)}")
|
1031 |
+
signals["ensemble_used"] = False
|
1032 |
+
signals["ensemble_error"] = str(e)
|
1033 |
+
|
1034 |
+
# 5. Enhanced Uncertainty Quantification
|
1035 |
+
try:
|
1036 |
+
if 'quantiles' in locals():
|
1037 |
+
skewed_uncertainty = calculate_skewed_uncertainty(quantiles)
|
1038 |
+
signals["skewed_uncertainty"] = skewed_uncertainty.tolist()
|
1039 |
+
except Exception as e:
|
1040 |
+
print(f"Skewed uncertainty calculation error: {str(e)}")
|
1041 |
+
|
1042 |
return signals, fig
|
1043 |
|
1044 |
except Exception as e:
|
|
|
1849 |
gr.Markdown("### Structured Product Metrics")
|
1850 |
hourly_metrics = gr.JSON(label="Product Metrics")
|
1851 |
|
1852 |
+
gr.Markdown("### Advanced Risk Analysis")
|
1853 |
hourly_risk_metrics = gr.JSON(label="Risk Metrics")
|
1854 |
|
1855 |
+
gr.Markdown("### Market Regime Analysis")
|
1856 |
+
hourly_regime_metrics = gr.JSON(label="Regime Metrics")
|
1857 |
+
|
1858 |
+
gr.Markdown("### Trading Signals")
|
1859 |
+
hourly_signals_advanced = gr.JSON(label="Advanced Trading Signals")
|
1860 |
+
|
1861 |
+
with gr.Column():
|
1862 |
gr.Markdown("### Sector & Financial Analysis")
|
1863 |
hourly_sector_metrics = gr.JSON(label="Sector Metrics")
|
1864 |
+
|
1865 |
+
gr.Markdown("### Stress Test Results")
|
1866 |
+
hourly_stress_results = gr.JSON(label="Stress Test Results")
|
1867 |
+
|
1868 |
+
gr.Markdown("### Ensemble Analysis")
|
1869 |
+
hourly_ensemble_metrics = gr.JSON(label="Ensemble Metrics")
|
1870 |
|
1871 |
# 15-Minute Analysis Tab
|
1872 |
with gr.TabItem("15-Minute Analysis"):
|
|
|
1914 |
gr.Markdown("### Structured Product Metrics")
|
1915 |
min15_metrics = gr.JSON(label="Product Metrics")
|
1916 |
|
1917 |
+
gr.Markdown("### Advanced Risk Analysis")
|
1918 |
min15_risk_metrics = gr.JSON(label="Risk Metrics")
|
1919 |
|
1920 |
+
gr.Markdown("### Market Regime Analysis")
|
1921 |
+
min15_regime_metrics = gr.JSON(label="Regime Metrics")
|
1922 |
+
|
1923 |
+
gr.Markdown("### Trading Signals")
|
1924 |
+
min15_signals_advanced = gr.JSON(label="Advanced Trading Signals")
|
1925 |
+
|
1926 |
+
with gr.Column():
|
1927 |
gr.Markdown("### Sector & Financial Analysis")
|
1928 |
min15_sector_metrics = gr.JSON(label="Sector Metrics")
|
1929 |
+
|
1930 |
+
gr.Markdown("### Stress Test Results")
|
1931 |
+
min15_stress_results = gr.JSON(label="Stress Test Results")
|
1932 |
+
|
1933 |
+
gr.Markdown("### Ensemble Analysis")
|
1934 |
+
min15_ensemble_metrics = gr.JSON(label="Ensemble Metrics")
|
1935 |
|
1936 |
def analyze_stock(symbol, timeframe, prediction_days, lookback_days, strategy,
|
1937 |
use_ensemble, use_regime_detection, use_stress_testing,
|
|
|
2061 |
|
2062 |
# Hourly analysis button click
|
2063 |
def hourly_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
|
2064 |
+
rfr: float, mi: str, cw: float, tw: float, sw: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict]:
|
2065 |
"""
|
2066 |
Process hourly timeframe stock analysis with advanced features.
|
2067 |
|
|
|
2080 |
sw (float): Statistical weight
|
2081 |
|
2082 |
Returns:
|
2083 |
+
Tuple containing all analysis results
|
2084 |
"""
|
2085 |
return analyze_stock(s, "1h", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
|
2086 |
|
|
|
2089 |
inputs=[hourly_symbol, hourly_prediction_days, hourly_lookback_days, hourly_strategy,
|
2090 |
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
|
2091 |
chronos_weight, technical_weight, statistical_weight],
|
2092 |
+
outputs=[hourly_signals, hourly_plot, hourly_metrics, hourly_risk_metrics, hourly_sector_metrics,
|
2093 |
+
hourly_regime_metrics, hourly_stress_results, hourly_ensemble_metrics]
|
2094 |
)
|
2095 |
|
2096 |
# 15-minute analysis button click
|
2097 |
def min15_analysis(s: str, pd: int, ld: int, st: str, ue: bool, urd: bool, ust: bool,
|
2098 |
+
rfr: float, mi: str, cw: float, tw: float, sw: float) -> Tuple[Dict, go.Figure, Dict, Dict, Dict, Dict, Dict, Dict]:
|
2099 |
"""
|
2100 |
Process 15-minute timeframe stock analysis with advanced features.
|
2101 |
|
|
|
2114 |
sw (float): Statistical weight
|
2115 |
|
2116 |
Returns:
|
2117 |
+
Tuple containing all analysis results
|
2118 |
"""
|
2119 |
return analyze_stock(s, "15m", pd, ld, st, ue, urd, ust, rfr, mi, cw, tw, sw)
|
2120 |
|
|
|
2123 |
inputs=[min15_symbol, min15_prediction_days, min15_lookback_days, min15_strategy,
|
2124 |
use_ensemble, use_regime_detection, use_stress_testing, risk_free_rate, market_index,
|
2125 |
chronos_weight, technical_weight, statistical_weight],
|
2126 |
+
outputs=[min15_signals, min15_plot, min15_metrics, min15_risk_metrics, min15_sector_metrics,
|
2127 |
+
min15_regime_metrics, min15_stress_results, min15_ensemble_metrics]
|
2128 |
)
|
2129 |
|
2130 |
return demo
|