I am trying to write a code in pinescript to get the next day fibonacci pivot point

41 views Asked by At

I am trying to write a code to get the next day fibonacci pivot point. but the problem is that the lines are extending to infinite, whereas I want to limit it to only next day.

The code is as follows:

`indicator("Next Day Fibonacci Pivot Point",overlay = true)

todaysClose = request.security(syminfo.tickerid,"D",close,gaps =barmerge.gaps_off, lookahead =barmerge.lookahead_on)
todaysHigh = request.security(syminfo.tickerid,"D",high,gaps =barmerge.gaps_off, lookahead =barmerge.lookahead_on)
todaysLow = request.security(syminfo.tickerid,"D",low,gaps =barmerge.gaps_off, lookahead =barmerge.lookahead_on)

pivot = (todaysHigh+todaysClose+todaysLow)/3

R1 = (pivot)+0.382*(todaysHigh-todaysLow)
S1 = (pivot)-0.382*(todaysHigh-todaysLow)
R2 = (pivot)+0.618*(todaysHigh-todaysLow)
S2 = (pivot)-0.618*(todaysHigh-todaysLow)
R3 = (pivot)+1*(todaysHigh-todaysLow)
S3 = (pivot)-1*(todaysHigh-todaysLow)
R4 = (pivot)+0.382*(todaysHigh-todaysLow)
S4 = (pivot)-0.382*(todaysHigh-todaysLow)
R5 = (pivot)+2*(todaysHigh-todaysLow)
S5 = (pivot)-2*(todaysHigh-todaysLow)
R6 = (pivot)+2.414*(todaysHigh-todaysLow)
S6 = (pivot)-2.414*(todaysHigh-todaysLow)
R7 = (pivot)+3*(todaysHigh-todaysLow)
S7 = (pivot)-3*(todaysHigh-todaysLow)
R8 = (pivot)+3.414*(todaysHigh-todaysLow)
S8 = (pivot)-3.414*(todaysHigh-todaysLow)
R9 = (pivot)+4*(todaysHigh-todaysLow)
S9 = (pivot)-4*(todaysHigh-todaysLow)

if(barstate.islast)
    line.new(bar_index,pivot, bar_index+1, pivot, extend = extend.right, color= color.blue,width = 2)
    line.new(bar_index,R1, bar_index+1, R1, extend = extend.right, color= color.red,width = 2, xloc = xloc.bar_index)
    line.new(bar_index,S1, bar_index+1, S1, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R2, bar_index+1, R2, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S2, bar_index+1, S2, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R3, bar_index+1, R3, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S3, bar_index+1, S3, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R4, bar_index+1, R4, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S4, bar_index+1, S4, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R5, bar_index+1, R5, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S5, bar_index+1, S5, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R6, bar_index+1, R6, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S6, bar_index+1, S6, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R7, bar_index+1, R7, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S7, bar_index+1, S7, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R8, bar_index+1, R8, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S8, bar_index+1, S8, extend = extend.right, color= color.green,width = 2)
    line.new(bar_index,R9, bar_index+1, R9, extend = extend.right, color= color.red,width = 2)
    line.new(bar_index,S9, bar_index+1, S9, extend = extend.right, color= color.green,width = 2)
1

There are 1 answers

1
vitruvius On

Well, you have extend = extend.right in each line.new() call. That's why they extend.

If you want to limit this extension until the start of the next of day, you need to calculate that time and use xloc=xloc.bar_time instead of xloc=xloc.bar_index.

Here is an example:

//@version=5
indicator("My script", overlay=true)

is_new_day = timeframe.change("D")
timestamp_next_day = timestamp(syminfo.timezone, year, month, dayofmonth + 1, hour, minute, second)

bgcolor(is_new_day ? color.new(color.blue, 85) : na)

if (is_new_day)
    line.new(time, open, timestamp_next_day, open, xloc=xloc.bar_time)

enter image description here

Please note that the white vertical line is manually drawn by me to show you the timestamp (next day) where the blue line stops.

You may need to cover corner cases like last day of the month or last day of the year. They seem to work as far as I can see.