'Pinescript multiple strategy.exit for one entry
I try to create two different strategy.exit function for one entry(one of them is for limit, other one is for stop). Because I want to take different comments from these two exit function.
Related code snippet:
strategy.entry("Longi" , direction = strategy.long, limit = close + 5)
strategy.exit("LongKapa" , from_entry = "Longi" , limit = xxx)
strategy.exit("LongKapa" , from_entry = "Longi" , stop = xxx)
But strategy tester neglect second one. So how can I get two different comments without using strategy.order in if block ?
Solution 1:[1]
Use logical or. Applicable to boolean expressions.
longCondition = ta.crossunder(ta.sma(close, 20), ta.sma(close, 50))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
exitCondition = ta.crossunder(ta.sma(close, 10), ta.sma(close, 20)) or ta.crossunder(ta.sma(close, 10), ta.sma(close, 50))
if (exitCondition)
strategy.exit("exit", "My Long Entry Id", profit = 10, loss = 5)
Solution 2:[2]
You should give it unique id's otherwise you will modify and not create a new one.
strategy.entry("Longi" , direction = strategy.long, limit = close + 5, qty=2)
strategy.exit("LongKapa-1" , from_entry = "Longi" , limit = xxx, qty=1)
strategy.exit("LongKapa-2" , from_entry = "Longi" , stop = xxx, qty=1)
https://www.tradingview.com/pine-script-reference/v5/#fun_strategy{dot}exit
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mazen Abduallah |
| Solution 2 | Roger Keulen |
