UPDATE: Adding complete script.
Objective: Create a line (similar to hline) on close price of the most recent distribution day. I need the line to extend from 50 bars before this bar till last bar (today).
I am stuck in the for loop
and usage of var
and unable to retrieve the correct values.
//@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")
question from:https://stackoverflow.com/questions/65872007/plot-a-line-on-the-closed-price-of-the-first-bar-that-meets-condition