【问题标题】:Plot a line on the closed price of the first bar that meets condition在满足条件的第一个柱的收盘价上绘制一条线
【发布时间】:2021-04-28 12:07:24
【问题描述】:

更新:添加完整的脚本。

目标:在最近最近分发日的收盘价上创建一条线(类似于 hline)。我需要将这条线从这个小节之前的 50 个小节延伸到最后一个小节(今天)。

我被困在for loopvar 的使用中,无法检索正确的值。

 //@version=4
study(title="Distribution", shorttitle="Distribution Study", format=format.volume, overlay=true, resolution="1D")

//Input Parameters
distributionDayThreshold    = -0.4

//-----------------------------------------------

//Functions -------------------------------------
hasVolumeIncreased      = (volume-volume[1]) > 0
priceChangePercent      = (close-close[1])/close[1]*100

//Get All Distribution Days
getDistributionDays     = ((priceChangePercent < distributionDayThreshold ) and (hasVolumeIncreased))

conditionBool = getDistributionDays
var bar = -1
var barClose = -1.0
var barFlag = false
var index = -1

for i=0 to bar_index
    if conditionBool[i] and not barFlag
        bar             := bar_index[i]
        barClose        := close[i]
        barFlag         := true
        index           := i
        
plot(barClose, title="barClose")

//Paint all distribution days
bgcolor(conditionBool?color.red : na, title="distributionDay")

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    您必须使用 var 关键字声明您的变量。
    这样,它将在整个脚本中保持其价值。

    请参阅用户手册中的Variable declaration

    摘录:var 关键字是一个特殊的修饰符,它指示编译器只创建和初始化一次变量。这种行为在变量的值必须通过脚本在连续柱上的迭代中保持不变的情况下非常有用。

    2021 年 1 月 29 日编辑:
    这是您更新问题的解决方案。

    //@version=4
    // Cannot use resolution parameter due to error: [line 8: The 'resolution' argument is incompatible with functions that have side effects.]
    study(title="Distribution", shorttitle="Distribution Study", format=format.volume, overlay=true) //, resolution="D") 
    
    //Input Parameters
    distributionDayThreshold    = input(-0.4)
    drawLineBarsBack            = input(50)
    
    //Variables -------------------------------------
    var line    hline                   = line.new(na, na, na, na, extend=extend.right, color=color.purple)
    var int     bar_last_distribution   = na
    var float   close_last_distribution = na
    
    //Functions -------------------------------------
    hasVolumeIncreased      = change(volume) > 0
    priceChangePercent      = change(close) / close[1] * 100
    
    //Check if it's a distribution day
    isDistributionDay       = (priceChangePercent < distributionDayThreshold) and hasVolumeIncreased
    
    if isDistributionDay
        line.set_xy1(hline, bar_index - (bar_index >= drawLineBarsBack ? drawLineBarsBack : 0), close)
        line.set_xy2(hline, bar_index,                                                          close)
    
    //Paint all distribution days
    bgcolor(isDistributionDay ? color.red : na, title="distributionDay")
    

    【讨论】:

    • 谢谢。我已经用结果更新了我的帖子。
    • 您应该发布整个脚本而不是 sn-p。这样社区就可以更好地为您提供帮助。
    • 完成。 :) 用完整的代码和问题陈述更新了问题。
    • 正是我需要实现的目标。干杯!您能否还指出为什么我的逻辑没有按预期运行?我可以在 for 循环 / var 用法中进行哪些更改以以这种方式实现它。不一定是最佳做法,但我想了解哪里出了问题。
    • 我认为您的逻辑没有按预期运行,因为您编写它就像普通的编程语言会执行它一样。 Pine 的情况并非如此。 Pine 在每个历史柱上和实时柱上的每一次变动时运行您的脚本。您可能想阅读Execution model of Pine scripts 以获得更好的理解。
    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多