After the strategy got stopped out (exit) I'd like to wait before I enter a new deal.
I tried to use barssince but I cannot find a condition meeting the last exit.
I expect to wait 4 hours after an exit occures before I enter a new trade.
//@version=3
strategy("ABC", shorttitle="ABC", initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.075, overlay = true, linktoseries = true)
nominal = 1000/close
SL = 0.0
SLold = nz(SL[1])
SL := max(max(max(strategy.position_avg_price,ohlc4)-atr(10)*3, strategy.position_avg_price * 0.96), SLold)
buy = cci(close,40)<50
testPeriodStart = timestamp(2018,2,1,0,0)
if time >= testPeriodStart
strategy.entry("Long", strategy.long, nominal, when = buy)
strategy.exit("Exit", "Long", stop = SL)
plot(SL, color = #006400)
Try this.
//@version=3
strategy("ABC", shorttitle="ABC", initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.075, overlay = true, linktoseries = true)
nominal = 1000/close
// create a variable with time of exit
timeExit = 0.0
timeExit := nz(timeExit[1])
msToHours(timeMs) =>
timeMs / 1000 / 60 / 60
isMoreThan4H() =>
msToHours(time - timeExit) >= 4
// detect the exit
if strategy.position_size < strategy.position_size[1]
timeExit := time
SL = 0.0
SLold = nz(SL[1])
SL := max(max(max(strategy.position_avg_price,ohlc4)-atr(10)*3, strategy.position_avg_price * 0.96), SLold)
buy = cci(close,40)<50
testPeriodStart = timestamp(2018,2,1,0,0)
if time >= testPeriodStart
strategy.entry("Long", strategy.long, nominal, when = buy and isMoreThan4H()) // Here added a check that is more than 4h since last exit
strategy.exit("Exit", "Long", stop = SL)
plot(SL, color = #006400)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.