RSI-Divergence Goggles [Trendoscope®]

Trendoscope · study · 188 行 · 点赞 2,672 · TradingView 原页

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

Pine Script

// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Trendoscope
//                                       ░▒             
//                                  ▒▒▒   ▒▒      
//                              ▒▒▒▒▒     ▒▒      
//                      ▒▒▒▒▒▒▒░     ▒     ▒▒          
//                  ▒▒▒▒▒▒           ▒     ▒▒          
//             ▓▒▒▒       ▒        ▒▒▒▒▒▒▒▒▒▒▒  
//   ▒▒▒▒▒▒▒▒▒▒▒ ▒        ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒         
//   ▒  ▒       ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░        
//   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒         
//   ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒                       
//    ▒▒▒▒▒         ▒▒▒▒▒▒▒                            
//                 ▒▒▒▒▒▒▒▒▒                           
//                ▒▒▒▒▒ ▒▒▒▒▒                          
//               ░▒▒▒▒   ▒▒▒▒▓      ████████╗██████╗ ███████╗███╗   ██╗██████╗  ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
//              ▓▒▒▒▒     ▒▒▒▒      ╚══██╔══╝██╔══██╗██╔════╝████╗  ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
//              ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        ██║   ██████╔╝█████╗  ██╔██╗ ██║██║  ██║██║   ██║███████╗██║     ██║   ██║██████╔╝█████╗ 
//             ▒▒▒▒▒       ▒▒▒▒▒       ██║   ██╔══██╗██╔══╝  ██║╚██╗██║██║  ██║██║   ██║╚════██║██║     ██║   ██║██╔═══╝ ██╔══╝  
//            ▒▒▒▒▒         ▒▒▒▒▒      ██║   ██║  ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║     ███████╗
//             ▒▒             ▒                        
//@version=5
import HeWhoMustNotBeNamed/DrawingTypes/2 as dr
import HeWhoMustNotBeNamed/DrawingMethods/2
import HeWhoMustNotBeNamed/ZigzagTypes/5 as zg
import HeWhoMustNotBeNamed/ZigzagMethods/6
import HeWhoMustNotBeNamed/ta/1 as eta
indicator("RSI-Divergence Goggles [Trendoscope®]", 'RDG[Trendoscope®]', overlay=true, max_lines_count = 500, max_labels_count = 500)
 
t1 = input.time(0, 'Corner 1', inline='Corner1', confirm=true, group='Widget', display = display.none)
p1 = input.price(0, '', inline='Corner1', confirm=true, group='Widget', display = display.none)
 
t2 = input.time(0, 'Corner 2', inline='Corner2', confirm=true, group='Widget', display = display.none)
p2 = input.price(0, '', inline='Corner2', confirm=true, group='Widget', display = display.none)
 
d1 = chart.point.from_time(t1, p1)
d2 = chart.point.from_time(t2, p2)
 
var bx = box.new(d1, d2, chart.fg_color, 2, line.style_solid, extend.none, xloc.bar_time, color.new(color.yellow, 90))
var lblEdge1 = label.new(d1, '', xloc.bar_time, color=color.purple, style = label.style_circle, size=size.tiny)
var lblEdge2 = label.new(d2, '', xloc.bar_time, color=color.purple, style = label.style_circle, size=size.tiny)
maxPrice = math.max(p1, p2)
minPrice = math.min(p1, p2)
 
maxTime = math.max(t1, t2)
minTime = math.min(t1, t2)
 
var minBarIndex = 0
var maxBarIndex = 0
 
if(minBarIndex == 0 and time >= minTime)
    minBarIndex := bar_index
 
if(time <= maxTime)
    maxBarIndex := bar_index
 
length = input.int(14, 'Length',  group = 'RSI', tooltip = 'RSI Length', display = display.none)
rsiColor = color.blue
 
