OPEN-SOURCE SCRIPT
Previous candle - D-T.R.A.D.I.N.G

//@version=6
indicator("Previous candle - byDen (Body % line & Wick size) [FINAL FIX]", overlay=true, max_boxes_count=200, max_lines_count=200, max_labels_count=200)
// ---------------- Inputs ----------------
showHighLow = input.bool(true, "Show High/Low lines")
showHLBox = input.bool(true, "Show High-Low Box (full range)")
showMidLine = input.bool(true, "Show Middle Line")
showCloseLine = input.bool(true, "Show Close Line")
showBodyPct = input.bool(true, "Show Body % Line")
enableWickBox = input.bool(true, "Show Wick-Body Box (previous candle)")
keepLastWick = input.bool(false, "Keep last Wick-Box when disabled")
bodyPct = input.float(25.0, "Body % Level", step=0.1, minval=0.0, maxval=100.0)
// colors / styles
cHighLow = input.color(color.white, "H/L Line Color")
hlStyleStr = input.string("solid", "H/L Style", options=["solid","dashed","dotted"])
hlWidth = input.int(1, "H/L Width", minval=1, maxval=5)
cMid = input.color(color.yellow, "Mid Line Color")
midStyleStr = input.string("dashed", "Mid Style", options=["solid","dashed","dotted"])
midWidth = input.int(1, "Mid Width", minval=1, maxval=5)
cClose = input.color(color.yellow, "Close Line Color")
closeStyleStr = input.string("dotted", "Close Style", options=["solid","dashed","dotted"])
closeWidth = input.int(1, "Close Width", minval=1, maxval=5)
cBoxBorder = input.color(color.red, "HL Box Border")
cBoxFill = color.new(input.color(color.gray, "HL Box Fill"), 85)
cBullBox = color.new(input.color(color.green, "Bull WickBox"), 80)
cBearBox = color.new(input.color(color.red, "Bear WickBox"), 80)
cWboxBorder = input.color(color.white, "WickBox Border")
cBodyPct = input.color(color.blue, "Body % Line Color")
bodyPctStyle = input.string("dashed", "Body % Line Style", options=["solid","dashed","dotted"])
bodyPctWidth = input.int(1, "Body % Line Width", minval=1, maxval=5)
// table colors / pip size
cPipBox = input.color(color.new(color.blue, 80), "Table Background Color")
cPipText = input.color(color.white, "Table Text Color")
pipSize = input.float(0.1, "Pip size (XAUUSD=0.1)", step=0.01)
wickLabelText = input.string("Wick", "Opis za wick")
bodyLabelText = input.string("H/L-Body%", "Opis za H/L-Body%")
descFontSizeStr = input.string("small", "Table Description Font Size", options=["tiny","small","normal","large","huge"])
valFontSizeStr = input.string("normal", "Table Value Font Size", options=["tiny","small","normal","large","huge"])
// custom texts for line labels
txtHigh = input.string("High", "Label Text - High")
txtLow = input.string("Low", "Label Text - Low")
txtMid = input.string("Mid", "Label Text - Mid")
txtClose = input.string("Close", "Label Text - Close")
txtBody = input.string("Body%", "Label Text - Body %")
lineFontSizeStr = input.string("small", "Line Label Font Size", options=["tiny","small","normal","large","huge"])
lineLabelPos = input.string("above", "Line Label Position", options=["above","below"])
lineLabelAlign = input.string("center", "Line Label Align", options=["left","center","right"])
// ---------------- Candle Display Options ----------------
highDisplay = input.string("Both", "High Line Display", options=["Both","Bullish","Bearish"])
lowDisplay = input.string("Both", "Low Line Display", options=["Both","Bullish","Bearish"])
midDisplay = input.string("Both", "Mid Line Display", options=["Both","Bullish","Bearish"])
closeDisplay = input.string("Both", "Close Line Display", options=["Both","Bullish","Bearish"])
bodyDisplay = input.string("Both", "Body % Line Display", options=["Both","Bullish","Bearish"])
hlBoxDisplay = input.string("Both", "HL Box Display", options=["Both","Bullish","Bearish"])
wickBoxDisplay = input.string("Both", "Wick Box Display", options=["Both","Bullish","Bearish"])
// timeframe filter
enabledTFs = input.string("1,5,15,30,60,240,D", "Enable on timeframes")
// ---------------- Helpers ----------------
is_tf_enabled(tfStr) =>
str.contains("," + enabledTFs + ",", "," + tfStr + ",")
line_style_from_str(s) =>
s == "dotted" ? line.style_dotted : s == "dashed" ? line.style_dashed : line.style_solid
get_font_size(fs) =>
fs == "tiny" ? size.tiny : fs == "small" ? size.small : fs == "large" ? size.large : fs == "huge" ? size.huge : size.normal
get_label_style(a) =>
a == "left" ? label.style_label_left : a == "right" ? label.style_label_right : label.style_label_center
get_y_pos(basePrice, pos) =>
pos == "above" ? basePrice + (syminfo.mintick * 10) : basePrice - (syminfo.mintick * 10)
// ---------------- Data ----------------
tf = timeframe.period
[prevO, prevH, prevL, prevC] = request.security(syminfo.tickerid, tf, [open, high, low, close], lookahead=barmerge.lookahead_off)
midLevel = (prevH + prevL) / 2
isBull = prevC > prevO
// ---------------- Persistent ----------------
var line lHigh = na
var line lLow = na
var line lMid = na
var line lClose = na
var line lBodyPct = na
var box bHL = na
var box wickBox = na
var table wickTable = na
var label labHigh = na
var label labLow = na
var label labMid = na
var label labClose = na
var label labBodyPct = na
offsetBars = input.int(20, "Bars right", minval=1, maxval=500)
leftX_base = bar_index
rightX_base = bar_index + offsetBars
if na(wickTable)
wickTable := table.new(position.bottom_right, 2, 2, border_width=1, frame_color=color.white)
// ---------------- DRAW ----------------
if barstate.isconfirmed and is_tf_enabled(tf)
// delete previous labels
if not na(labHigh)
label.delete(labHigh)
labHigh := na
if not na(labLow)
label.delete(labLow)
labLow := na
if not na(labMid)
label.delete(labMid)
labMid := na
if not na(labClose)
label.delete(labClose)
labClose := na
if not na(labBodyPct)
label.delete(labBodyPct)
labBodyPct := na
// --- DRAW HIGH ---
drawHigh = (highDisplay=="Both") or (highDisplay=="Bullish" and isBull) or (highDisplay=="Bearish" and not isBull)
if showHighLow and drawHigh
if not na(lHigh)
line.delete(lHigh)
lHigh := line.new(leftX_base, prevH, rightX_base, prevH, xloc=xloc.bar_index, color=cHighLow, width=hlWidth, style=line_style_from_str(hlStyleStr))
labHigh := label.new(rightX_base, get_y_pos(prevH,lineLabelPos), txtHigh, xloc=xloc.bar_index, yloc=yloc.price, style=get_label_style(lineLabelAlign), textcolor=cHighLow, size=get_font_size(lineFontSizeStr))
// --- DRAW LOW ---
drawLow = (lowDisplay=="Both") or (lowDisplay=="Bullish" and isBull) or (lowDisplay=="Bearish" and not isBull)
if showHighLow and drawLow
if not na(lLow)
line.delete(lLow)
lLow := line.new(leftX_base, prevL, rightX_base, prevL, xloc=xloc.bar_index, color=cHighLow, width=hlWidth, style=line_style_from_str(hlStyleStr))
labLow := label.new(rightX_base, get_y_pos(prevL,lineLabelPos), txtLow, xloc=xloc.bar_index, yloc=yloc.price, style=get_label_style(lineLabelAlign), textcolor=cHighLow, size=get_font_size(lineFontSizeStr))
// --- DRAW MID ---
drawMid = (midDisplay=="Both") or (midDisplay=="Bullish" and isBull) or (midDisplay=="Bearish" and not isBull)
if showMidLine and drawMid
if not na(lMid)
line.delete(lMid)
lMid := line.new(leftX_base, midLevel, rightX_base, midLevel, xloc=xloc.bar_index, color=cMid, width=midWidth, style=line_style_from_str(midStyleStr))
labMid := label.new(rightX_base, get_y_pos(midLevel,lineLabelPos), txtMid, xloc=xloc.bar_index, yloc=yloc.price, style=get_label_style(lineLabelAlign), textcolor=cMid, size=get_font_size(lineFontSizeStr))
// --- DRAW CLOSE ---
drawClose = (closeDisplay=="Both") or (closeDisplay=="Bullish" and isBull) or (closeDisplay=="Bearish" and not isBull)
if showCloseLine and drawClose
if not na(lClose)
line.delete(lClose)
lClose := line.new(leftX_base, prevC, rightX_base, prevC, xloc=xloc.bar_index, color=cClose, width=closeWidth, style=line_style_from_str(closeStyleStr))
labClose := label.new(rightX_base, get_y_pos(prevC,lineLabelPos), txtClose, xloc=xloc.bar_index, yloc=yloc.price, style=get_label_style(lineLabelAlign), textcolor=cClose, size=get_font_size(lineFontSizeStr))
// --- DRAW BODY% ---
float bodySize = math.abs(prevC-prevO)
float levelPct = na
drawBody = (bodyDisplay=="Both") or (bodyDisplay=="Bullish" and isBull) or (bodyDisplay=="Bearish" and not isBull)
if showBodyPct and bodySize>0 and drawBody
if not na(lBodyPct)
line.delete(lBodyPct)
levelPct := isBull ? (prevC - (bodyPct/100)*bodySize) : (prevC + (bodyPct/100)*bodySize)
lBodyPct := line.new(leftX_base, levelPct, rightX_base, levelPct, xloc=xloc.bar_index, color=cBodyPct, width=bodyPctWidth, style=line_style_from_str(bodyPctStyle))
labBodyPct := label.new(rightX_base, get_y_pos(levelPct,lineLabelPos), txtBody, xloc=xloc.bar_index, yloc=yloc.price, style=get_label_style(lineLabelAlign), textcolor=cBodyPct, size=get_font_size(lineFontSizeStr))
// --- BOXES ---
drawHLBox = (hlBoxDisplay=="Both") or (hlBoxDisplay=="Bullish" and isBull) or (hlBoxDisplay=="Bearish" and not isBull)
if showHLBox and drawHLBox
if not na(bHL)
box.delete(bHL)
bHL := box.new(leftX_base, prevH, rightX_base, prevL, xloc=xloc.bar_index, border_color=cBoxBorder, bgcolor=cBoxFill)
drawWickBox = (wickBoxDisplay=="Both") or (wickBoxDisplay=="Bullish" and isBull) or (wickBoxDisplay=="Bearish" and not isBull)
if enableWickBox and drawWickBox
float topP = isBull ? prevH : prevC
float botP = isBull ? prevC : prevL
if not na(topP) and not na(botP)
if not na(wickBox)
box.delete(wickBox)
wickBox := box.new(leftX_base, topP, rightX_base, botP, xloc=xloc.bar_index, border_color=cWboxBorder, bgcolor=(isBull?cBullBox:cBearBox))
else if not enableWickBox and not keepLastWick and not na(wickBox)
box.delete(wickBox)
wickBox := na
// --- TABLE (vedno na obeh) ---
float wickPips = isBull ? prevH-prevC : prevC-prevL
float bodyPips = na
if not na(levelPct)
bodyPips := isBull ? math.round((prevH-levelPct)/pipSize) : math.round((levelPct-prevL)/pipSize)
table.cell(wickTable, 0, 0, wickLabelText, text_color=cPipText, bgcolor=cPipBox, text_size=get_font_size(descFontSizeStr))
table.cell(wickTable, 1, 0, str.tostring(wickPips/pipSize), text_color=cPipText, bgcolor=cPipBox, text_size=get_font_size(valFontSizeStr))
if not na(bodyPips)
table.cell(wickTable, 0, 1, bodyLabelText, text_color=cPipText, bgcolor=cPipBox, text_size=get_font_size(descFontSizeStr))
table.cell(wickTable, 1, 1, str.tostring(bodyPips), text_color=cPipText, bgcolor=cPipBox, text_size=get_font_size(valFontSizeStr))
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.