feat: Add 7% stop loss mechanism to DynamicStrategy
This commit is contained in:
parent
5f55b67070
commit
51be586696
@ -95,6 +95,18 @@ 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:
|
||||||
|
# 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
|
# Debug current price and position
|
||||||
if len(self.data.Close) % 20 == 0: # Log every 20th candle to avoid spam
|
if len(self.data.Close) % 20 == 0: # Log every 20th candle to avoid spam
|
||||||
print(f"Current price: {price}, Has position: {bool(self.position)}")
|
print(f"Current price: {price}, Has position: {bool(self.position)}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user