Stop Loss should trigger in same bar as entry
Posted: Mon Nov 21, 2022 4:25 am
Hi coders, hoping someone can help me out here! I have a trade that is opened, and within the same candle, the stop loss price is breached. However, the strategy tester is not showing the stop loss order executing until the open of the next bar. I have bar magnifier turned on. I must have some sort of error in my logic, or the way I am writing my strategy.exit command. Any help would be much appreciated!
In the screenshot, the green arrow is the entry trigger bar (ignore the close long on that candle), the entry happens at the open of the next bar. The stop loss is offset by 1 but you can see the entry bar went below the SL value, but didn't actually close long until the open of the next bar.
// Entry Conditions
longEntry = strategy.position_size == 0 and priceAboveMA and direction < 0 and barstate.isconfirmed
shortEntry = strategy.position_size == 0 and priceBelowMA and direction > 0 and barstate.isconfirmed
// Calculate Trailing Stop and Take Profits
useTrailingStop = input.bool(defval=true, title="Use Trailing Stop?")
stopDistance = input.float(defval=3, title="Stop Loss Distance (ATR)", step=1)
tpMult = input.float(defval=3, title="Take Profit Multiple", step=0.1)
stopATR = ta.atr(14) * stopDistance
tpATR = ta.atr(14) * tpMult
bought = strategy.position_size[0] > strategy.position_size[1]
sold = strategy.position_size[0] < strategy.position_size[1]
since_long = ta.barssince(bought) + 1
since_short = ta.barssince(sold) + 1
longStopValue = 0.0
shortStopValue = 0.0
longStopPrice = 0.0
shortStopPrice = 999999999.0
longTP = 0.0
shortTP = 0.0
if strategy.position_size > 0
longStopValue := close[1] - stopATR
if not useTrailingStop
longStopPrice := close[since_long] - stopATR[since_long]
else
longStopPrice := math.max(longStopValue, longStopPrice[1])
longTP := close[since_long] + tpATR[since_long]
else
longStopPrice := 0.0
if strategy.position_size < 0
shortStopValue := close[1] + stopATR
if not useTrailingStop
shortStopPrice := close[since_short] + stopATR[since_short]
else
shortStopPrice := math.min(shortStopValue, shortStopPrice[1])
shortTP := close[since_short] - tpATR[since_short]
else
shortStopPrice := 999999999.0
if longEntry
strategy.entry(id="Long", direction=strategy.long)
if shortEntry
strategy.entry(id="Short", direction=strategy.short)
if strategy.position_size > 0
strategy.exit(id="Close Long", from_entry="Long", limit=longTP, stop=longStopPrice)
if strategy.position_size < 0
strategy.exit(id="Close Short", from_entry="Short", limit=shortTP, stop=shortStopPrice)
In the screenshot, the green arrow is the entry trigger bar (ignore the close long on that candle), the entry happens at the open of the next bar. The stop loss is offset by 1 but you can see the entry bar went below the SL value, but didn't actually close long until the open of the next bar.
// Entry Conditions
longEntry = strategy.position_size == 0 and priceAboveMA and direction < 0 and barstate.isconfirmed
shortEntry = strategy.position_size == 0 and priceBelowMA and direction > 0 and barstate.isconfirmed
// Calculate Trailing Stop and Take Profits
useTrailingStop = input.bool(defval=true, title="Use Trailing Stop?")
stopDistance = input.float(defval=3, title="Stop Loss Distance (ATR)", step=1)
tpMult = input.float(defval=3, title="Take Profit Multiple", step=0.1)
stopATR = ta.atr(14) * stopDistance
tpATR = ta.atr(14) * tpMult
bought = strategy.position_size[0] > strategy.position_size[1]
sold = strategy.position_size[0] < strategy.position_size[1]
since_long = ta.barssince(bought) + 1
since_short = ta.barssince(sold) + 1
longStopValue = 0.0
shortStopValue = 0.0
longStopPrice = 0.0
shortStopPrice = 999999999.0
longTP = 0.0
shortTP = 0.0
if strategy.position_size > 0
longStopValue := close[1] - stopATR
if not useTrailingStop
longStopPrice := close[since_long] - stopATR[since_long]
else
longStopPrice := math.max(longStopValue, longStopPrice[1])
longTP := close[since_long] + tpATR[since_long]
else
longStopPrice := 0.0
if strategy.position_size < 0
shortStopValue := close[1] + stopATR
if not useTrailingStop
shortStopPrice := close[since_short] + stopATR[since_short]
else
shortStopPrice := math.min(shortStopValue, shortStopPrice[1])
shortTP := close[since_short] - tpATR[since_short]
else
shortStopPrice := 999999999.0
if longEntry
strategy.entry(id="Long", direction=strategy.long)
if shortEntry
strategy.entry(id="Short", direction=strategy.short)
if strategy.position_size > 0
strategy.exit(id="Close Long", from_entry="Long", limit=longTP, stop=longStopPrice)
if strategy.position_size < 0
strategy.exit(id="Close Short", from_entry="Short", limit=shortTP, stop=shortStopPrice)