Translate

Views

Saturday, August 30, 2025

HTF Candle D/H4/H1/M15


 

//@version=6

indicator("HTF Candles — D / H4 / H1 / M15 (fixed)", overlay=true)

// --- Inputs
tfD  = input.timeframe("D",   "Daily TF")
tfH4 = input.timeframe("240", "H4 TF")
tfH1 = input.timeframe("60",  "H1 TF")
tfM15= input.timeframe("15",  "M15 TF")

shiftD  = input.int(45, "Shift D (bars to the right)")
shiftH4 = input.int(30, "Shift H4 (bars to the right)")
shiftH1 = input.int(15, "Shift H1 (bars to the right)")
shiftM15= input.int(0,  "Shift M15 (bars to the right)")

boxWidth = input.int(6, "Box width (bars)", minval=1)

// --- Fetch HTF OHLC via request.security
oD  = request.security(syminfo.tickerid, tfD,  open)
hD  = request.security(syminfo.tickerid, tfD,  high)
lD  = request.security(syminfo.tickerid, tfD,  low)
cD  = request.security(syminfo.tickerid, tfD,  close)

oH4 = request.security(syminfo.tickerid, tfH4, open)
hH4 = request.security(syminfo.tickerid, tfH4, high)
lH4 = request.security(syminfo.tickerid, tfH4, low)
cH4 = request.security(syminfo.tickerid, tfH4, close)

oH1 = request.security(syminfo.tickerid, tfH1, open)
hH1 = request.security(syminfo.tickerid, tfH1, high)
lH1 = request.security(syminfo.tickerid, tfH1, low)
cH1 = request.security(syminfo.tickerid, tfH1, close)

oM15 = request.security(syminfo.tickerid, tfM15, open)
hM15 = request.security(syminfo.tickerid, tfM15, high)
lM15 = request.security(syminfo.tickerid, tfM15, low)
cM15 = request.security(syminfo.tickerid, tfM15, close)

// --- Draw helper (vẽ 1 cây nến HTF ở vị trí offset)
drawHTF(_name, _o, _h, _l, _c, _shift) =>
    bcol = _c >= _o ? color.green : color.red
    left = bar_index + _shift
    right = bar_index + _shift + boxWidth
    top = math.max(_o, _c)
    bot = math.min(_o, _c)
    // thân
    _ = box.new(left, top, right, bot, border_color = bcol, bgcolor = color.new(bcol, 0), xloc = xloc.bar_index)
    // râu
    wickX = bar_index + _shift + math.round(boxWidth / 2)
    _ = line.new(wickX, _h, wickX, _l, color = bcol, xloc = xloc.bar_index)
    // label (giá trị)
    _ = label.new(bar_index + _shift + boxWidth + 1, _h, text = _name + " H: " + str.tostring(_h), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(bcol,80), textcolor = bcol)
    _ = label.new(bar_index + _shift + boxWidth + 1, _o, text = "O: " + str.tostring(_o), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(bcol,80), textcolor = bcol)
    _ = label.new(bar_index + _shift + boxWidth + 1, _l, text = "L: " + str.tostring(_l), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(bcol,80), textcolor = bcol)

// --- Xoá các đồ họa cũ và vẽ lại chỉ ở barstate.islast để tránh tạo quá nhiều object
if barstate.islast
    for ln in line.all
        ln.delete()
    for lb in label.all
        lb.delete()
    for bx in box.all
        bx.delete()

    // Vẽ 4 khung
    drawHTF("D",  oD,  hD,  lD,  cD,  shiftD)
    drawHTF("H4", oH4, hH4, lH4, cH4, shiftH4)
    drawHTF("H1", oH1, hH1, lH1, cH1, shiftH1)
    drawHTF("M15",oM15,hM15,lM15,cM15,shiftM15)

// --- (Tuỳ chọn) hiển thị bảng nhỏ thông tin
var tbl = table.new(position.middle_right, 1, 4, border_width=1, bgcolor=color.new(#eeeeee, 30))
if barstate.islast
    table.cell(tbl, 0, 0, "TF: " + tfD + " / " + tfH4 + " / " + tfH1 + " / " + tfM15)
    table.cell(tbl, 0, 1, "D O: " + str.tostring(oD) + "  C: " + str.tostring(cD))
    table.cell(tbl, 0, 2, "H4 O: " + str.tostring(oH4) + "  C: " + str.tostring(cH4))
    table.cell(tbl, 0, 3, "H1 O: " + str.tostring(oH1) + "  C: " + str.tostring(cH1))

No comments: