[ad] HTF

Jogartt · study · 123 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("[ad] HTF", overlay = true, max_boxes_count = 10, max_lines_count = 30)
 
const string DISPLAY = "Right-side candles"
const string STYLE = "Appearance"
 
string periodInput = input.string("Daily", "Candle timeframe", options = ["H4", "Daily", "3 Days", "Weekly", "Monthly"], group = DISPLAY)
int candleCount = input.int(3, "Candle count", minval = 2, maxval = 10, group = DISPLAY,
     tooltip = "Includes the current candle. The rightmost candle updates until it closes.")
int chartGap = input.int(36, "Offset from chart", minval = 2, maxval = 240, group = DISPLAY,
     tooltip = "Gap from the last chart bar, measured in chart bars. If candles are clipped, increase the chart's right margin.")
int candleWidth = input.int(6, "Candle width", minval = 2, maxval = 16, step = 2, group = STYLE,
     tooltip = "Candle body width, measured in chart bars.")
color bullColor = input.color(#257C6B, "Bullish body", group = STYLE)
color bearColor = input.color(#AD4D54, "Bearish body", group = STYLE)
bool separateDetails = input.bool(false, "Separate border and wick colors", group = STYLE)
color bullBorder = input.color(#257C6B, "Bullish border", group = STYLE, active = separateDetails)
color bearBorder = input.color(#AD4D54, "Bearish border", group = STYLE, active = separateDetails)
color bullWick = input.color(#257C6B, "Bullish wick", group = STYLE, active = separateDetails)
color bearWick = input.color(#AD4D54, "Bearish wick", group = STYLE, active = separateDetails)
 
string htf = switch periodInput
    "H4" => "240"
    "3 Days" => "3D"
    "Weekly" => "1W"
    "Monthly" => "1M"
    => "1D"
 
type CandleDrawing
    box body
    line upperWick
    line lowerWick
    line doji
 
method update(CandleDrawing drawing, int left, int width, float o, float h, float l, float c,
     color fillColor, color borderColor, color wickColor) =>
    int right = left + width
    int center = left + int(width / 2)
    float top = math.max(o, c)
    float bottom = math.min(o, c)
    bool flat = o == c
    box.set_lefttop(drawing.body, left, flat ? na : top)
    box.set_rightbottom(drawing.body, right, flat ? na : bottom)
    box.set_border_color(drawing.body, borderColor)
    box.set_bgcolor(drawing.body, fillColor)
    line.set_xy1(drawing.upperWick, center, h)
    line.set_xy2(drawing.upperWick, center, top)
    line.set_color(drawing.upperWick, wickColor)
    line.set_xy1(drawing.lowerWick, center, bottom)
    line.set_xy2(drawing.lowerWick, center, l)
    line.set_color(drawing.lowerWick, wickColor)
    line.set_xy1(drawing.doji, left, flat ? o : na)
    line.set_xy2(drawing.doji, right, flat ? o : na)
    line.set_color(drawing.doji, borderColor)
 
bool showCandles = chart.is_standard and timeframe.isminutes and timeframe.multiplier <= 240 and
     timeframe.in_seconds() < timeframe.in_seconds(htf)
 
if showCandles
    // Track a partial candle in case the HTF feed still returns the previous period.
    int periodStart = time(htf)
    var float partialOpen = na
    var float partialHigh = na
    var float partialLow = na
    if na(partialOpen) or periodStart != periodStart[1]
        partialOpen := open
        partialHigh := high
        partialLow := low
    else
        partialHigh := math.max(partialHigh, high)
        partialLow := math.min(partialLow, low)
 
    // Fetch native HTF candles, including on custom intervals such as M24.
    [feedStart, o0, h0, l0, c0,
         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] = request.security(syminfo.tickerid, htf,
         [time, open, high, low, close,
         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]], lookahead = barmerge.lookahead_off)
 
    var array<CandleDrawing> drawings = array.new<CandleDrawing>()
    if barstate.isfirst
        for i = 1 to candleCount
            drawings.push(CandleDrawing.new(box.new(na, na, na, na),
                 line.new(na, na, na, na), line.new(na, na, na, na), line.new(na, na, na, na)))
 
    if barstate.islast
        // Use the fallback only for the current period. Closed candles come from the HTF feed.
        bool usePartial = not na(periodStart) and (na(feedStart) or feedStart < periodStart)
        array<float> opens = array.from(o0, o1, o2, o3, o4, o5, o6, o7, o8, o9)
        array<float> highs = array.from(h0, h1, h2, h3, h4, h5, h6, h7, h8, h9)
        array<float> lows = array.from(l0, l1, l2, l3, l4, l5, l6, l7, l8, l9)
        array<float> closes = array.from(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9)
        int slot = 0
        for age = candleCount - 1 to 0
            int sourceAge = age - (usePartial ? 1 : 0)
            bool isPartial = sourceAge < 0
            float o = isPartial ? partialOpen : opens.get(sourceAge)
            float h = isPartial ? partialHigh : highs.get(sourceAge)
            float l = isPartial ? partialLow : lows.get(sourceAge)
            float c = isPartial ? close : closes.get(sourceAge)
            if not na(o) and not na(h) and not na(l) and not na(c)
                int left = bar_index + chartGap + slot * (candleWidth + 4)
                bool bullish = c >= o
                color bodyColor = bullish ? bullColor : bearColor
                color borderColor = separateDetails ? (bullish ? bullBorder : bearBorder) : bodyColor
                color wickColor = separateDetails ? (bullish ? bullWick : bearWick) : bodyColor
                drawings.get(slot).update(left, candleWidth, o, h, l, c, bodyColor, borderColor, wickColor)
                slot += 1

← 返回列表