Reputation: 1
Not really sure why but when i use "stop" instead of "loss" in strategy.exit() my stop loss doesn't trigger on the same candle as the entry.
longCondition = DIPlus > DIMinus
initialEntryPrice = strategy.position_avg_price
stoplosslong = initialEntryPrice - 2
stoplossshort = initialEntryPrice + 2
if (longCondition and buy and buy3 and buy5 and timeIsAllowed)
strategy.entry("ENTER BUY", strategy.long)
alert('6247349125333,buy,' +syminfo.ticker+ ',sl=8,tp=4,risk=100', alert.freq_once_per_bar_close)
strategy.exit("EXIT BUY", profit= 4, stop = stoplosslong)
shortCondition = DIPlus < DIMinus
if (shortCondition and sell and sell3 and sell5 and timeIsAllowed)
strategy.entry("ENTER SELL", strategy.short)
alert('6247349125333,sell,' +syminfo.ticker+ ',sl=8,tp=4,risk=100', alert.freq_once_per_bar_close)
strategy.exit("EXIT SELL", profit=4, stop = stoplossshort)
Upvotes: 0
Views: 1143
Reputation: 1961
During backtesting strategy calculates on the close
of each bar.
For the script
//@version=5
strategy("My script", overlay = true)
strategy.entry("ENTER BUY", strategy.long)
strategy.exit("EXIT BUY", stop = strategy.position_avg_price)
those 2 commands will be executed at close
of the bar #0.
The entry will be placed only on the open
of the next bar. At the close
of the bar #0 the strategy.exit()
command will be ignored, because at this moment strategy.position_avg_price = na
(position is not opened yet).
After order placement at the open
of the bar #1, again, strategy will be recalculated on the close of this bar, and as the strategy.exit()
command will have finite value of the stop
parameter, exit command will be placed and executed after bar #1.
When you use loss
parameter, it has finite value, strategy.exit()
executes on the close
of the bar #0 and waiting for the "ENTER BUY"
position to be opened, and, after position is filled, the command to exit position might exit position immediately after loss
in ticks in reached.
If you want case 1 to work more like as case 2, you may enable calc_on_order_fills
to execute strategy.exit()
after "ENTER BUY" is opened.
Upvotes: 1