如何停止连续打印多个 BUY/SELL 信号? (Pine, Script, Trading, View, PineScript, TradingView)
How do I stop multiple BUY/SELL signals from printing in a row? (Pine, Script, Trading, View, PineScript, TradingView)
我正在尝试用我的脚本实现一些目标...
- 如果之前的信号是“卖出”,我只想打印“买入”信号(反之亦然)
- 我只想打印一个“买入”信号,如果它小于之前的“卖出”信号。 (反之亦然)
我整天都在努力寻找解决这个问题的办法。
我不明白如何使用“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。
干杯,祝你编码和交易好运
我正在尝试用我的脚本实现一些目标...
- 如果之前的信号是“卖出”,我只想打印“买入”信号(反之亦然)
- 我只想打印一个“买入”信号,如果它小于之前的“卖出”信号。 (反之亦然)
我整天都在努力寻找解决这个问题的办法。 我不明白如何使用“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。
干杯,祝你编码和交易好运