[英]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. 因此,holdLastHigh和holdLastLow的值将作为新的分形形式不断更新。
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”先前定义为:
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. 但是我希望“ stop_long_FB”在信号事件期间(并且仅在信号事件期间)具有与“ holdLastLow”相同的值。 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. 我的问题是,“ strategy.entry”不允许您设置止损,只能设置止损条目,而且我不确定“ strategy.exit”是否仅采用“ stop_long_FB”的最新值(具体取决于当长信号第一次出现时,与“ holdLastLow”的第一个值相反,“ holdLastLow”的值(其本身一直在不断更新)。
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. 由于未定义TP,因此我使用了经过修改的
strategy.*()
调用进行测试。 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. 我还纠正了遇到新的分形时所保存的高/低索引,因为当您识别它时它会被
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.