This shows how you can force an exit using forceLongExit
when your normal exitCondition
has not occurred:
//@version=4
strategy("S", "", true)
int barsLimit = input(10)
bool enterLong = rising(rsi(close, 20), 3)
bool exitCondition = crossunder(close, sma(close, 20))
bool forceLongExit = barssince(change(strategy.opentrades)) >= barsLimit
bool exitLong = exitCondition or forceLongExit
strategy.entry("Long", strategy.long, when = enterLong)
strategy.close("Long", when = exitLong)
//Debugging.
plotchar(enterLong, "enterLong", "▲", location.bottom, size = size.tiny)
plotchar(forceLongExit, "forceLongExit", "◄", location.top, size = size.tiny)
plotchar(exitCondition, "exitCondition", "▼", location.top, size = size.tiny)
It includes debugging plots that show on the chart when your conditions are met.
