feat: Add 7% stop loss mechanism to DynamicStrategy

This commit is contained in:
Bobby (aider) 2025-02-13 23:37:01 -08:00
parent 5f55b67070
commit 51be586696

View File

@ -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)}")