简体   繁体   中英

How can I set a stop loss in TV's Strategy Tester that is based on some previous variable?

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.

You need to keep track of when you're entering trades and save the stop at that time. I used modified strategy.*() calls to test since TP wasn't defined. I also corrected index of the high/low saved when you encounter a new fractal, as it is offset by n when you identify it.

//@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.
// ————— Easier to change as an input.
n = input(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

var float holdLastHigh = na
var float holdLastLow = na
// ————— hi/lo to save needs to be indexed.
if upFractal
    holdLastHigh := high[n]
if dnFractal
    holdLastLow := low[n]
plot(change(holdLastHigh) ? holdLastHigh : na, "holdLastHigh", color.lime, 2, plot.style_linebr)
plot(change(holdLastLow) ? na : holdLastLow, "holdLastLow", color.maroon, 2, plot.style_linebr)

// Create long and short signals based on fractal break of X pips
var inLong = false
var inShort = false
signalLong = not inShort and not inLong and close >= holdLastHigh + (7 * syminfo.mintick)
signalShort = not inShort and not inLong and close <= holdLastLow - (7 * syminfo.mintick)
plotshape(signalLong, style=shape.triangleup, size=size.tiny, location=location.abovebar, color=color.teal, transp=0)
plotshape(signalShort, style=shape.triangledown, size=size.tiny, location=location.belowbar, color=color.maroon, transp=0)


// Set your stop-loss
var stop_long_FB = 0.
var stop_short_FB = 0.
if signalLong
    stop_long_FB := holdLastLow
if signalShort
    stop_short_FB := holdLastHigh
plot(inLong ? stop_long_FB : na, "stop_long_FB", color.aqua, 2, plot.style_circles)
plot(inShort ? stop_short_FB : na, "stop_short_FB", color.blue, 2, plot.style_circles)

// Set your take-profits

// tp_long =

strategy.entry("long", true, when = signalLong)
strategy.entry("short", false, when = signalShort)
strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB)
strategy.exit("Stop Loss/TP", "short", stop = stop_short_FB)

// 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)
// strategy.exit("Stop Loss/TP", "short", stop = stop_short_FB, limit = tp_long)

// Determine if strat is in a long/short position.
inLong  := strategy.position_size > 0
inShort := strategy.position_size < 0

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM