I have a question about the strategy tester and setting stop-losses. I am testing a strategy of buying/selling fractal breaks, and I'm storing the value of the latest fully formed fractal the following way:
// Store the fractal value in a variable
holdLastHigh = fixnan(upFractal?close:na)
holdLastLow = fixnan(dnFractal?close:na)
Thus, the values of holdLastHigh and holdLastLow will be continuously updated as new fractals form.
When it's time to write the exit strategy, I have:
// Define your exit rules
strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB)
where "stop_long_FB" is previously defined as:
stop_long_FB = holdLastLow
But I'd like "stop_long_FB" to have the same value as the "holdLastLow" during (and only during) the signal event. In other words, the stop should be the nearest opposite fractal during the original long signal. The stop loss should not be updated as fresh down fractals appear.
I've written the signal like this:
// Create a long entry based on a 7-pip break of the last upFractal:
signalLong = close >= holdLastHigh + (7 * syminfo.mintick)
My issue is that "strategy.entry" does not allow you to set a stop loss, only a stop entry, and I'm not sure whether "strategy.exit" will simply take the latest value of "stop_long_FB" (which depends on the value of "holdLastLow", which itself is being constantly updated) as opposed to the first value of "holdLastLow", when the long signal first occurs.
Any ideas? My full code is (still incomplete):
//@version=4
strategy("Pol Fractal Tester", shorttitle="P Fractals", format=format.price, precision=0, overlay=true, initial_capital=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=2)
// Define "n" as the number of periods and keep a minimum value of 2 for error handling.
n = 2
// Define a 3-bar fractal
upFractal = ((high[n+1] < high[n]) and (high[n-1] < high[n]))
dnFractal = ((low[n+1] > low[n]) and (low[n-1] > low[n]))
// Plot the fractals as shapes on the chart
plotshape(upFractal, style=shape.triangleup, size=size.small, location=location.abovebar, offset=-2, color=#29a89b, transp=0)
plotshape(dnFractal, style=shape.triangledown, size=size.small, location=location.belowbar, offset=-2, color=color.maroon, transp=0)
// Store the fractal value in a variable
holdLastHigh = fixnan(upFractal?high:na)
holdLastLow = fixnan(dnFractal?low:na)
// Create long and short signals based on fractal break of X pips
signalLong = close >= holdLastHigh + (7 * syminfo.mintick)
signalShort = close <= holdLastLow - (7 * syminfo.mintick)
// Set your stop-loss
stop_long_FB = holdLastLow
// Set your take-profits
tp_long =
strategy.entry("long", true, limit = holdLastHigh + 7, when = signalLong)
strategy.entry("short", false, limit = holdLastLow - 7, when = signalShort)
strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB, limit = tp_long)
Thank you.