pips bar
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Swingbird

//@version=5
indicator("pips bar", overlay = true)
pips = input.int(20, 'pips')
col_bar = input.color(color.orange, 'color')
wid_bar = input.int(4, 'bar width', [1,2,3,4])
xbars = input.int(10, 'gaps between the last bar')
bar_pos = input.string('top', 'position', ['top','middle','bottom'])

// pips and bar length
tkr = syminfo.ticker
pip = str.pos(tkr, 'J')==3 ? 0.01: 0.0001
bar_len = pip * pips

// virtical position
lbt = chart.left_visible_bar_time
rbt = chart.right_visible_bar_time
var float hp = na 
var float lp = na 

if time == lbt
    hp := high
    lp := low
if time > lbt and time <= rbt
    hp := hp < high ? high: hp
    lp := lp > low  ? low : lp

var float y1 = na
var float y2 = na 

if bar_pos == 'top'
    y1 := hp - bar_len
    y2 := hp
else if bar_pos == 'middle'
    y1 := (hp+lp)/2 - bar_len/2
    y2 := (hp+lp)/2 + bar_len/2
else
    y1 := lp
    y2 := lp + bar_len    

// horizontal position
dt = time - nz(time[1])
regular_dt = math.min(dt, nz(dt[1], dt))

var arr_dt = array.new_int()
if dt > nz(dt[1])
    arr_dt.push(dt)

var int wk_interval = 0 
if arr_dt.size()>2
    wk_interval := arr_dt.get(2)
weekend_time = request.security(syminfo.tickerid, 'W', time_close)

int weekend_adjust = 0
if session.islastbar and time_close == weekend_time
    weekend_adjust := wk_interval

pb_time = time + regular_dt*xbars + weekend_adjust

// pips bar and label
var pipsbar = line.new(0,0,0,0,xloc.bar_time, color = col_bar, width = wid_bar)
var la = label.new(0,0, str.tostring(pips) + ' pips' + '\n', xloc.bar_time, textcolor = col_bar, style = label.style_none)
// var tbl = table.new(position.top_right, 2, 2)
if barstate.islast
    pipsbar.set_xy1(pb_time, y1)
    pipsbar.set_xy2(pb_time, y2)
    la.set_xy(pb_time, y2)
    // tbl.cell(0,0, tkr)
    // tbl.cell(0,1, str.tostring(pip))


