我正在尝试用我的脚本实现一些目标......
- 如果前一个信号是“卖出”,我只想打印“买入”信号(反之亦然)
- 我只想在小于之前的“卖出”信号时打印“买入”信号。 (反之亦然)
我一整天都在努力寻找这个问题的解决方案。
我不明白如何使用“valuewhen”或“barssince”来实现这一目标。
//Long
emalong = out1 > out2
macdlong = macd > signal and ((macd[1] < signal[1]) or (macd[2] <
signal[2]))
psarlong = psar < close and ((psar[1] > close[1]) or (psar[2] >
close[2]))
//Short
emashort = out1 < out2
macdshort = macd < signal and ((macd[1] > signal[1]) or (macd[2]
> signal[2]))
psarshort = psar > close and ((psar[1] < close[1]) or (psar[2] <
close[2]))
//Collect
longentry = emalong and macdlong and psarlong
shortentry = emashort and macdshort and psarshort
//Plot
plotshape(longentry, style=shape.circle, color=#26a69a, text="⬆",
textcolor=#ffffff, location=location.belowbar,
size=size.tiny)
plotshape(shortentry, style=shape.circle, color=#ef5350,
text="⬇", textcolor=#ffffff, location=location.abovebar,
size=size.tiny)
为此,我们需要创建一个不会在每个柱上重新计算的变量。 “var”让我们可以这样做,因为它将保留我们在运行时赋予它的任何值。然后我们可以有条件地为其分配一个新变量。在这种情况下,我们将使用您的多头和空头信号将多头或空头的数字分配为 1 或 -1。我在下面添加了注释:
// we create a variable that "saves" and doesnt calc on each bar
var pos = 0
// we save it to a new number when long happens. Long can be 1
if longentry and pos <= 0
pos := 1
// we save it again when short happens. Short can be -1
if shortentry and pos >= 0
pos := -1
// here we check if we have a newly detetected change from another number to our pos number this bar
// Is pos equal to 1 and was it not equal to 1 one bar ago
longsignal = pos == 1 and (pos != 1)[1]
shortsignal = pos == -1 and (pos != -1)[1]
//Plot
// we change our plot shape to coincide with the change detection
plotshape(longsignal, style=shape.circle, color=#26a69a, text="⬆", textcolor=#ffffff, location=location.belowbar, size=size.tiny)
plotshape(shortsignal, style=shape.circle, color=#ef5350, text="⬇", textcolor=#ffffff, location=location.abovebar, size=size.tiny)
从这个意义上说,我们甚至可以创建一个退出变量,使我们的头寸变为零。我们必须制定退出条件并以相同的方式将 pos 分配给 0。
干杯并祝您编码和交易好运
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)