0
votes

I recently created a script in tradingview's pine editor.

study("bota", overlay=true)
 t = time("240", session.extended) // 1440=60*24 is the number of minutes in a whole day
is_first = na(t[1]) and not na(t) or t[1] < t


apertura = na


if is_first and barstate.isnew
    apertura := open
    
else
    apertura := apertura[1]
    

plot((apertura)+60, color=blue, linewidth=1, transp=20)
plot((apertura)-60, color=red, linewidth=1, transp=20)

In which when executing it does the following: https://www.tradingview.com/x/TmBHSzjj/

But what I am looking for is this: https://www.tradingview.com/x/4rRH1FTf/

What I want with the script is that when a four hour candle starts (utc) draw two lines until the end of those four hours (regardless of the timing of the chart) without showing the previous candles and for the line to continue until the end, 60 USD above initial price (blue line) and USD 60 below (red line). Look in some forums, but it seems that the pine editor reloads all the candles above and modifies the script according to the loaded candles.

1

1 Answers

0
votes

This should do what you intended.

//@version=4
// Credits for timeframe functions: https://www.tradingview.com/script/Wvcqygsx-MTF-Oscillator-Framework-PineCoders/
study("bota", overlay=true)

i_tf          = input("240",  "Timeframe for lines",                input.resolution)
i_offset_src  = input(open,   "Source to calculate offset from",    input.source)
i_offset      = input(60,     "Offset value",                       input.float)

var line    line_top = line.new(na, na, na, na, xloc=xloc.bar_time, color=color.blue)
var line    line_src = line.new(na, na, na, na, xloc=xloc.bar_time, color=color.gray, style=line.style_dotted)
var line    line_bot = line.new(na, na, na, na, xloc=xloc.bar_time, color=color.red)

// ————— Converts current chart resolution into a float minutes value.
f_resInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)

// ————— Returns resolution of _res string timeframe in minutes.
f_tfResInMinutes(_res) =>
    // _res: resolution of any TF (in `timeframe.period` string format).
    security(syminfo.tickerid, _res, f_resInMinutes())

var int tf_milliseconds = int(f_tfResInMinutes(i_tf) * 60 * 1000)
    
f_line_move(_line, _x, _y) => 
    line.set_xy1(_line, _x,                   _y)
    line.set_xy2(_line, _x + tf_milliseconds, _y)

[src,t] = security(syminfo.tickerid, i_tf, [i_offset_src,time], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)

if (barstate.islast)
    f_line_move(line_top, t, src + i_offset)
    f_line_move(line_src, t, src)
    f_line_move(line_bot, t, src - i_offset)