MTF PO3 Candles + Labels

wenlang0226 · study · 139 行 · 点赞 16 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("MTF PO3 Candles + Labels", "MTF PO3", overlay = true, max_boxes_count = 100, max_lines_count = 100, max_labels_count = 50)
// Invisible compiler anchor. All visible output is created with drawings below.
plot(na, "Drawing anchor", display = display.none)
// This indicator is designed for charts at 5 minutes or lower so every requested
// timeframe is equal to or higher than the chart timeframe.
// ─────────────────────────────────────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────────────────────────────────────
string GROUP_DISPLAY = "Display"
bool show5m  = input.bool(true, "5m",  inline = "tf", group = GROUP_DISPLAY)
bool show15m = input.bool(true, "15m", inline = "tf", group = GROUP_DISPLAY)
bool show30m = input.bool(true, "30m", inline = "tf", group = GROUP_DISPLAY)
bool show1h  = input.bool(true, "1H",  inline = "tf", group = GROUP_DISPLAY)
bool show4h  = input.bool(true, "4H",  inline = "tf", group = GROUP_DISPLAY)
bool includeLive = input.bool(true, "Include the live candle", tooltip = "On: C3 is the currently forming candle. Off: all three candles are confirmed/closed.", group = GROUP_DISPLAY)
string labelPosition = input.string("Bottom", "Timeframe label position", options = ["Bottom", "Top"], group = GROUP_DISPLAY)
string GROUP_LAYOUT = "Layout"
int positionOffset = input.int(12, "Distance from current price", minval = 1, maxval = 200, group = GROUP_LAYOUT)
int candleWidth = input.int(2, "Candle width", minval = 1, maxval = 8, group = GROUP_LAYOUT)
int candleGap = input.int(1, "Gap between candles", minval = 0, maxval = 8, group = GROUP_LAYOUT)
int groupGap = input.int(5, "Gap between timeframes", minval = 1, maxval = 20, group = GROUP_LAYOUT)
float labelPadding = input.float(0.10, "Label padding (group range)", minval = 0.02, maxval = 1.00, step = 0.01, group = GROUP_LAYOUT)
string labelSize = input.string(size.large, "Label size", options = [size.normal, size.large, size.huge], group = GROUP_LAYOUT)
string GROUP_STYLE = "Style"
color bullBody = input.color(color.rgb(8, 153, 129), "Bull body", inline = "body", group = GROUP_STYLE)
color bearBody = input.color(color.rgb(242, 54, 69), "Bear body", inline = "body", group = GROUP_STYLE)
int bodyTransparency = input.int(0, "Transparency", minval = 0, maxval = 95, inline = "body", group = GROUP_STYLE)
color bullWick = input.color(color.rgb(8, 153, 129), "Bull wick", inline = "wick", group = GROUP_STYLE)
color bearWick = input.color(color.rgb(242, 54, 69), "Bear wick", inline = "wick", group = GROUP_STYLE)
color borderColor = input.color(color.rgb(55, 65, 81), "Border", inline = "border", group = GROUP_STYLE)
int borderWidth = input.int(1, "Width", minval = 0, maxval = 4, inline = "border", group = GROUP_STYLE)
color labelColor = input.color(color.rgb(210, 214, 220), "Timeframe label", inline = "text", group = GROUP_STYLE)
// ─────────────────────────────────────────────────────────────────────────────
// Drawing-object storage and cleanup
// ─────────────────────────────────────────────────────────────────────────────
var array<box> bodies = array.new<box>()
var array<line> wicks = array.new<line>()
var array<label> tfLabels = array.new<label>()
clearDrawings() =>
    while array.size(bodies) > 0
        box.delete(array.pop(bodies))
    while array.size(wicks) > 0
        line.delete(array.pop(wicks))
    while array.size(tfLabels) > 0
        label.delete(array.pop(tfLabels))
// Returns the three candles in chronological order.
// With live mode enabled:  C1 = [2], C2 = [1], C3 = [0/live].
// With live mode disabled: C1 = [3], C2 = [2], C3 = [1/last closed].
getPO3(string tf) =>
    int s = includeLive ? 0 : 1
    request.security(
         syminfo.tickerid,
         tf,
         [open[s + 2], high[s + 2], low[s + 2], close[s + 2],
          open[s + 1], high[s + 1], low[s + 1], close[s + 1],
          open[s],     high[s],     low[s],     close[s]],
         gaps = barmerge.gaps_off,
         lookahead = barmerge.lookahead_off)
