在满足条件的第一个柱的收盘价上画一条线

Plot a line on the closed price of the first bar that meets condition

更新:添加完整脚本。

Objective:在最近派发日的收盘价上创建一条线(类似于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")

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

请参阅用户手册中的 Variable declaration

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

编辑 29/01/2021:
这是您更新后的问题的解决方案。

//@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")