Sunflower04
Sunflower04

Reputation: 1

How to draw a fan in Pine v5? Lines connecting open/close in a range?

Hello I'm new to pine and i have a question. I would like to write a script that helps me drawing lines connecting the close of the first candle in a selected range and the close of every other candle in the range.

I think I have some problem understanding pine runtime because using for loops or conditional structures seems bad but I can't find a solution around this. I tried with if but had no succes, the idea was that after i select the start/end point, the code should be something like this:

if bar_index > bar_index[barStart] and bar_index < bar_index[barEnd]
     line.new(bar_index[barStart], close[barStart], bar_index, close)
   else na

After this I tried with a for loop, again with no success:

for i = bar_index[barStart]+1 to bar_index[barEnd]
line.new(bar_index[barStart], close[barStart], bar_index[i], close[i])

The code I use to select the range and count the candles inside it is this one:

//@version=5
indicator("Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
p0          = input.price(defval = 0,                               confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
p1          = input.price(defval = 0,                               confirm = true)
///////////////////////////////////////////////////////////////////////////////////

// Bar counting 
t_bar(_t) =>
    var int _bar = na
    if time_close[1] <= _t and time >= _t
        _bar := bar_index
    _bar
    
start       =   int(t_bar(t0))
end         =   int(t_bar(t1))
//Counting bars in the selected range
barStart    =   bar_index - start
barEnd      =   bar_index - end
barDelta    =   end - start
//Print results
plot(barStart,  "Range start")
plot(barEnd,    "Range end")
plot(barDelta,  "Candles in range")

But from here on I don't know how to proceed. This should be pretty easy but I'm stuck.

What I'm trying to draw What I'm trying to draw

Thank you to anyone willing to help!!

Upvotes: 0

Views: 419

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3803

You don't need the loop or the input price variables. The lines can be drawn bar by bar as the script's execution enters your time range and the price variables can also be obtained at the same time.

//@version=5
indicator("Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
///////////////////////////////////////////////////////////////////////////////////

first_bar = time >= t0 and time[1] < t0
in_range = time > t0 and time <= t1
post_bar = time > t1 and time[1] <= t1

var float start_close = na
var int start_index = na

if first_bar
    start_close := close
    start_index := bar_index

if in_range and not first_bar
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)
    
if post_bar
    num_bars = bar_index[1] - start_index
    delta = close[1] - start_close
    info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
    label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)

Follow up question :

To draw the lines on a higher timeframe and have them "persist" once you move to a lower timeframe is a bit trickier. You will have to use an input to manually set the higher timeframe as the script has no way to determine the previous timeframe that it was applied to.

When you set t0 and t1 on the higher timeframe, the timestamp values will correspond to the opening time for those higher time frame bars. This isn't ideal as the lower timeframe candle that starts at this same time isn't the close value we are after.

By using request.security() we can then get the actual closing time of the higher timeframe bar which has the closing value we do want.

So we can use time to determine when we've started the correct higher time frame bars and then use time_close to determine when we are on the lower time frame bar that coincides with the higher timeframe close.

//@version=5
indicator("MTF Close", overlay=true)
//      Range Start
t0          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
//      Range End
t1          = input.time(timestamp("20 Jul 2021 00:00 +0300"),      confirm = true)
///////////////////////////////////////////////////////////////////////////////////

tf = input.timeframe("240", title = "higher timeframe")

htf_close = request.security(syminfo.tickerid, tf, time_close)

is_htf_closing_bar = time_close == htf_close

new_htf = ta.change(time(tf)) != 0

var bool started_first_htf_bar = false
var float start_close = na
var int start_index = na

var bool started_last_htf_bar = false

if time >= t0 and time[1] < t0 and new_htf
    started_first_htf_bar := true
else if new_htf
    started_first_htf_bar := false

if started_first_htf_bar and is_htf_closing_bar and na(start_close)
    start_close := close
    start_index := bar_index
else if not started_first_htf_bar and is_htf_closing_bar and time > t0 and time < t1
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)

if time >= t1 and time[1] < t1 and new_htf
    started_last_htf_bar := true
else if new_htf
    started_last_htf_bar := false

if started_last_htf_bar and is_htf_closing_bar
    line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)

post_bar = new_htf and started_last_htf_bar[1]

if post_bar
    num_bars = bar_index[1] - start_index
    delta = close[1] - start_close
    info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
    label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)    

Upvotes: 0

Related Questions