import pandas as pd import ta def backtest_strategy(df): if len(df) < 60: return 0 df = df.copy() close = df["Close"].squeeze() rsi = ta.momentum.RSIIndicator(close).rsi() ma20 = close.rolling(20).mean() ma50 = close.rolling(50).mean() wins = 0 trades = 0 for i in range(50, len(df) - 1): rsi_val = rsi.iloc[i] ma20_val = ma20.iloc[i] ma50_val = ma50.iloc[i] price_today = close.iloc[i] price_next = close.iloc[i + 1] signal = False # RSI oversold bounce if rsi_val < 35: signal = True # MA bullish crossover if ma20_val > ma50_val: signal = True if signal: trades += 1 if price_next > price_today: wins += 1 if trades == 0: return 0 winrate = (wins / trades) * 100 return round(winrate, 2)