我如何根据先前的一些变量在 TV 的策略测试器中设置止损?

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

我对策略测试器和设置止损有疑问。我正在测试 buying/selling 分形突破的策略,我正在通过以下方式存储最新完全形成的分形的值:

// Store the fractal value in a variable
holdLastHigh = fixnan(upFractal?close:na)
holdLastLow = fixnan(dnFractal?close:na)

因此,holdLastHigh 和 holdLastLow 的值将随着新的分形形式不断更新。

写退出策略的时候,我有:

// Define your exit rules
strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB)

其中 "stop_long_FB" 之前定义为:

stop_long_FB = holdLastLow

但我希望 "stop_long_FB" 在信号事件期间(且仅在信号事件期间)具有与 "holdLastLow" 相同的值。换句话说,止损应该是原始多头信号期间最近的相反分形。止损不应随着新的向下分形出现而更新。

我是这样写信号的:

// Create a long entry based on a 7-pip break of the last upFractal:
signalLong = close >= holdLastHigh + (7 * syminfo.mintick)

我的问题是"strategy.entry"不允许你设置止损,只能设置止损入场,我不确定"strategy.exit"是否会简单地采取 "stop_long_FB" 的 latest 值(取决于 "holdLastLow" 的值,它本身正在不断更新),而不是 first 的值"holdLastLow",长信号首次出现时。

有什么想法吗?我的完整代码是(仍然不完整):

//@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)

谢谢。

您需要跟踪进入交易的时间并保存当时的止损。由于未定义 TP,我使用修改后的 strategy.*() 调用进行测试。我还更正了遇到新分形时保存的 high/low 的索引,因为当您识别它时它会被 n 偏移。

//@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