refactor: Implement built-in stop loss functionality in DynamicStrategy
This commit is contained in:
parent
03e454cb09
commit
32907718a1
@ -99,33 +99,16 @@ class DynamicStrategy(Strategy):
|
|||||||
def next(self):
|
def next(self):
|
||||||
price = self.data.Close[-1]
|
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
|
# Example trading logic using indicators
|
||||||
for ind_name, ind_config in self.indicator_configs.items():
|
for ind_name, ind_config in self.indicator_configs.items():
|
||||||
if ind_config['type'] == 'SMA':
|
if ind_config['type'] == 'SMA':
|
||||||
current_sma = self.indicators[ind_name][-1]
|
current_sma = self.indicators[ind_name][-1]
|
||||||
print(f"SMA - Price: {price:.2f}, SMA: {current_sma:.2f}")
|
print(f"SMA - Price: {price:.2f}, SMA: {current_sma:.2f}")
|
||||||
if price > current_sma and not self.position:
|
if price > current_sma and not self.position:
|
||||||
print(f"BUY signal - Price above SMA")
|
# Set stop loss at 7% below the entry price
|
||||||
self.buy()
|
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:
|
elif price < current_sma and self.position:
|
||||||
print(f"SELL signal - Price below SMA")
|
print(f"SELL signal - Price below SMA")
|
||||||
self.position.close()
|
self.position.close()
|
||||||
@ -138,8 +121,9 @@ class DynamicStrategy(Strategy):
|
|||||||
|
|
||||||
print(f"MACD - MACD: {macd:.2f}, Signal: {signal:.2f}")
|
print(f"MACD - MACD: {macd:.2f}, Signal: {signal:.2f}")
|
||||||
if macd > signal and prev_macd <= prev_signal and not self.position:
|
if macd > signal and prev_macd <= prev_signal and not self.position:
|
||||||
print(f"BUY signal - MACD crossover")
|
stop_loss = price * 0.93
|
||||||
self.buy()
|
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:
|
elif macd < signal and prev_macd >= prev_signal and self.position:
|
||||||
print(f"SELL signal - MACD crossunder")
|
print(f"SELL signal - MACD crossunder")
|
||||||
self.position.close()
|
self.position.close()
|
||||||
@ -149,11 +133,10 @@ class DynamicStrategy(Strategy):
|
|||||||
lower = self.indicators[f"{ind_name}_lower"][-1]
|
lower = self.indicators[f"{ind_name}_lower"][-1]
|
||||||
|
|
||||||
print(f"BB - Price: {price:.2f}, Upper: {upper:.2f}, Lower: {lower:.2f}")
|
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:
|
if price <= lower and not self.position:
|
||||||
print(f"BUY signal - Price at lower band")
|
stop_loss = price * 0.93
|
||||||
self.buy()
|
print(f"BUY signal - Price at lower band with stop loss at {stop_loss:.2f}")
|
||||||
# Sell when price touches upper band
|
self.buy(sl=stop_loss)
|
||||||
elif price >= upper and self.position:
|
elif price >= upper and self.position:
|
||||||
print(f"SELL signal - Price at upper band")
|
print(f"SELL signal - Price at upper band")
|
||||||
self.position.close()
|
self.position.close()
|
||||||
@ -162,11 +145,10 @@ class DynamicStrategy(Strategy):
|
|||||||
rsi = self.indicators[ind_name][-1]
|
rsi = self.indicators[ind_name][-1]
|
||||||
print(f"RSI - Value: {rsi:.2f}")
|
print(f"RSI - Value: {rsi:.2f}")
|
||||||
|
|
||||||
# Buy when RSI is below 30 (oversold)
|
|
||||||
if rsi < 30 and not self.position:
|
if rsi < 30 and not self.position:
|
||||||
print(f"BUY signal - RSI oversold")
|
stop_loss = price * 0.93
|
||||||
self.buy()
|
print(f"BUY signal - RSI oversold with stop loss at {stop_loss:.2f}")
|
||||||
# Sell when RSI is above 70 (overbought)
|
self.buy(sl=stop_loss)
|
||||||
elif rsi > 70 and self.position:
|
elif rsi > 70 and self.position:
|
||||||
print(f"SELL signal - RSI overbought")
|
print(f"SELL signal - RSI overbought")
|
||||||
self.position.close()
|
self.position.close()
|
||||||
@ -175,8 +157,9 @@ class DynamicStrategy(Strategy):
|
|||||||
current_ema = self.indicators[ind_name][-1]
|
current_ema = self.indicators[ind_name][-1]
|
||||||
print(f"EMA - Price: {price:.2f}, EMA: {current_ema:.2f}")
|
print(f"EMA - Price: {price:.2f}, EMA: {current_ema:.2f}")
|
||||||
if price > current_ema and not self.position:
|
if price > current_ema and not self.position:
|
||||||
print(f"BUY signal - Price above EMA")
|
stop_loss = price * 0.93
|
||||||
self.buy()
|
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:
|
elif price < current_ema and self.position:
|
||||||
print(f"SELL signal - Price below EMA")
|
print(f"SELL signal - Price below EMA")
|
||||||
self.position.close()
|
self.position.close()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user