Daily Candle Overlay on Lower Timeframe Chart Pine Script Tradingview

295 Views Asked by At
//@version=5
indicator("Daily Overlay", overlay = true)

htf = input.timeframe('D', 'Higher Time Frame', tooltip = 'Must be greater than timeframe of current chart.')
showWarn = input.bool(true, 'Display Warning Label if Current TF >= HTF')

var box body = na
var box upperWick = na
var box lowerWick = na
var startIndex = 0
var prevBarNO = 30
var prevStarts = array.new<int>(prevBarNO)

newTime = ta.change(time(htf))
[ohtf, hhtf, lhtf, chtf] = request.security(syminfo.tickerid, htf, [open, high, low, close])
bullish = chtf > ohtf

if newTime
    startIndex := bar_index
    if prevStarts.size() > prevBarNO
        prevStarts.pop()
        prevStarts.unshift(startIndex)
    else
        prevStarts.unshift(startIndex)

if newTime or barstate.islast
    (body[1]).delete(), (upperWick[1]).delete(), (lowerWick[1]).delete()
    boxColor = bullish ? color.green : color.red
    body := box.new(startIndex, bullish ? chtf : ohtf, bar_index, bullish ? ohtf : chtf, color.new(boxColor,70), bgcolor = color.new(boxColor,70))
    upperWick := box.new(startIndex, hhtf, bar_index, bullish ? chtf : ohtf, color.new(boxColor,90), bgcolor = color.new(boxColor,90))
    lowerWick := box.new(startIndex, bullish ? ohtf : chtf, bar_index, lhtf, color.new(boxColor,90), bgcolor = color.new(boxColor,90))

oldStart = ta.valuewhen(newTime, bar_index, 0)

[o1,h1,l1,c1,o2,h2,l2,c2,o3,h3,l3,c3,o4,h4,l4,c4,o5,h5,l5,c5,o6,h6,l6,c6,o7,h7,l7,c7,o8,h8,l8,c8,o9,h9,l9,c9,o10,h10,l10,c10,o11,h11,l11,c11,o12,h12,l12,c12,o13,h13,l13,c13,o14,h14,l14,c14,o15,h15,l15,c15,o16,h16,l16,c16,o17,h17,l17,c17,o18,h18,l18,c18,o19,h19,l19,c19,o20,h20,l20,c20,o21,h21,l21,c21,o22,h22,l22,c22,o23,h23,l23,c23,o24,h24,l24,c24,o25,h25,l25,c25,o26,h26,l26,c26,o27,h27,l27,c27,o28,h28,l28,c28,o29,h29,l29,c29]
 = request.security(syminfo.tickerid, htf, [open[1],high[1],low[1],close[1],open[2],high[2],low[2],close[2],
 open[3],high[3],low[3],close[3],open[4],high[4],low[4],close[4],open[5],high[5],low[5],close[5],
 open[6],high[6],low[6],close[6],open[7],high[7],low[7],close[7],open[8],high[8],low[8],close[8],
open[9],high[9],low[9],close[9],open[10],high[10],low[10],close[10],open[11],high[11],low[11],close[11],open[12],high[12],low[12],close[12],open[13],high[13],low[13],close[13],open[14],high[14],low[14],close[14],open[15],high[15],low[15],close[15],open[16],high[16],low[16],close[16],open[17],high[17],low[17],close[17],open[18],high[18],low[18],close[18],open[19],high[19],low[19],close[19],open[20],high[20],low[20],close[20],open[21],high[21],low[21],close[21],open[22],high[22],low[22],close[22],open[23],high[23],low[23],close[23],open[24],high[24],low[24],close[24],open[25],high[25],low[25],close[25],open[26],high[26],low[26],close[26],open[27],high[27],low[27],close[27],open[28],high[28],low[28],close[28],open[29],high[29],low[29],close[29]])

ohtf1 = array.from(o1,o2,o3,o4,o5,o6,o7,o8,o9,o10,o11,o12,o13,o14,o15,o16,o17,o18,o19,o20,o21,o22,o23,o24,o25,o26,o27,o28,o29)
hhtf1 = array.from(h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13,h14,h15,h16,h17,h18,h19,h20,h21,h22,h23,h24,h25,h26,h27,h28,h29)
lhtf1 = array.from(l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14,l15,l16,l17,l18,l19,l20,l21,l22,l23,l24,l25,l26,l27,l28,l29)
chtf1 = array.from(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22,c23,c24,c25,c26,c27,c28,c29)

if barstate.islast
    for i = 0 to (prevBarNO - 2)
        ohtfx = array.get(ohtf1,i)
        hhtfx = array.get(hhtf1,i)
        lhtfx = array.get(lhtf1,i)
        chtfx = array.get(chtf1,i)
        bullBar = ohtfx < chtfx
        box.new(prevStarts.get(i+1), bullBar? chtfx : ohtfx, prevStarts.get(i) - 1, bullBar ? ohtfx : chtfx, bullBar ? color.new(color.green,70) : color.new(color.red,70), bgcolor = bullBar ? color.new(color.green,70) : color.new(color.red,70))
        box.new(prevStarts.get(i+1), hhtfx, prevStarts.get(i) - 1, bullBar ? chtfx : ohtfx, bullBar ? color.new(color.green,90) : color.new(color.red,90), bgcolor = bullBar ? color.new(color.green,90) : color.new(color.red,90))
        box.new(prevStarts.get(i+1), lhtfx, prevStarts.get(i) - 1, bullBar ? ohtfx : chtfx, bullBar ? color.new(color.green,90) : color.new(color.red,90), bgcolor = bullBar ? color.new(color.green,90) : color.new(color.red,90))
       
if barstate.islast and timeframe.in_seconds() >= timeframe.in_seconds(htf) and showWarn
    label.new(bar_index, high, 'Please select a higher time frame from the indicator settings\nor change current the chart to lower time frame to display\nhigher time frame candles.', color=color.aqua, textcolor = color.white, textalign = text.align_left)

the code below although somewhat primitive was designed to give me overlay of daily candles on top of lower timeframe chart. when I get the higher timeframe OHLC data using request.security, I can get 18 past daily data and display them but once I try to access more than 18, it messes up the drawing on the chart (see screenshot). haven't been able to figure out the issue. if anyone has any ideas pls let me know.

Additionally, I would like it better if I could map the daily candles on the entire 1h chart as far back as server allows but manually requesting OHLC data of past candles makes it impossible to do. if anyone can help with this as well it is much appreciated. chart screenshot

1

There are 1 best solutions below

1
Gu5tavo71 On

In boxes, the default maximum value is 50.
You use 30 * 3 = 90

Try max_boxes_count

Something like that:

indicator("Daily Overlay", overlay = true, max_boxes_count = 200)