Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

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 varand 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...