Spaces:
Sleeping
Sleeping
Update models/technical_analysis.py
Browse files- models/technical_analysis.py +48 -26
models/technical_analysis.py
CHANGED
|
@@ -6,31 +6,53 @@ import ta
|
|
| 6 |
class EnhancedTechnicalAnalysis:
|
| 7 |
def __init__(self, data: pd.DataFrame):
|
| 8 |
self.data = data.copy()
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
def calculate_all_indicators(self) -> pd.DataFrame:
|
| 11 |
"""Calculate all technical indicators."""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
self.data['
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
class EnhancedTechnicalAnalysis:
|
| 7 |
def __init__(self, data: pd.DataFrame):
|
| 8 |
self.data = data.copy()
|
| 9 |
+
# Ensure the data is properly formatted
|
| 10 |
+
self.data.set_index('Date', inplace=True)
|
| 11 |
+
|
| 12 |
def calculate_all_indicators(self) -> pd.DataFrame:
|
| 13 |
"""Calculate all technical indicators."""
|
| 14 |
+
try:
|
| 15 |
+
# Ensure we're working with float values
|
| 16 |
+
self.data['Close'] = pd.to_numeric(self.data['Close'], errors='coerce')
|
| 17 |
+
self.data['High'] = pd.to_numeric(self.data['High'], errors='coerce')
|
| 18 |
+
self.data['Low'] = pd.to_numeric(self.data['Low'], errors='coerce')
|
| 19 |
+
self.data['Volume'] = pd.to_numeric(self.data['Volume'], errors='coerce')
|
| 20 |
+
|
| 21 |
+
# Trend Indicators
|
| 22 |
+
self.data['SMA_20'] = ta.trend.sma_indicator(close=self.data['Close'].values, window=20)
|
| 23 |
+
self.data['SMA_50'] = ta.trend.sma_indicator(close=self.data['Close'].values, window=50)
|
| 24 |
+
self.data['EMA_20'] = ta.trend.ema_indicator(close=self.data['Close'].values, window=20)
|
| 25 |
+
|
| 26 |
+
macd = ta.trend.MACD(close=self.data['Close'].values)
|
| 27 |
+
self.data['MACD'] = macd.macd()
|
| 28 |
+
self.data['MACD_Signal'] = macd.macd_signal()
|
| 29 |
+
|
| 30 |
+
# Momentum Indicators
|
| 31 |
+
self.data['RSI'] = ta.momentum.RSIIndicator(close=self.data['Close'].values).rsi()
|
| 32 |
+
self.data['Stoch'] = ta.momentum.StochasticOscillator(
|
| 33 |
+
high=self.data['High'].values,
|
| 34 |
+
low=self.data['Low'].values,
|
| 35 |
+
close=self.data['Close'].values
|
| 36 |
+
).stoch()
|
| 37 |
+
|
| 38 |
+
# Volatility Indicators
|
| 39 |
+
bb = ta.volatility.BollingerBands(close=self.data['Close'].values)
|
| 40 |
+
self.data['BB_Upper'] = bb.bollinger_hband()
|
| 41 |
+
self.data['BB_Lower'] = bb.bollinger_lband()
|
| 42 |
+
self.data['BB_Middle'] = bb.bollinger_mavg()
|
| 43 |
+
|
| 44 |
+
# Volume Indicators
|
| 45 |
+
self.data['OBV'] = ta.volume.OnBalanceVolumeIndicator(
|
| 46 |
+
close=self.data['Close'].values,
|
| 47 |
+
volume=self.data['Volume'].values
|
| 48 |
+
).on_balance_volume()
|
| 49 |
+
|
| 50 |
+
# Reset index to include Date column
|
| 51 |
+
self.data.reset_index(inplace=True)
|
| 52 |
+
|
| 53 |
+
return self.data
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Error calculating indicators: {str(e)}")
|
| 57 |
+
# Return original data if calculation fails
|
| 58 |
+
return self.data
|