drawGroup(
     int slot,
     string tfName,
     float o1, float h1, float l1, float c1,
     float o2, float h2, float l2, float c2,
     float o3, float h3, float l3, float c3) =>
    int groupWidth = 3 * candleWidth + 2 * candleGap
    int groupLeft = bar_index + positionOffset + slot * (groupWidth + groupGap)
    array<float> opens = array.from(o1, o2, o3)
    array<float> highs = array.from(h1, h2, h3)
    array<float> lows = array.from(l1, l2, l3)
    array<float> closes = array.from(c1, c2, c3)
    for i = 0 to 2
        float o = array.get(opens, i)
        float h = array.get(highs, i)
        float l = array.get(lows, i)
        float c = array.get(closes, i)
        int left = groupLeft + i * (candleWidth + candleGap)
        int right = left + candleWidth
        int center = int(math.round((left + right) * 0.5))
        bool bullish = c >= o
        color bodyColor = bullish ? bullBody : bearBody
        color wickColor = bullish ? bullWick : bearWick
        if not na(o) and not na(h) and not na(l) and not na(c)
            array.push(wicks, line.new(center, h, center, l, xloc = xloc.bar_index, color = wickColor, width = 1))
            array.push(bodies, box.new(
                 left = left,
                 top = math.max(o, c),
                 right = right,
                 bottom = math.min(o, c),
                 xloc = xloc.bar_index,
                 border_color = borderColor,
                 border_width = borderWidth,
                 bgcolor = color.new(bodyColor, bodyTransparency)))
    float groupHigh = math.max(h1, math.max(h2, h3))
    float groupLow = math.min(l1, math.min(l2, l3))
    float groupRange = math.max(groupHigh - groupLow, syminfo.mintick * 10)
    float labelY = labelPosition == "Top" ? groupHigh + groupRange * labelPadding : groupLow - groupRange * labelPadding
    int labelX = groupLeft + int(math.round(groupWidth * 0.5))
    array.push(tfLabels, label.new(
         x = labelX,
         y = labelY,
         text = tfName,
         xloc = xloc.bar_index,
         yloc = yloc.price,
         color = color.new(color.black, 100),
         style = label.style_none,
         textcolor = labelColor,
         size = labelSize,
         textalign = text.align_center))
// ─────────────────────────────────────────────────────────────────────────────
// Data requests: five requests, 12 tuple elements each (well under Pine limits).
// ─────────────────────────────────────────────────────────────────────────────
[o5_1, h5_1, l5_1, c5_1, o5_2, h5_2, l5_2, c5_2, o5_3, h5_3, l5_3, c5_3] = getPO3("5")
[o15_1, h15_1, l15_1, c15_1, o15_2, h15_2, l15_2, c15_2, o15_3, h15_3, l15_3, c15_3] = getPO3("15")
[o30_1, h30_1, l30_1, c30_1, o30_2, h30_2, l30_2, c30_2, o30_3, h30_3, l30_3, c30_3] = getPO3("30")
[o1h_1, h1h_1, l1h_1, c1h_1, o1h_2, h1h_2, l1h_2, c1h_2, o1h_3, h1h_3, l1h_3, c1h_3] = getPO3("60")
[o4h_1, h4h_1, l4h_1, c4h_1, o4h_2, h4h_2, l4h_2, c4h_2, o4h_3, h4h_3, l4h_3, c4h_3] = getPO3("240")
// request.security() only returns one lower-timeframe bar per chart bar. Requiring
// a <= 5m chart prevents an inaccurate 5m PO3 when the chart itself is above 5m.
float chartSeconds = timeframe.in_seconds()
if barstate.isfirst and (na(chartSeconds) or chartSeconds > timeframe.in_seconds("5"))
    runtime.error("MTF PO3: use this indicator on a 5-minute or lower chart so the 5m PO3 remains complete and accurate.")
if barstate.islast
    clearDrawings()
    int slot = 0
    if show5m
        drawGroup(slot, "5m", o5_1, h5_1, l5_1, c5_1, o5_2, h5_2, l5_2, c5_2, o5_3, h5_3, l5_3, c5_3)
        slot += 1
    if show15m
        drawGroup(slot, "15m", o15_1, h15_1, l15_1, c15_1, o15_2, h15_2, l15_2, c15_2, o15_3, h15_3, l15_3, c15_3)
        slot += 1
    if show30m
        drawGroup(slot, "30m", o30_1, h30_1, l30_1, c30_1, o30_2, h30_2, l30_2, c30_2, o30_3, h30_3, l30_3, c30_3)
        slot += 1
    if show1h
        drawGroup(slot, "1H", o1h_1, h1h_1, l1h_1, c1h_1, o1h_2, h1h_2, l1h_2, c1h_2, o1h_3, h1h_3, l1h_3, c1h_3)
        slot += 1
    if show4h
        drawGroup(slot, "4H", o4h_1, h4h_1, l4h_1, c4h_1, o4h_2, h4h_2, l4h_2, c4h_2, o4h_3, h4h_3, l4h_3, c4h_3)

← 返回列表