refactor: Implement built-in stop loss functionality in DynamicStrategy

This commit is contained in:
Bobby (aider) 2025-02-13 23:55:14 -08:00
parent 03e454cb09
commit 32907718a1

View File

@ -99,33 +99,16 @@ 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()
@ -138,8 +121,9 @@ 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()
@ -149,11 +133,10 @@ class DynamicStrategy(Strategy):
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()
@ -162,11 +145,10 @@ class DynamicStrategy(Strategy):
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()
@ -175,8 +157,9 @@ class DynamicStrategy(Strategy):
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()