Felipe Gomes
Felipe Gomes

Reputation: 1

Script having trouble exiting at the target or stop (calc_on_order_fills)

So, this issue has been troubling me a lot. I'm going to summarize as much as I can. I have this simple script, and it works fine, but the problem occurs when there’s an apparent rapid market movement, the stop or target isn't respected—I’m referring to the backtester on historical data. When either is breached during a quick market move, the exit happens at the close of the breached bar. I will attach a screenshot with some examplesExit on close and not at target, the examples that I found is the price violating the target, but that happens in the same way at the stop limit.

Ok, that said, if you set calc_on_order_fills to true, it fixes the problem I just described. But then another, more significant problem arises. When it's on, it creates some fictional entries and exits that don't actually exist, and they generate profit. The equity curve changes completely and becomes a perfectly profitable curve. I can't understand the logic behind these invented entries; they are completely bizarre. I'll attach some screenshots of these entries Comparasion of entires with cal_on_order_fills on/off.

If the problem with calc_on_every_tick were resolved, or if the issue of exiting at the target or stop during high volatility were fixed, it would be perfect. This problem only happens with the identification of hammers and stars; it doesn't occur with the other script I have that identifies engulfing candles.

Script:

// @version=5
Strategy("Strategy Orders", overlay=true)

// Get user input
multiplier  = input.float(title="ATR Multiplier", defval=1.0)
rr          = input.float(title="Risk:Reward", defval=1.0)
fibLevel    = input.float(title="Fib Level", defval=0.33)

// Get ATR value
atr = ta.atr(14)

candleSize = high - low

// Calculate fibonacci level for current candle
bullFib = (low - high) * fibLevel + high
bearFib = (high - low) * fibLevel + low

// Determine which price source closes or opens highest/lowest
lowestBody  = close < open ? close : open
highestBody = close > open ? close : open

// Determine if we have a valid hammer or shooting star candle
hammerCandle = lowestBody >= bullFib 
starCandle   = highestBody <= bearFib 

buySignal = hammerCandle and not na(atr) and strategy.position_size == 0 and barstate.isconfirmed
sellSignal = starCandle and not na(atr) and strategy.position_size == 0 and barstate.isconfirmed

// Calculate stops & targets
longStop  = low - (atr * multiplier)
shortStop = high + (atr * multiplier)
longStopDistance  = close - longStop
shortStopDistance = shortStop - close
longTarget  = close + (longStopDistance * rr)
shortTarget = close - (shortStopDistance * rr)

// Save stops & targets
var t_stop = 0.0
var t_target = 0.0

// Enter buy orders
if buySignal
    strategy.entry(id="Long", direction=strategy.long)
    t_stop := longStop
    t_target := longTarget

// Enter sell orders
if sellSignal
    strategy.entry(id="Short", direction=strategy.short)
    t_stop := shortStop
    t_target := shortTarget

// Manage exit orders (TP & SL)
if (strategy.position_size > 0)
    strategy.exit(id="Long Exit", from_entry="Long", limit=t_target, stop=t_stop)

if (strategy.position_size < 0)
    strategy.exit(id="Short Exit", from_entry="Short", limit=t_target, stop=t_stop)

// Draw data to chart
plotshape(buySignal, style=shape.triangleup, color=color.green, location=location.belowbar)
plotshape(sellSignal, style=shape.triangledown, color=color.red, location=location.abovebar)
plot(strategy.position_size != 0 ? t_stop : na, color=color.red, style=plot.style_linebr)
plot(strategy.position_size != 0 ? t_target : na, color=color.green, style=plot.style_linebr)

// Draw trade data
plot(strategy.position_size != 0 or buySignal or sellSignal ? t_stop : na, title="Trade Stop Price",        color=color.red, style=plot.style_linebr)
plot(strategy.position_size != 0 or buySignal or sellSignal ? t_target : na, title="Trade Target Price", color=color.green, style=plot.style_linebr)

I've been trying to fix this for the past week. I tried various codes, used strategy.close, strategy.close combined with strategy.exit, and even added other variables to double-check the stop and target. I tried other things as well, but nothing worked. I simplified the identification of the candlestick pattern (hammer and stars), removed the 'fiblevel' input, and made this value a constant in an attempt to make it even simpler. Still, nothing worked.

Upvotes: 0

Views: 36

Answers (0)

Related Questions