本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Digital Power | MTF Candles on Right", shorttitle="DP MTF Candles", overlay=true, max_boxes_count=100, max_lines_count=100, max_labels_count=100)
// =====================================================================
// SETTINGS
// =====================================================================
string grpTf = "1. Higher timeframes"
bool showTf1 = input.bool(true, "Show", inline="tf1", group=grpTf)
string tf1 = input.timeframe("240", "Group 1", inline="tf1", group=grpTf)
bool showTf2 = input.bool(true, "Show", inline="tf2", group=grpTf)
string tf2 = input.timeframe("D", "Group 2", inline="tf2", group=grpTf)
bool showTf3 = input.bool(true, "Show", inline="tf3", group=grpTf)
string tf3 = input.timeframe("W", "Group 3", inline="tf3", group=grpTf)
string grpLayout = "2. Candle layout"
int candleCount = input.int(3, "Candles per group", minval=1, maxval=5, group=grpLayout)
int rightOffset = input.int(5, "Distance to the right (bars)", minval=2, maxval=50, group=grpLayout)
int candleWidth = input.int(3, "Body width (bars)", minval=2, maxval=6, group=grpLayout)
int candleGap = input.int(1, "Space between candles", minval=1, maxval=8, group=grpLayout)
int groupGap = input.int(5, "Space between groups", minval=4, maxval=24, group=grpLayout)
bool showTitles = input.bool(true, "Show timeframe titles", group=grpLayout)
bool showOHLC = input.bool(false, "Show last candle OHLC on each group", group=grpLayout)
bool showTips = input.bool(true, "Show OHLC tooltips (hover over dots)", group=grpLayout)
string grpColors = "3. Candle colors"
color bullColor = input.color(color.rgb(23, 158, 140), "Bullish body", group=grpColors)
color bearColor = input.color(color.rgb(225, 78, 90), "Bearish body", group=grpColors)
color wickColor = input.color(color.gray, "Wick", group=grpColors)
int bodyOpacity = input.int(0, "Body transparency", minval=0, maxval=90, group=grpColors)
// =====================================================================
// DATA MODEL: Each actual HTF candle, including the developing one
// =====================================================================
type HtfCandle
int startTime
float op
float hi
float lo
float cl
// Keep the most recent candles, updating the live candle in place.
f_store(array<HtfCandle> history, int t, float o, float h, float l, float c) =>
if not na(t) and not na(o) and not na(h) and not na(l) and not na(c)
int n = array.size(history)
if n == 0
array.push(history, HtfCandle.new(t, o, h, l, c))
else
HtfCandle last = array.get(history, n - 1)
if t == last.startTime
array.set(history, n - 1, HtfCandle.new(t, o, h, l, c))
else if t > last.startTime
array.push(history, HtfCandle.new(t, o, h, l, c))
if array.size(history) > 6
array.shift(history)
f_tfName(string tf) =>
tf == "240" ? "4H" : tf == "60" ? "1H" : tf == "D" or tf == "1D" ? "1D" : tf == "W" or tf == "1W" ? "1W" : tf == "M" or tf == "1M" ? "1M" : tf
f_details(HtfCandle c, string caption) =>
caption + " | " + str.format_time(c.startTime, "yyyy-MM-dd HH:mm") +
"\nO: " + str.tostring(c.op, format.mintick) +
"\nH: " + str.tostring(c.hi, format.mintick) +
"\nL: " + str.tostring(c.lo, format.mintick) +
"\nC: " + str.tostring(c.cl, format.mintick)
// =====================================================================
// DRAWING OBJECT MANAGEMENT
// =====================================================================
var array<box> drawnBoxes = array.new<box>()
var array<line> drawnLines = array.new<line>()
var array<label> drawnLabels = array.new<label>()
f_clear() =>
while array.size(drawnBoxes) > 0
box.delete(array.pop(drawnBoxes))
while array.size(drawnLines) > 0
line.delete(array.pop(drawnLines))
while array.size(drawnLabels) > 0
label.delete(array.pop(drawnLabels))
// Returns the x coordinate from which the next group should begin.
f_drawGroup(array<HtfCandle> history, int xStart, string caption, float atrValue) =>
int available = array.size(history)
int visible = math.min(available, candleCount)
int nextX = xStart
if visible > 0
float groupHigh = na
float groupLow = na
for i = 0 to visible - 1
HtfCandle c = array.get(history, available - visible + i)
int leftX = xStart + i * (candleWidth + candleGap)
int rightX = leftX + candleWidth
int centerX = leftX + int(candleWidth / 2)
color barColor = c.cl >= c.op ? bullColor : bearColor
groupHigh := na(groupHigh) ? c.hi : math.max(groupHigh, c.hi)
groupLow := na(groupLow) ? c.lo : math.min(groupLow, c.lo)
// Wick must be drawn before the body.
line wick = line.new(x1=centerX, y1=c.hi, x2=centerX, y2=c.lo, xloc=xloc.bar_index, color=wickColor, width=1)
array.push(drawnLines, wick)
float top = math.max(c.op, c.cl)
float bottom = math.min(c.op, c.cl)
if top == bottom
line doji = line.new(x1=leftX, y1=c.cl, x2=rightX, y2=c.cl, xloc=xloc.bar_index, color=barColor, width=2)
array.push(drawnLines, doji)
else
box body = box.new(left=leftX, top=top, right=rightX, bottom=bottom, xloc=xloc.bar_index, border_color=barColor, border_width=1, bgcolor=color.new(barColor, bodyOpacity))
array.push(drawnBoxes, body)
// A small dot carries the timestamp and full OHLC in its tooltip.
if showTips
label tooltipDot = label.new(x=centerX, y=c.hi, text="·", xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, textcolor=chart.fg_color, size=size.tiny, tooltip=f_details(c, caption))
array.push(drawnLabels, tooltipDot)
// Optional numeric OHLC panel for the most recent candle only.
if showOHLC and i == visible - 1
label info = label.new(x=rightX + 1, y=c.cl, text=f_details(c, caption), xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, color=color.new(chart.bg_color, 15), textcolor=chart.fg_color, size=size.tiny, textalign=text.align_left)
array.push(drawnLabels, info)
if showTitles
int titleX = xStart + int(((visible - 1) * (candleWidth + candleGap) + candleWidth) / 2)
float titleY = groupHigh + math.max(nz(atrValue, 0.0) * 0.45, syminfo.mintick * 10.0)
label heading = label.new(x=titleX, y=titleY, text=caption, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_down, color=color.new(chart.bg_color, 15), textcolor=chart.fg_color, size=size.small)
array.push(drawnLabels, heading)
nextX := xStart + visible * (candleWidth + candleGap) + groupGap
nextX
// =====================================================================
// REQUEST HIGHER-TIMEFRAME DATA ON EACH CHART BAR (NOT ONLY LAST BAR)
// =====================================================================
[o1, h1, l1, c1, t1] = request.security(syminfo.tickerid, tf1, [open, high, low, close, time], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
[o2, h2, l2, c2, t2] = request.security(syminfo.tickerid, tf2, [open, high, low, close, time], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
[o3, h3, l3, c3, t3] = request.security(syminfo.tickerid, tf3, [open, high, low, close, time], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
var array<HtfCandle> history1 = array.new<HtfCandle>()
var array<HtfCandle> history2 = array.new<HtfCandle>()
var array<HtfCandle> history3 = array.new<HtfCandle>()
f_store(history1, t1, o1, h1, l1, c1)
f_store(history2, t2, o2, h2, l2, c2)
f_store(history3, t3, o3, h3, l3, c3)
int chartSec = timeframe.in_seconds(timeframe.period)
bool valid1 = timeframe.in_seconds(tf1) >= chartSec
bool valid2 = timeframe.in_seconds(tf2) >= chartSec
bool valid3 = timeframe.in_seconds(tf3) >= chartSec
float atr14 = ta.atr(14)
// Delete old objects and render once at the most recent chart bar.
if barstate.islast
f_clear()
int nextX = bar_index + rightOffset
if showTf1 and valid1
nextX := f_drawGroup(history1, nextX, f_tfName(tf1), atr14)
if showTf2 and valid2
nextX := f_drawGroup(history2, nextX, f_tfName(tf2), atr14)
if showTf3 and valid3
nextX := f_drawGroup(history3, nextX, f_tfName(tf3), atr14)
if (showTf1 and not valid1) or (showTf2 and not valid2) or (showTf3 and not valid3)
label warning = label.new(x=bar_index + 2, y=high, text="Some selected timeframes are LOWER than the chart timeframe; those groups are hidden.", xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.orange, 20), textcolor=color.black, size=size.tiny)
array.push(drawnLabels, warning)