Spaces:
Running
on
Zero
Running
on
Zero
fix strength error and pandas to numpy error
Browse files
app.py
CHANGED
@@ -1133,12 +1133,14 @@ def detect_market_regime(returns: pd.Series, n_regimes: int = 3) -> Dict:
|
|
1133 |
try:
|
1134 |
if HMM_AVAILABLE:
|
1135 |
# Use HMM for regime detection
|
|
|
|
|
1136 |
model = hmm.GaussianHMM(n_components=n_regimes, random_state=42, covariance_type="full")
|
1137 |
-
model.fit(
|
1138 |
|
1139 |
# Get regime probabilities for the last observation
|
1140 |
-
regime_probs = model.predict_proba(
|
1141 |
-
current_regime = model.predict(
|
1142 |
|
1143 |
# Calculate regime characteristics
|
1144 |
regime_means = model.means_.flatten()
|
@@ -1624,6 +1626,10 @@ def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict
|
|
1624 |
bb_upper = df['BB_Upper'].iloc[-1]
|
1625 |
bb_lower = df['BB_Lower'].iloc[-1]
|
1626 |
|
|
|
|
|
|
|
|
|
1627 |
if current_price < bb_lower:
|
1628 |
bb_signal = "Buy"
|
1629 |
bb_confidence = 0.7
|
@@ -1636,8 +1642,9 @@ def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict
|
|
1636 |
|
1637 |
signals["Bollinger"] = {
|
1638 |
"signal": bb_signal,
|
|
|
1639 |
"confidence": bb_confidence,
|
1640 |
-
"position":
|
1641 |
}
|
1642 |
|
1643 |
# SMA signal
|
@@ -1645,6 +1652,10 @@ def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict
|
|
1645 |
sma_20 = df['SMA_20'].iloc[-1]
|
1646 |
sma_50 = df['SMA_50'].iloc[-1]
|
1647 |
|
|
|
|
|
|
|
|
|
1648 |
if sma_20 > sma_50:
|
1649 |
sma_signal = "Buy"
|
1650 |
sma_confidence = 0.6
|
@@ -1654,8 +1665,9 @@ def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict
|
|
1654 |
|
1655 |
signals["SMA"] = {
|
1656 |
"signal": sma_signal,
|
|
|
1657 |
"confidence": sma_confidence,
|
1658 |
-
"ratio":
|
1659 |
}
|
1660 |
|
1661 |
# Calculate weighted overall signal
|
@@ -1663,10 +1675,14 @@ def advanced_trading_signals(df: pd.DataFrame, regime_info: Dict = None) -> Dict
|
|
1663 |
sell_signals = []
|
1664 |
|
1665 |
for signal_name, signal_data in signals.items():
|
|
|
|
|
|
|
|
|
1666 |
if signal_data["signal"] == "Buy":
|
1667 |
-
buy_signals.append(
|
1668 |
elif signal_data["signal"] == "Sell":
|
1669 |
-
sell_signals.append(
|
1670 |
|
1671 |
weighted_buy = sum(buy_signals) if buy_signals else 0
|
1672 |
weighted_sell = sum(sell_signals) if sell_signals else 0
|
|
|
1133 |
try:
|
1134 |
if HMM_AVAILABLE:
|
1135 |
# Use HMM for regime detection
|
1136 |
+
# Convert pandas Series to numpy array for reshape
|
1137 |
+
returns_array = returns.dropna().values
|
1138 |
model = hmm.GaussianHMM(n_components=n_regimes, random_state=42, covariance_type="full")
|
1139 |
+
model.fit(returns_array.reshape(-1, 1))
|
1140 |
|
1141 |
# Get regime probabilities for the last observation
|
1142 |
+
regime_probs = model.predict_proba(returns_array.reshape(-1, 1))
|
1143 |
+
current_regime = model.predict(returns_array.reshape(-1, 1))[-1]
|
1144 |
|
1145 |
# Calculate regime characteristics
|
1146 |
regime_means = model.means_.flatten()
|
|
|
1626 |
bb_upper = df['BB_Upper'].iloc[-1]
|
1627 |
bb_lower = df['BB_Lower'].iloc[-1]
|
1628 |
|
1629 |
+
# Calculate position within Bollinger Bands (0-1 scale)
|
1630 |
+
bb_position = (current_price - bb_lower) / (bb_upper - bb_lower) if bb_upper != bb_lower else 0.5
|
1631 |
+
bb_strength = abs(bb_position - 0.5) * 2 # 0-1 scale, strongest at edges
|
1632 |
+
|
1633 |
if current_price < bb_lower:
|
1634 |
bb_signal = "Buy"
|
1635 |
bb_confidence = 0.7
|
|
|
1642 |
|
1643 |
signals["Bollinger"] = {
|
1644 |
"signal": bb_signal,
|
1645 |
+
"strength": bb_strength,
|
1646 |
"confidence": bb_confidence,
|
1647 |
+
"position": bb_position
|
1648 |
}
|
1649 |
|
1650 |
# SMA signal
|
|
|
1652 |
sma_20 = df['SMA_20'].iloc[-1]
|
1653 |
sma_50 = df['SMA_50'].iloc[-1]
|
1654 |
|
1655 |
+
# Calculate SMA strength based on ratio
|
1656 |
+
sma_ratio = sma_20 / sma_50 if sma_50 != 0 else 1.0
|
1657 |
+
sma_strength = abs(sma_ratio - 1.0) # 0-1 scale, strongest when ratio differs most from 1
|
1658 |
+
|
1659 |
if sma_20 > sma_50:
|
1660 |
sma_signal = "Buy"
|
1661 |
sma_confidence = 0.6
|
|
|
1665 |
|
1666 |
signals["SMA"] = {
|
1667 |
"signal": sma_signal,
|
1668 |
+
"strength": sma_strength,
|
1669 |
"confidence": sma_confidence,
|
1670 |
+
"ratio": sma_ratio
|
1671 |
}
|
1672 |
|
1673 |
# Calculate weighted overall signal
|
|
|
1675 |
sell_signals = []
|
1676 |
|
1677 |
for signal_name, signal_data in signals.items():
|
1678 |
+
# Get strength with default value if not present
|
1679 |
+
strength = signal_data.get("strength", 0.5) # Default strength of 0.5
|
1680 |
+
confidence = signal_data.get("confidence", 0.5) # Default confidence of 0.5
|
1681 |
+
|
1682 |
if signal_data["signal"] == "Buy":
|
1683 |
+
buy_signals.append(strength * confidence)
|
1684 |
elif signal_data["signal"] == "Sell":
|
1685 |
+
sell_signals.append(strength * confidence)
|
1686 |
|
1687 |
weighted_buy = sum(buy_signals) if buy_signals else 0
|
1688 |
weighted_sell = sum(sell_signals) if sell_signals else 0
|