maType = input.string('sma', 'Moving Average', ['sma', 'ema', 'rma', 'hma', 'wma', 'vwma'], inline='ma', group='Trend', display = display.none)
maLength = input.int(200, '', minval=5, step=50, inline='ma', group='Trend', display = display.none, tooltip='Moving Average to identify trend. Direction of moving average between the divergence pivots identify trend')
 
zigzagLength = input.int(13, 'Length',  group = 'Zigzag', tooltip = 'Zigzag Length', display = display.none)
showLabel = input.bool(false, 'Show Pivot Labels', group = 'Zigzag',  tooltip = 'Show RSI/Price Values', display = display.none)
showZigzag = input.bool(false, 'Display Zigzag',group = 'Zigzag',  tooltip = 'Display RSI/Price Zigzag', display = display.none)
zigzagColor = input.color(color.yellow, '', group = 'Zigzag', inline='c', display = display.none)
textColor = input.color(color.black, '', group = 'Zigzag', inline='c', tooltip = 'Zigzag line and label color', display = display.none)
 
type ZigzagDrawing
    array<label> pivotLabels
    array<line> divergenceLines
    array<chart.point> pricePoints
    array<chart.point> rsiPoints
    polyline priceLine
    polyline rsiLine
 
type ZigzagProperties
    color lineColor = color.blue
    int lineWidth = 1
    string lineStyle = line.style_solid
    bool showLabel = false
    bool showZigzag = false
    color textColor = color.black
    int maxObjects = 300
    string xloc = xloc.bar_time
 
method flush(ZigzagDrawing this)=>
    while this.pivotLabels.size() > 0
        this.pivotLabels.pop().delete()
    while this.divergenceLines.size() > 0
        this.divergenceLines.pop().delete()
    this.pricePoints.clear()
    this.rsiPoints.clear()
    this.priceLine.delete()
    this.rsiLine.delete()
 
method draw(ZigzagDrawing this, ZigzagProperties properties)=>
    if(properties.showZigzag)
        this.priceLine := polyline.new(this.pricePoints, false, false, properties.xloc, properties.lineColor)
        this.rsiLine := polyline.new(this.rsiPoints, false, false, properties.xloc, properties.lineColor)
 
