SEOSammo
SEOSammo

Reputation: 19

Plot Horizontal Lines At Higher Time Frame OHLC Values Using Pine Script

I'm trying to write a Pine Script indicator to plot horizontal lines at the OHLC values of different time frames.

(So see OHLC values of the daily chart when looking at 4H, for example).

The following works for the current time frame. But if I try to add the resolution parameter it doesn't work.

//@version=4
study("Time Frame Levels", overlay=true)

if barstate.isnew

    dailyLine = line.new(x1=bar_index[1], y1=close, x2=bar_index[0], y2=close, extend=extend.right, color=color.blue)

To reduce chart clutter, I also want to only plot lines that are +/- 5% of the current price.

Upvotes: 0

Views: 643

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6915

This draws lines for high and low of each previous timeframe.
For example, the daily line will mark the high and low of the previous day.

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

f_line_new() => line.new(na, na, na, na, xloc.bar_time, extend.right)

var line    line_5_h           = f_line_new()
var line    line_5_l           = f_line_new()
var line    line_15_h          = f_line_new()
var line    line_15_l          = f_line_new()
var line    line_30_h          = f_line_new()
var line    line_30_l          = f_line_new()
var line    line_h_h           = f_line_new()
var line    line_h_l           = f_line_new()
var line    line_d_h           = f_line_new()
var line    line_d_l           = f_line_new()
var line    line_w_h           = f_line_new()
var line    line_w_l           = f_line_new()
var line    line_m_h           = f_line_new()
var line    line_m_l           = f_line_new()
var line    line_q_h           = f_line_new()
var line    line_q_l           = f_line_new()
var line    line_y_h           = f_line_new()
var line    line_y_l           = f_line_new()

f_data_get(_res) => security(syminfo.tickerid, _res, [open,high,low,close,time,open[1],high[1],low[1],close[1],time[1],open[2],high[2],low[2],close[2],time[2]], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

f_line_move(_line, _x, _y) => 
    line.set_xy1(_line, _x,   _y)
    line.set_xy2(_line, _x+1, _y)

// Get data for all timeframes
[o0_5,  h0_5,  l0_5,  c0_5,  t0_5,  o1_5,  h1_5,  l1_5,  c1_5,  t1_5,  o2_5,  h2_5,  l2_5,  c2_5,  t2_5 ] = f_data_get("5")   
[o0_15, h0_15, l0_15, c0_15, t0_15, o1_15, h1_15, l1_15, c1_15, t1_15, o2_15, h2_15, l2_15, c2_15, t2_15] = f_data_get("15")   
[o0_30, h0_30, l0_30, c0_30, t0_30, o1_30, h1_30, l1_30, c1_30, t1_30, o2_30, h2_30, l2_30, c2_30, t2_30] = f_data_get("30")   
[o0_h,  h0_h,  l0_h,  c0_h,  t0_h,  o1_h,  h1_h,  l1_h,  c1_h,  t1_h,  o2_h,  h2_h,  l2_h,  c2_h,  t2_h ] = f_data_get("60")   
[o0_d,  h0_d,  l0_d,  c0_d,  t0_d,  o1_d,  h1_d,  l1_d,  c1_d,  t1_d,  o2_d,  h2_d,  l2_d,  c2_d,  t2_d ] = f_data_get("D")    
[o0_w,  h0_w,  l0_w,  c0_w,  t0_w,  o1_w,  h1_w,  l1_w,  c1_w,  t1_w,  o2_w,  h2_w,  l2_w,  c2_w,  t2_w ] = f_data_get("W")    
[o0_m,  h0_m,  l0_m,  c0_m,  t0_m,  o1_m,  h1_m,  l1_m,  c1_m,  t1_m,  o2_m,  h2_m,  l2_m,  c2_m,  t2_m ] = f_data_get("M")    
[o0_q,  h0_q,  l0_q,  c0_q,  t0_q,  o1_q,  h1_q,  l1_q,  c1_q,  t1_q,  o2_q,  h2_q,  l2_q,  c2_q,  t2_q ] = f_data_get("3M")   
[o0_y,  h0_y,  l0_y,  c0_y,  t0_y,  o1_y,  h1_y,  l1_y,  c1_y,  t1_y,  o2_y,  h2_y,  l2_y,  c2_y,  t2_y ] = f_data_get("12M")  

// Only move lines on last bar 
if barstate.islast
    f_line_move(line_5_h, t1_5, h1_5)
    f_line_move(line_5_l, t1_5, l1_5)

    f_line_move(line_15_h, t1_15, h1_15)
    f_line_move(line_15_l, t1_15, l1_15)

    f_line_move(line_30_h, t1_30, h1_30)
    f_line_move(line_30_l, t1_30, l1_30)

    f_line_move(line_h_h, t1_h, h1_h)
    f_line_move(line_h_l, t1_h, l1_h)

    f_line_move(line_d_h, t1_d, h1_d)
    f_line_move(line_d_l, t1_d, l1_d)

    f_line_move(line_w_h, t1_w, h1_w)
    f_line_move(line_w_l, t1_w, l1_w)

    f_line_move(line_m_h, t1_m, h1_m)
    f_line_move(line_m_l, t1_m, l1_m)

    f_line_move(line_q_h, t1_q, h1_q)
    f_line_move(line_q_l, t1_q, l1_q)

    f_line_move(line_y_h, t1_y, h1_y)
    f_line_move(line_y_l, t1_y, l1_y)

Upvotes: 2

Related Questions