【问题标题】:Pine script - Count bar from conditionPine 脚本 - 从条件计数栏
【发布时间】:2023-03-13 11:43:02
【问题描述】:

当条件为真时,我如何获得条形编号? barsince give me always value 0

我需要创建一系列追踪止损。

这是我的代码

    SMA16=sma(close,16)
SMA50=sma(close,50)
plot(SMA16,color=color.red)
plot(SMA50,color=color.blue)



longcondition=crossover(SMA16,SMA50)

count = barssince(longcondition)

SL=valuewhen(longcondition,close,0)*0.5
TSL = SL + highest(high,count)-valuewhen(longcondition,close,0)

plot(TSL,color=color.navy)    
plot(SL,color=color.black)

【问题讨论】:

    标签: pine-script trading


    【解决方案1】:

    highest() 的问题是,在第一个小节 (bar_index = 0) 你传递给它的长度 = 0。你需要处理这个:

    //@version=4
    study("My Script", max_bars_back = 300)
    
    SMA16=sma(close,16)
    SMA50=sma(close,50)
    plot(SMA16,color=color.red)
    plot(SMA50,color=color.blue)
    
    longcondition=crossover(SMA16,SMA50)
    
    var int bar = 0
    var int count = 0
    
    if longcondition
        bar := bar_index
    count := bar_index - bar + 1
    
    SL=valuewhen(longcondition,close,0)*0.5
    TSL = SL + highest(high,count)-valuewhen(longcondition,close,0)
    
    plot(TSL,color=color.navy)    
    plot(SL,color=color.black)
    

    barssince()

    //@version=4
    study("My Script", max_bars_back = 300)
    
    SMA16=sma(close,16)
    SMA50=sma(close,50)
    plot(SMA16,color=color.red)
    plot(SMA50,color=color.blue)
    longcondition=crossover(SMA16,SMA50)
    
    b = barssince(longcondition)
    count = na(b)? 1 : b
    
    SL=valuewhen(longcondition,close,0)*0.5
    TSL = SL + highest(high,count+1)-valuewhen(longcondition,close,0)
    
    plot(TSL,color=color.navy)    
    plot(SL,color=color.black)
    

    【讨论】:

    • 谢谢,但我现在有不同的错误:Pine 无法确定系列的引用长度。尝试使用 max_bars_back
    • 更新示例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2022-01-12
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多