You must declare your variable with the var
keyword.
That way, it'll keep it's value throughout the script.
See Variable declaration in the user manual.
Excerpt: The var keyword is a special modifier that instructs the compiler to create and initialize the variable only once. This behavior is very useful in cases where a variable’s value must persist through the iterations of a script across successive bars.
Edit 29/01/2021:
This is the solution to your updated question.
//@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")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…