From e48fcfafbd07f92489f200e7432213459c2d2899 Mon Sep 17 00:00:00 2001 From: "Bobby (aider)" Date: Thu, 13 Feb 2025 23:16:58 -0800 Subject: [PATCH] feat: Add trading logic for BB, RSI, and EMA indicators in strategy --- src/pages/backtesting/backtesting_page.py | 28 ++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/pages/backtesting/backtesting_page.py b/src/pages/backtesting/backtesting_page.py index 4cb931e..0f11fb1 100644 --- a/src/pages/backtesting/backtesting_page.py +++ b/src/pages/backtesting/backtesting_page.py @@ -69,7 +69,6 @@ class DynamicStrategy(Strategy): self.indicators[f"{ind_name}_lower"] = bb_vals[2] def next(self): - # Simple example strategy - should be customized based on user input price = self.data.Close[-1] # Example trading logic using indicators @@ -90,6 +89,33 @@ class DynamicStrategy(Strategy): self.buy() elif macd < signal and prev_macd >= prev_signal and self.position: self.position.close() + + elif ind_config['type'] == 'BB': + upper = self.indicators[f"{ind_name}_upper"][-1] + lower = self.indicators[f"{ind_name}_lower"][-1] + + # Buy when price touches lower band + if price <= lower and not self.position: + self.buy() + # Sell when price touches upper band + elif price >= upper and self.position: + self.position.close() + + elif ind_config['type'] == 'RSI': + rsi = self.indicators[ind_name][-1] + + # Buy when RSI is below 30 (oversold) + if rsi < 30 and not self.position: + self.buy() + # Sell when RSI is above 70 (overbought) + elif rsi > 70 and self.position: + self.position.close() + + elif ind_config['type'] == 'EMA': + if price > self.indicators[ind_name][-1] and not self.position: + self.buy() + elif price < self.indicators[ind_name][-1] and self.position: + self.position.close() def get_available_indicators() -> Dict: """Returns available indicators and their parameters"""