Major Previous ATHs and Wick Lows

sweat_shorts · study · 107 行 · 点赞 10 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sweat_shorts
 
//@version=6
indicator("Major Previous ATHs", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// Inputs
showHighs    = input.bool(true, "Show Previous ATHs", group = "Previous All Time Highs")
minDays    = input.int(5, "Trading Days Without a New ATH", minval = 1, group = "Previous All Time Highs", tooltip = "An all-time high only counts once price has gone this many trading days without exceeding it.")
minSepPct  = input.float(2, "Min Separation Between ATHs (%)", minval = 0, step = 0.5, group = "Previous All Time Highs", tooltip = "When two ATHs are closer than this, only the higher one is kept.")
maxDistPct = input.float(12.0, "Max Distance From Current Price (%)", minval = 0, step = 0.5, group = "Previous All Time Highs", tooltip = "Only ATHs within this distance of the current price are drawn.")
lineColor  = input.color(#D6A62E, "Line Color", group = "Previous All Time Highs")
lineWidth  = input.int(1, "Line Width", minval = 1, maxval = 4, group = "Previous All Time Highs")
labelOffset = input.int(25, "Label Offset (candles)", minval = 0, maxval = 500, group = "Previous All Time Highs")
textSizeInput = input.string("Normal", "Text Size", options = ["Tiny", "Small", "Normal", "Large"])
 
showLows    = input.bool(true, "Show Wick Lows", group = "Weekly Wick Lows")
leftWeeks   = input.int(4, "Lowest Low of Prior Weeks", minval = 1, group = "Weekly Wick Lows", tooltip = "The low must be the lowest in this many weeks before it.")
holdWeeks   = input.int(4, "Weeks Without Returning", minval = 1, group = "Weekly Wick Lows", tooltip = "The low must not be broken for this many weeks after it. A new level appears once this time has passed.")
minWickPct  = input.float(25.0, "Min Lower Wick (% of Candle)", minval = 0, maxval = 100, group = "Weekly Wick Lows")
bounceWeeks = input.int(4, "Bounce Within (weeks)", minval = 0, group = "Weekly Wick Lows", tooltip = "0 only counts the bounce within the wick's own week.")
bounceAtr   = input.float(1.5, "Min Bounce (x weekly ATR)", minval = 0, step = 0.1, group = "Weekly Wick Lows", tooltip = "How far price must rise from the low, measured in 14-week ATRs.")
lowSepPct   = input.float(2, "Min Separation Between Lows (%)", minval = 0, step = 0.5, group = "Weekly Wick Lows", tooltip = "When two wick lows are closer than this, only the lower one is kept.")
lowDistPct  = input.float(12.0, "Max Distance From Current Price (%)", minval = 0, step = 0.5, group = "Weekly Wick Lows")
lowColor    = input.color(#83A993, "Line Color", group = "Weekly Wick Lows")
 
getTextSize(string selectedSize) =>
    switch selectedSize
        "Tiny"   => size.tiny
        "Normal" => size.normal
        "Large"  => size.large
        => size.small
 
// Returns every ATH that went minDays without being exceeded, in ascending price order
confirmedAths(int days) =>
    var levels = array.new<float>()
    var times = array.new<int>()
    var float ath = na
    var int athTime = na
    var int athBar = na
    var bool pending = false
    if pending and bar_index - athBar >= days
        levels.push(ath)
        times.push(athTime)
        pending := false
    if na(ath) or high > ath
        ath := high
        athTime := time
        athBar := bar_index
        pending := true
    [levels, times]
 
// Returns every weekly candle with a long lower wick that was a swing low, bounced hard and wasn't revisited
majorWickLows() =>
    var levels = array.new<float>()
    var times = array.new<int>()
    candleLow = low[holdWeeks]
    candleRange = high[holdWeeks] - candleLow
    wick = math.min(open[holdWeeks], close[holdWeeks]) - candleLow
    swingLow = ta.lowest(low, leftWeeks + holdWeeks + 1)
    bounceBars = math.min(bounceWeeks, holdWeeks)
    bounceHigh = ta.highest(high, bounceBars + 1)[holdWeeks - bounceBars]
    candleAtr = ta.atr(14)[holdWeeks]
    if candleLow == swingLow and candleRange > 0 and wick / candleRange * 100 >= minWickPct and bounceHigh - candleLow >= bounceAtr * candleAtr
        levels.push(candleLow)
        times.push(time[holdWeeks])
    [levels, times]
 
// Always use regular-session daily bars, so neither the chart's timeframe nor after-hours prices can change the ATHs
regularTicker = ticker.modify(syminfo.tickerid, session = session.regular)
[athLevels, athTimes] = request.security(regularTicker, "D", confirmedAths(minDays))
[lowLevels, lowTimes] = request.security(regularTicker, "W", majorWickLows())
 
// Drawing
var lines = array.new<line>()
var labels = array.new<label>()
 
if barstate.islast
    for l in lines
        l.delete()
    lines.clear()
    for l in labels
        l.delete()
    labels.clear()
 
    if showHighs and not na(athLevels) and athLevels.size() > 0
        float lastKept = na
        // Walk from the highest ATH down, dropping any within minSepPct of the last one kept
        for i = athLevels.size() - 1 to 0
            level = athLevels.get(i)
            if na(lastKept) or (lastKept - level) / lastKept * 100 >= minSepPct
                lastKept := level
                if math.abs(level - close) / close * 100 <= maxDistPct
                    lines.push(line.new(athTimes.get(i), level, time, level, xloc = xloc.bar_time, extend = extend.right, color = lineColor, style = line.style_solid, width = lineWidth))
                    labels.push(label.new(bar_index + labelOffset, level, "previous ATH", xloc = xloc.bar_index, yloc = yloc.price, style = label.style_none, textcolor = lineColor, textalign = text.align_right, size = getTextSize(textSizeInput)))
 
    if showLows and not na(lowLevels) and lowLevels.size() > 0
        float lastKept = na
        // Walk from the lowest low up, dropping any within lowSepPct of the last one kept
        for i in lowLevels.sort_indices(order.ascending)
            level = lowLevels.get(i)
            if na(lastKept) or (level - lastKept) / level * 100 >= lowSepPct
                lastKept := level
                if math.abs(level - close) / close * 100 <= lowDistPct
                    lines.push(line.new(lowTimes.get(i), level, time, level, xloc = xloc.bar_time, extend = extend.right, color = lowColor, style = line.style_solid, width = lineWidth))
                    // Sits below the line so it can't overlap a previous ATH label at nearly the same price
                    labels.push(label.new(bar_index + labelOffset, level, "wick low", xloc = xloc.bar_index, yloc = yloc.price, style = label.style_label_up, color = color.new(lowColor, 100), textcolor = lowColor, textalign = text.align_right, size = getTextSize(textSizeInput)))

← 返回列表