diff --git a/src/pages/backtesting/backtesting_page.py b/src/pages/backtesting/backtesting_page.py index 49db1e4..1865755 100644 --- a/src/pages/backtesting/backtesting_page.py +++ b/src/pages/backtesting/backtesting_page.py @@ -95,6 +95,18 @@ class DynamicStrategy(Strategy): def next(self): price = self.data.Close[-1] + # Check stop loss for existing position + if self.position: + # Calculate the percentage loss from entry + entry_price = self.position.entry_price + current_loss = ((price - entry_price) / entry_price) * 100 + + # If loss exceeds 7%, close the position + if current_loss < -7: + print(f"Stop loss triggered - Loss: {current_loss:.2f}%") + self.position.close() + return # Exit the method to prevent new entries on the same candle + # Debug current price and position if len(self.data.Close) % 20 == 0: # Log every 20th candle to avoid spam print(f"Current price: {price}, Has position: {bool(self.position)}")