getColor(dir, ratio)=>dir > 0? (ratio > 1 ? color.green : color.orange) : (ratio > 1 ? color.red : color.lime)
getText(dir, ratio)=>dir > 0? (ratio > 1 ? 'HH' : 'LH') : (ratio > 1 ? 'LL' : 'HL')
method draw(zg.Zigzag this, ZigzagProperties properties, int minBarIndex, int maxBarIndex)=>
    if(barstate.islast)
        var drawing = ZigzagDrawing.new(array.new<label>(), array.new<line>(), array.new<chart.point>(), array.new<chart.point>())
        drawing.flush()
        for [index, pivot] in this.zigzagPivots
            if(pivot.point.bar >= minBarIndex and pivot.point.bar <=maxBarIndex)
                price = pivot.indicatorValues.get(0)
                rsi = pivot.indicatorValues.get(1)
                ma = pivot.indicatorValues.get(2)
                rsiPlot = pivot.point.price
                priceRatio = pivot.indicatorRatios.get(0)
                rsiRatio = pivot.indicatorRatios.get(1)
                pricePoint = chart.point.new(pivot.point.bartime, pivot.point.bar, price)
                rsiPoint = chart.point.new(pivot.point.bartime, pivot.point.bar, rsiPlot)
                drawing.pricePoints.push(pricePoint)
                drawing.rsiPoints.push(rsiPoint)
 
                priceLblColor = getColor(pivot.dir, priceRatio)
                priceLblText = getText(pivot.dir, priceRatio)
                lblStyle = pivot.dir > 0? label.style_label_down : label.style_label_up
                priceTooltip = 'Price : '+str.tostring(price) + '('+str.tostring(priceRatio)+')'
                priceLblSize = size.tiny
 
                rsiLblColor = getColor(pivot.dir, rsiRatio)
                rsiLblText = getText(pivot.dir, rsiRatio)
                rsiTooltip = 'RSI : '+str.tostring(rsi) + '('+str.tostring(rsiRatio)+')'
                rsiLblSize = size.tiny
 
                divergence = 0
                if(priceLblColor != rsiLblColor)
                    lastPivot = this.zigzagPivots.get(index+2)
                    if(lastPivot.point.bar >= minBarIndex and lastPivot.point.bar <=maxBarIndex)
                        lastPrice = lastPivot.indicatorValues.get(0)
                        lastMa = lastPivot.indicatorValues.get(2)
                        lastRsiPlot = lastPivot.point.price
                        lastPricePoint = chart.point.new(lastPivot.point.bartime, lastPivot.point.bar, lastPrice)
                        lastRsiPoint = chart.point.new(lastPivot.point.bartime, lastPivot.point.bar, lastRsiPlot)
                        trend = math.sign(ma-lastMa)
                        dir = math.sign(lastPivot.dir)
                        sentiment = math.sign(rsiRatio - priceRatio)
 
                        divergence := trend == dir and sentiment < 0? 1 : trend != dir and sentiment > 0 ? -1 : 0
                        if(divergence != 0)
                            divergenceText = (dir > 0? 'Bearish' : 'Bullish')+(divergence>0?'Divergence':'Hidden Divergence')
                            divergenceColor = dir > 0? color.red : color.green
                            priceDivergenceLine = line.new(pricePoint, lastPricePoint, properties.xloc, extend.none, divergenceColor)
                            rsiDivergenceLine = line.new(rsiPoint, lastRsiPoint, properties.xloc, extend.none, divergenceColor)
                            drawing.divergenceLines.push(priceDivergenceLine)
                            drawing.divergenceLines.push(rsiDivergenceLine)
                            priceLblSize := size.small
                            rsiLblSize := size.small
                            rsiLblText := divergence >0 ? 'D' : 'H'
                            priceLblText := rsiLblText
                            priceLblColor := divergence >0 ? (dir > 0? color.orange : color.lime) : (dir > 0? color.red : color.green)
                            rsiLblColor := priceLblColor
 
                if(properties.showLabel or divergence != 0)
                    pricePivotLabel = label.new(pricePoint, priceLblText, properties.xloc, yloc.price,
                                     priceLblColor, lblStyle, properties.textColor, size.tiny, tooltip=priceTooltip)
                    drawing.pivotLabels.push(pricePivotLabel)
 
                    rsiPivotLabel = label.new(rsiPoint, rsiLblText, properties.xloc, yloc.price,
                                     rsiLblColor, lblStyle, properties.textColor, size.tiny, tooltip=rsiTooltip)
 
                    drawing.pivotLabels.push(rsiPivotLabel)
        drawing.draw(properties)
 
rsi = ta.rsi(close, length)
ma = eta.ma(close, maType, maLength)
adjustedRsi = minPrice + rsi*(maxPrice - minPrice)/100
 
plot(time >= minTime and time <=maxTime? adjustedRsi : na, "RSI", color = rsiColor, display = display.pane)
 
indicators = matrix.new<float>()
indicatorNames = array.from('Price', 'RSI', maType+str.tostring(maLength))
 
indicators.add_row(0, array.from(close, close, close))
indicators.add_row(1, array.from(rsi, rsi, rsi))
indicators.add_row(2, array.from(ma, ma, ma))
 
var zg.Zigzag zigzag = zg.Zigzag.new(zigzagLength, 300, 0)
var ZigzagProperties properties = ZigzagProperties.new(zigzagColor, 1, showLabel = showLabel, showZigzag = showZigzag, textColor = textColor)
zigzag.calculate(array.from(adjustedRsi), indicators, indicatorNames)
 
zigzag.draw(properties, minBarIndex, maxBarIndex)

← 返回列表