ADD more indictaors, BB,RSI and Z-score
This commit is contained in:
@@ -375,7 +375,39 @@ class DataEngine:
|
||||
prev_close = float(df.iloc[-2]['close'])
|
||||
change_pct = ((last_close - prev_close) / prev_close) * 100
|
||||
count = len(df)
|
||||
# --- NEW INDICATORS START ---
|
||||
|
||||
# Bollinger Bands %B (20-day)
|
||||
bb_pct = "N/A"
|
||||
if count >= 20:
|
||||
from ta.volatility import BollingerBands
|
||||
indicator_bb = BollingerBands(close=df['close'], window=20, window_dev=2)
|
||||
m_avg = indicator_bb.bollinger_mavg().iloc[-1]
|
||||
h_band = indicator_bb.bollinger_hband().iloc[-1]
|
||||
l_band = indicator_bb.bollinger_lband().iloc[-1]
|
||||
# Calculate %B: where is price relative to bands?
|
||||
if (h_band - l_band) != 0:
|
||||
bb_pct = round((last_close - l_band) / (h_band - l_band), 2)
|
||||
|
||||
# RSI (14-day)
|
||||
rsi_val = "N/A"
|
||||
if count >= 14:
|
||||
from ta.momentum import RSIIndicator
|
||||
rsi_val = round(RSIIndicator(close=df['close'], window=14).rsi().iloc[-1], 1)
|
||||
|
||||
# Z-Score Helper Function
|
||||
def get_z_score(window):
|
||||
if count >= window:
|
||||
rolling_mean = df['close'].rolling(window=window).mean()
|
||||
rolling_std = df['close'].rolling(window=window).std()
|
||||
z = (last_close - rolling_mean.iloc[-1]) / rolling_std.iloc[-1]
|
||||
return round(z, 2)
|
||||
return "N/A"
|
||||
|
||||
z60 = get_z_score(60)
|
||||
z120 = get_z_score(120)
|
||||
|
||||
# Existing EMA Logic (Distance % from EMA)
|
||||
def get_ema_offset(window):
|
||||
if count >= window:
|
||||
from ta.trend import EMAIndicator
|
||||
@@ -396,18 +428,22 @@ class DataEngine:
|
||||
|
||||
return {
|
||||
"symbol": self.symbol,
|
||||
"last_date": formatted_date, # <--- New field added
|
||||
"last_date": formatted_date,
|
||||
"last_close": round(last_close, 2),
|
||||
"change_pct": round(change_pct, 2),
|
||||
"low_52": round(float(df.tail(252)['close'].min()), 2),
|
||||
"high_52": round(float(df.tail(252)['close'].max()), 2),
|
||||
"bb_pct": bb_pct,
|
||||
"rsi": rsi_val,
|
||||
"z60": z60,
|
||||
"z120": z120,
|
||||
"last_ema20": get_ema_offset(20),
|
||||
"last_ema50": get_ema_offset(50),
|
||||
"last_ema100": get_ema_offset(100),
|
||||
"last_ema200": get_ema_offset(200),
|
||||
"kd_values": f"{int(k_val)}/{int(d_val)}" if k_val != "N/A" else "N/A"
|
||||
}
|
||||
|
||||
|
||||
class StrategyEngine:
|
||||
"""
|
||||
Handles financial strategy simulations and backtesting.
|
||||
|
||||
Reference in New Issue
Block a user