Valid Pullbacks and Trend

kpt_gonzo · study · 199 行 · 点赞 768 · 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/
// © kpt_gonzo
 
//@version=5
indicator("Valid Pullbacks and Trend", "VP&T", overlay = true, max_labels_count = 500, max_lines_count = 500)
 
type pivotType
    float price
    int timestamp
 
method copy(pivotType this, pivotType from) =>
    this.price := from.price
    this.timestamp := from.timestamp
 
method invalid(pivotType this) =>
    na(this.price)
 
method valid(pivotType this) =>
    not na(this.price)
 
method invalidate(pivotType this) =>
    this.price := na
 
method set(pivotType this, float price, int timestamp = time) =>
    this.price := price
    this.timestamp := timestamp
 
method setHigher(pivotType this, pivotType pivot) =>
    if this.invalid() or pivot.price > this.price
        this.copy(pivot)
 
method setLower(pivotType this, pivotType pivot) =>
    if this.invalid() or pivot.price < this.price
        this.copy(pivot)
 
type pivotsType
    label[] labels
    bool show
    color highColor
    color lowColor
    string style
    string size
    int maxCount
 
method add(pivotsType this, pivotType pivot, bool dir) =>
    if this.show and this.maxCount > 0
        labelText = switch this.style
            "text" => dir ? "H" : "L"
            => ""
        textColor = dir ? this.highColor : this.lowColor
        labelStyle = switch this.style
            "triangle" => dir ? label.style_triangleup : label.style_triangledown
            "dot" => label.style_circle
            => dir ? label.style_label_down : label.style_label_up
        labelColor = switch this.style
            "text" => color.new(color.black, 100)
            => dir ? this.highColor : this.lowColor
        labelLoc = switch this.style
            "triangle" => dir ? yloc.abovebar : yloc.belowbar
            => yloc.price
        lab = label.new(pivot.timestamp, pivot.price, labelText, xloc = xloc.bar_time, yloc = labelLoc, color = labelColor, style = labelStyle, textcolor = textColor, size = this.size, force_overlay = true)
        array.push(this.labels, lab)
        if array.size(this.labels) > this.maxCount
            lab := array.shift(this.labels)
            lab.delete()
 
method addHigh(pivotsType this, pivotType pivot) =>
    this.add(pivot, true)
 
method addLow(pivotsType this, pivotType pivot) =>
    this.add(pivot, false)
 
type trendType
    chart.point[] points
    polyline line
    bool show
    color color
    string style
    int width
 
method add(trendType this, pivotType pivot) =>
    if this.show
        this.points.push(chart.point.from_time(pivot.timestamp, pivot.price))
 
method draw(trendType this) =>
    if this.show
        polyline.delete(this.line)
        style = switch this.style
            "solid" => line.style_solid
            "dotted" => line.style_dotted
            "dashed" => line.style_dashed
        this.line := polyline.new(this.points, xloc = xloc.bar_time, line_color = this.color, line_style = style, line_width = this.width, force_overlay = true)
 
var pivots = pivotsType.new(array.new<label>())
var extTrend = trendType.new(array.new<chart.point>())
var intTrend = trendType.new(array.new<chart.point>())
 
const string GRP_PIVOTS = "Pivots"
 
pivots.show := input.bool(true, "", group = GRP_PIVOTS, inline = "PIVOT_LABEL")
pivots.highColor := input.color(color.new(color.red, 50), "High", group = GRP_PIVOTS, inline = "PIVOT_LABEL")
pivots.lowColor := input.color(color.new(color.green, 50), "Low", group = GRP_PIVOTS, inline = "PIVOT_LABEL")
pivots.style := input.string("triangle", "Style", ["text", "triangle", "dot"], group = GRP_PIVOTS, inline = "PIVOT_LABEL")
pivots.size := input.string(size.auto, "Size", [size.auto, size.tiny, size.small, size.normal, size.large, size.huge], group = GRP_PIVOTS, inline = "PIVOT_LABEL")
pivots.maxCount := input.int(100, "Maximum visible", minval = 0, maxval = 500, group = GRP_PIVOTS)
 
const string GRP_TREND = "Trend"
 
extTrend.show := input.bool(true, "External", group = GRP_TREND, inline = "TREND_EXT")
extTrend.color := input.color(color.silver, "", group = GRP_TREND, inline = "TREND_EXT")
extTrend.style := input.string("solid", "Style", ["solid", "dotted", "dashed"], group = GRP_TREND, inline = "TREND_EXT")
extTrend.width := input.int(1, "Width", minval = 1, group = GRP_TREND, inline = "TREND_EXT")
intTrend.show := input.bool(true, "Internal", group = GRP_TREND, inline = "TREND_INT")
intTrend.color := input.color(color.gray, "", group = GRP_TREND, inline = "TREND_INT")
intTrend.style := input.string("dotted", "Style", ["solid", "dotted", "dashed"], group = GRP_TREND, inline = "TREND_INT")
intTrend.width := input.int(1, "Width", minval = 1, group = GRP_TREND, inline = "TREND_INT")
 
find() =>
    var bool dir = true
    var pivotType h = pivotType.new(high, time)
    var pivotType l = pivotType.new(low, time)
    pivotType newh = pivotType.new()
    pivotType newl = pivotType.new()
 
    if dir
        if low < l.price
            if high > h.price
                h.set(high)
            newh.copy(h)
            dir := false
            h.set(high)
            l.set(low)
        else if high > h.price
            l.set(low)
            h.set(high)
    else
        if high > h.price
            if low < l.price
                l.set(low)
            newl.copy(l)   
            dir := true    
            h.set(high)
            l.set(low)
        else if low < l.price
            h.set(high)
            l.set(low)
    [newh, newl]
 
var bool trend = na
var pivotType eh = pivotType.new()
var pivotType el = pivotType.new()
var pivotType ih = pivotType.new()
var pivotType il = pivotType.new()
 
[h, l] = find()
 
if eh.valid() and high > eh.price
    if il.valid()
        el.copy(il)
        il.invalidate()
        extTrend.add(el)
    eh.invalidate()
    ih.invalidate()
 
if el.valid() and low < el.price
    if ih.valid()
        eh.copy(ih)
        ih.invalidate()
        extTrend.add(eh)
    el.invalidate()
    il.invalidate()
 
if h.valid()
    pivots.addHigh(h)
    intTrend.add(h)
 
    if el.invalid() and il.valid()
        el.copy(il)
        ih.copy(h)
        extTrend.add(el)
    
if l.valid()
    pivots.addLow(l)
    intTrend.add(l)
 
    if eh.invalid() and ih.valid()
        eh.copy(ih)
        il.copy(l)
        extTrend.add(eh)
 
if h.valid()
    ih.setHigher(h)
 
if l.valid()
    il.setLower(l)
 
if barstate.islast
    intTrend.draw()
    extTrend.draw()

← 返回列表