diff --git a/src/pages/backtesting/backtesting_page.py b/src/pages/backtesting/backtesting_page.py index e5f656d..7347b68 100644 --- a/src/pages/backtesting/backtesting_page.py +++ b/src/pages/backtesting/backtesting_page.py @@ -98,38 +98,21 @@ class DynamicStrategy(Strategy): def next(self): price = self.data.Close[-1] - - # Check stop loss for existing position - if self.position and self.position.size > 0: # Make sure we have an active position - try: - # Calculate the percentage loss from entry - entry_price = self.position.open_price # Changed from price to open_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 - except AttributeError as e: - print(f"Warning: Could not check stop loss - {str(e)}") - - # 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)}") - + # Example trading logic using indicators for ind_name, ind_config in self.indicator_configs.items(): if ind_config['type'] == 'SMA': current_sma = self.indicators[ind_name][-1] print(f"SMA - Price: {price:.2f}, SMA: {current_sma:.2f}") if price > current_sma and not self.position: - print(f"BUY signal - Price above SMA") - self.buy() + # Set stop loss at 7% below the entry price + stop_loss = price * 0.93 + print(f"BUY signal - Price above SMA with stop loss at {stop_loss:.2f}") + self.buy(sl=stop_loss) elif price < current_sma and self.position: print(f"SELL signal - Price below SMA") self.position.close() - + elif ind_config['type'] == 'MACD': macd = self.indicators[f"{ind_name}_macd"][-1] signal = self.indicators[f"{ind_name}_signal"][-1] @@ -138,45 +121,45 @@ class DynamicStrategy(Strategy): print(f"MACD - MACD: {macd:.2f}, Signal: {signal:.2f}") if macd > signal and prev_macd <= prev_signal and not self.position: - print(f"BUY signal - MACD crossover") - self.buy() + stop_loss = price * 0.93 + print(f"BUY signal - MACD crossover with stop loss at {stop_loss:.2f}") + self.buy(sl=stop_loss) elif macd < signal and prev_macd >= prev_signal and self.position: print(f"SELL signal - MACD crossunder") self.position.close() - + elif ind_config['type'] == 'BB': upper = self.indicators[f"{ind_name}_upper"][-1] lower = self.indicators[f"{ind_name}_lower"][-1] print(f"BB - Price: {price:.2f}, Upper: {upper:.2f}, Lower: {lower:.2f}") - # Buy when price touches lower band if price <= lower and not self.position: - print(f"BUY signal - Price at lower band") - self.buy() - # Sell when price touches upper band + stop_loss = price * 0.93 + print(f"BUY signal - Price at lower band with stop loss at {stop_loss:.2f}") + self.buy(sl=stop_loss) elif price >= upper and self.position: print(f"SELL signal - Price at upper band") self.position.close() - + elif ind_config['type'] == 'RSI': rsi = self.indicators[ind_name][-1] print(f"RSI - Value: {rsi:.2f}") - # Buy when RSI is below 30 (oversold) if rsi < 30 and not self.position: - print(f"BUY signal - RSI oversold") - self.buy() - # Sell when RSI is above 70 (overbought) + stop_loss = price * 0.93 + print(f"BUY signal - RSI oversold with stop loss at {stop_loss:.2f}") + self.buy(sl=stop_loss) elif rsi > 70 and self.position: print(f"SELL signal - RSI overbought") self.position.close() - + elif ind_config['type'] == 'EMA': current_ema = self.indicators[ind_name][-1] print(f"EMA - Price: {price:.2f}, EMA: {current_ema:.2f}") if price > current_ema and not self.position: - print(f"BUY signal - Price above EMA") - self.buy() + stop_loss = price * 0.93 + print(f"BUY signal - Price above EMA with stop loss at {stop_loss:.2f}") + self.buy(sl=stop_loss) elif price < current_ema and self.position: print(f"SELL signal - Price below EMA") self.position.close()