【问题标题】:How do I stop multiple BUY/SELL signals from printing in a row? (Pine, Script, Trading, View, PineScript, TradingView)如何阻止连续打印多个买入/卖出信号? (Pine, Script, Trading, View, PineScript, TradingView)
【发布时间】:2021-10-03 16:15:58
【问题描述】:

我正在尝试用我的脚本实现一些目标...

  1. 如果前一个信号是“卖出”,我只想打印一个“买入”信号(反之亦然)
  2. 如果它小于之前的“卖出”信号,我只想打印一个“买入”信号。 (反之亦然)

我整天都在努力寻找解决这个问题的方法。 我不明白如何使用“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)

【问题讨论】:

    标签: pine-script indicator


    【解决方案1】:

    为此,我们需要创建一个不会在每个柱上重新计算的变量。 “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。

    祝您在编码和交易中好运

    【讨论】:

    • 非常感谢!
    • 这正是我所需要的,效果很好。正如你所说,我也会用它来分别绘制出口和入口。非常感谢您的回复。
    • 太棒了!很高兴为您提供帮助
    • 如何绘制 2 个退出条件?一个在多头头寸之后,一个在空头头寸之后?我试图用 2 和 -2 分配它们,但显然这是一个愚蠢的想法。大声笑不知道如何分配他们。
    • 如果退出且 pos == 1 然后将 pos 分配给 0 在此之下。对短退出执行相同的操作,但检查 pos == -1。
    猜你喜欢
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多