Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

pine script - How to have only the latest instance of certain plotted lines visible?

First: I'm pretty sure this is my poor understanding of how things work ??

I'm trying to plot a a "solid channel" with two horizontal straight lines as the upper and lower limits (red lines), and two straight horizontal lines that mark the first and the forth "forths" of the range between the upper and lower limits (the blue lines).

I was able to plot them. The problem is: I'd like to have only the last instance of these lines in the screen (based on the lookback period), but most of the time there are many of them.

Below you can see the code. And below the code, you can see in the first screenshot what I expect, and in the second and third screenshots you can see how it usually looks like.

Question is: how can I always have only one instance (the latest one) of this channel in the screen (i.e. only the four expected lines, two red and two blue)?

Any help will be highly appreciated! Cheers!

lookBack = 24
range = 4

highestHigh = highest(high, lookBack)
lowestLow = lowest(low, lookBack)

xAxisStartAt = bar_index[lookBack]
xAxisFinishesAt = bar_index

uppperLimit = highestHigh
lowerLimit = lowestLow

upperRange = ((highestHigh - lowestLow)/range) + lowestLow
lowerRange = highestHigh - ((highestHigh - lowestLow)/range)

// PAINTBRUSH
line.new(xAxisStartAt, uppperLimit, xAxisFinishesAt, uppperLimit, color=color.red, width= 3, extend=extend.none)
line.new(xAxisStartAt, lowerLimit, xAxisFinishesAt, lowerLimit, color=color.red, width= 3, extend=extend.none)
line.new(xAxisStartAt, upperRange, xAxisFinishesAt, upperRange, color=color.blue, width= 1, extend=extend.none)
line.new(xAxisStartAt, lowerRange, xAxisFinishesAt, lowerRange, color=color.blue, width= 1, extend=extend.none)

enter image description here enter image description here enter image description here


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

1 Reply

0 votes
by (71.8m points)

You must define your lines once, and then move the instances of the lines with line.set_xy1() and line.set_xy2()

//@version=4
study("TEST", overlay=true)

var ul = line.new(na, na, na, na, color=color.red,  width= 3, extend=extend.none)
var ll = line.new(na, na, na, na, color=color.red,  width= 3, extend=extend.none)
var ur = line.new(na, na, na, na, color=color.blue, width= 1, extend=extend.none)
var lr = line.new(na, na, na, na, color=color.blue, width= 1, extend=extend.none)

lookBack = 24
range = 4

highestHigh = highest(high, lookBack)
lowestLow = lowest(low, lookBack)

xAxisStartAt = bar_index[lookBack]
xAxisFinishesAt = bar_index

upperLimit = highestHigh
lowerLimit = lowestLow

upperRange = ((highestHigh - lowestLow)/range) + lowestLow
lowerRange = highestHigh - ((highestHigh - lowestLow)/range)

// PAINTBRUSH
line.set_xy1(ul, xAxisStartAt,    upperLimit)
line.set_xy2(ul, xAxisFinishesAt, upperLimit)

line.set_xy1(ll, xAxisStartAt,    lowerLimit)
line.set_xy2(ll, xAxisFinishesAt, lowerLimit)

line.set_xy1(ur, xAxisStartAt,    upperRange)
line.set_xy2(ur, xAxisFinishesAt, upperRange)

line.set_xy1(lr, xAxisStartAt,    lowerRange)
line.set_xy2(lr, xAxisFinishesAt, lowerRange)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...