Breakouts & Pullbacks [Trendoscope®]

Trendoscope · study · 226 行 · 点赞 2,719 · 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 Pty Ltd, Trendoscope®
//                                       ░▒             
//                                  ▒▒▒   ▒▒      
//                              ▒▒▒▒▒     ▒▒      
//                      ▒▒▒▒▒▒▒░     ▒     ▒▒          
//                  ▒▒▒▒▒▒           ▒     ▒▒          
//             ▓▒▒▒       ▒        ▒▒▒▒▒▒▒▒▒▒▒  
//   ▒▒▒▒▒▒▒▒▒▒▒ ▒        ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒         
//   ▒  ▒       ░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░        
//   ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▒         
//   ▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ ▒▒                       
//    ▒▒▒▒▒         ▒▒▒▒▒▒▒                            
//                 ▒▒▒▒▒▒▒▒▒                           
//                ▒▒▒▒▒ ▒▒▒▒▒                          
//               ░▒▒▒▒   ▒▒▒▒▓      ████████╗██████╗ ███████╗███╗   ██╗██████╗  ██████╗ ███████╗ ██████╗ ██████╗ ██████╗ ███████╗
//              ▓▒▒▒▒     ▒▒▒▒      ╚══██╔══╝██╔══██╗██╔════╝████╗  ██║██╔══██╗██╔═══██╗██╔════╝██╔════╝██╔═══██╗██╔══██╗██╔════╝
//              ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒        ██║   ██████╔╝█████╗  ██╔██╗ ██║██║  ██║██║   ██║███████╗██║     ██║   ██║██████╔╝█████╗ 
//             ▒▒▒▒▒       ▒▒▒▒▒       ██║   ██╔══██╗██╔══╝  ██║╚██╗██║██║  ██║██║   ██║╚════██║██║     ██║   ██║██╔═══╝ ██╔══╝  
//            ▒▒▒▒▒         ▒▒▒▒▒      ██║   ██║  ██║███████╗██║ ╚████║██████╔╝╚██████╔╝███████║╚██████╗╚██████╔╝██║     ███████╗
//             ▒▒             ▒                        
//@version=6
indicator("Breakouts & Pullbacks [Trendoscope®]", 'B&P[Trendoscope®]', overlay = true)
import Trendoscope/Drawing/4 as dr
import Trendoscope/oota/1 as eta
import Trendoscope/arrays/2 as pa
minimumGap = input.int(30, 'Minimum Gap', minval=10, step=10, tooltip='Minimum gap in terms of bar between break of new ATH', display=display.none)
 
type ATHData
    float pullbackPrice
    float pullbackPercent
    int pullbackBars
    int recoveryBars
    int span
    int distanceFromLast
    float priceFromLast
    float percentFromLast
    float runup
    float runupPercent
    int runupBars
    float totalRunup
    float totalRunupPercent
    int totalRunupBars
    
 
type ATHBreakout
    chart.point start
    chart.point end
    chart.point lowest
    chart.point last
    chart.point highest
    ATHData data
    polyline triangle
    label lbl
 
method getArrays(array<ATHBreakout> breakouts)=>
    array<float> pullbackPercents = array.new<float>()
    array<int> pullbackBars = array.new<int>()
    array<int> recoveryBars = array.new<int>()
    array<float> runUpPercents = array.new<float>()
    array<int> runupBars = array.new<int>()
    array<float> totalRunUpPercents = array.new<float>()
    array<int> totalRunupBars = array.new<int>()
    for breakout in breakouts
        pullbackPercents.push(breakout.data.pullbackPercent)
        pullbackBars.push(breakout.data.pullbackBars)
        recoveryBars.push(breakout.data.recoveryBars)
        runUpPercents.push(breakout.data.runupPercent)
        runupBars.push(breakout.data.runupBars)
        totalRunUpPercents.push(breakout.data.totalRunupPercent)
        totalRunupBars.push(breakout.data.totalRunupBars)
    [pullbackPercents, pullbackBars, recoveryBars, runUpPercents, runupBars, totalRunUpPercents, totalRunupBars]
 
 
method getTooltip(ATHData this)=>
    str.format('Runup(Before) : {3} ( {4} ) / {5}\nPullback : {0} ( {1} ) / {2}\nRecovery : {9} bars\nRunup(After) : {6} ( {7} ) / {8}',
         this.pullbackPrice, str.tostring(this.pullbackPercent, format.percent), this.pullbackBars,
         this.priceFromLast, str.tostring(this.percentFromLast, format.percent), this.distanceFromLast,
         this.runup, str.tostring(this.runupPercent, format.percent), this.runupBars, this.recoveryBars)
 
method updateData(ATHBreakout this)=>
    if(not na(this.highest))
        this.data.runup := this.highest.price - this.end.price
        this.data.runupPercent := this.data.runup*100 / this.end.price
        this.data.runupBars := this.highest.index - this.end.index
        this.data.totalRunup := this.highest.price - this.lowest.price
        this.data.totalRunupPercent := this.data.totalRunup*100 / this.lowest.price
        this.data.totalRunupBars := this.highest.index - this.lowest.index
 
method draw(ATHBreakout this)=>
    this.updateData()
    trianglepoints = array.from(this.end, this.start, this.lowest, this.end, this.highest)
    this.triangle.delete()
    this.triangle := polyline.new(trianglepoints, false, false, xloc.bar_time, color.yellow, force_overlay = true)
    this.lbl.delete()
    this.lbl := label.new(this.lowest, '', xloc.bar_time, yloc.belowbar, color.green, label.style_square, size=size.small, tooltip = this.data.getTooltip(), force_overlay = true)
 
method populateData(ATHBreakout this)=>
    this.data := ATHData.new()
    this.data.pullbackPrice := this.start.price - this.lowest.price
    this.data.pullbackPercent := this.data.pullbackPrice*100/this.start.price
    this.data.pullbackBars := this.lowest.index - this.start.index
    this.data.recoveryBars := this.end.index - this.lowest.index
    this.data.span := this.end.index - this.start.index
    if(not na(this.last))
        this.data.distanceFromLast := this.start.index - this.last.index
        this.data.priceFromLast := this.start.price - this.last.price
        this.data.percentFromLast := this.data.priceFromLast*100 / this.last.price
    if(not na(this.highest))
        this.data.runup := this.highest.price - this.end.price
        this.data.runupPercent := this.data.runup*100 / this.end.price
        this.data.runupBars := this.highest.index - this.end.index
        this.data.totalRunup := this.highest.price - this.lowest.price
        this.data.totalRunupPercent := this.data.totalRunup*100 / this.lowest.price
        this.data.totalRunupBars := this.highest.index - this.lowest.index
    this
 
var ath = chart.point.now(high)
var chart.point lastAthBreakBar = na
var int lastGap = na
 
var localLow = chart.point.now(low)
 
var array<ATHBreakout> breakouts = array.new<ATHBreakout>()
 
gap = high > ath.price? bar_index - ath.index : 0
ath := high > ath.price? chart.point.now(high) : ath
 
localLow := low <= localLow.price? chart.point.now(low): localLow
var ATHBreakout lastbreakout = na
if(gap >= minimumGap)
    lastAthBreakBar := ath
    lastGap := gap
    lastAth = ath[1]
    currentAth = chart.point.new(ath.time, ath.index, lastAth.price)
    lastbreakout := ATHBreakout.new(lastAth, currentAth, localLow, breakouts.size()>0?breakouts.last().end: na).populateData()
    breakouts.push(lastbreakout)
 
if(gap > 0)
    localLow := chart.point.now(low)
 
if(not na(lastbreakout))
    lastbreakout.highest := ath
    lastbreakout.draw()
 
barsSincebreak = na(lastAthBreakBar)? na : bar_index-lastAthBreakBar.index
 
plot(lastGap, 'Gap', color.red, display=display.data_window)
plot(barsSincebreak, 'Time Since Last Break', color.blue, display = display.data_window)
 
 
[pullbackPercents, pullbackBars, recoveryBars, runUpPercents, runupBars, totalRunupPercents, totalRunupBars] = breakouts.getArrays()
 
lookForEntry = false
lookForExit = false
if not na(lastbreakout)
    currentPullback = lastbreakout.highest.price-low
    currentPullbackPercent = currentPullback*100/lastbreakout.highest.price
    currentPullbackBars = bar_index - lastbreakout.highest.index
 
    currentRunupPercent = high < lastbreakout.end.price ? 0.0 : lastbreakout.data.runupPercent
    currentRunupBars = high < lastbreakout.end.price? 0 : lastbreakout.data.runupBars
 
    currentTotalRunupPercent = high < lastbreakout.end.price ? (high - localLow.price)*100 / localLow.price : lastbreakout.data.totalRunupPercent
    currentTotalRunupBars = high < lastbreakout.end.price ? bar_index - localLow.index : lastbreakout.data.totalRunupBars
 
    pullbackPercents.push(currentPullbackPercent)
    pullbackBars.push(currentPullbackBars)
 
    if(high < lastbreakout.end.price)
        runUpPercents.push(currentRunupPercent)
        runupBars.push(currentRunupBars)
 
        totalRunupPercents.push(currentTotalRunupPercent)
        totalRunupBars.push(currentTotalRunupBars)
 
    if(barstate.islast)
        var tbl = table.new(position.top_right, 5, 10, frame_color = chart.fg_color, frame_width = 1, border_width = 1, force_overlay = true)
        tbl.cell(0, 0, 'Factor', text_color = color.white, bgcolor=color.new(color.maroon, 70))
        tbl.cell(1, 0, 'Median', text_color = color.white, bgcolor=color.new(color.maroon, 70))
        tbl.cell(2, 0, 'Max', text_color = color.white, bgcolor=color.new(color.maroon, 70))
        tbl.cell(3, 0, 'Current', text_color = color.white, bgcolor=color.new(color.maroon, 70))
        tbl.cell(4, 0, 'PercentRank', text_color = color.white, bgcolor=color.new(color.maroon, 70))
 
        r1Color = color.new(color.from_gradient(pullbackPercents.percentrank(pullbackPercents.size()-1), 0, 100, color.red, color.green), 70)
        r2Color = color.new(color.from_gradient(pullbackBars.percentrank(pullbackBars.size()-1), 0, 100, color.red, color.green), 70)
        r3Color = color.new(color.from_gradient(runUpPercents.percentrank(runUpPercents.size()-1), 0, 100, color.green, color.red), 70)
        r4Color = color.new(color.from_gradient(runupBars.percentrank(runupBars.size()-1), 0, 100, color.green, color.red), 70)
        r5Color = color.new(color.from_gradient(totalRunupPercents.percentrank(totalRunupPercents.size()-1), 0, 100, color.green, color.red), 70)
        r6Color = color.new(color.from_gradient(totalRunupBars.percentrank(totalRunupBars.size()-1), 0, 100, color.green, color.red), 70)
 
 
        tbl.cell(0, 1, 'Pullback', text_color = chart.fg_color, bgcolor = r1Color)
        tbl.cell(0, 2, 'Pullback Bars', text_color = chart.fg_color, bgcolor = r2Color)
        tbl.cell(0, 3, 'Runup', text_color = chart.fg_color, bgcolor = r3Color)
        tbl.cell(0, 4, 'Runup Bars', text_color = chart.fg_color, bgcolor = r4Color)
        tbl.cell(0, 5, 'Total Runup', text_color = chart.fg_color, bgcolor = r5Color)
        tbl.cell(0, 6, 'Total Runup Bars', text_color = chart.fg_color, bgcolor = r6Color)
 
        tbl.cell(1, 1, str.tostring(pullbackPercents.median(), format.percent), text_color = chart.fg_color, bgcolor = r1Color)
        tbl.cell(1, 2, str.tostring(pullbackBars.median()), text_color = chart.fg_color, bgcolor = r2Color)
        tbl.cell(1, 3, str.tostring(runUpPercents.median(), format.percent), text_color = chart.fg_color, bgcolor = r3Color)
        tbl.cell(1, 4, str.tostring(runupBars.median()), text_color = chart.fg_color, bgcolor = r4Color)
        tbl.cell(1, 5, str.tostring(totalRunupPercents.median(), format.percent), text_color = chart.fg_color, bgcolor = r5Color)
        tbl.cell(1, 6, str.tostring(totalRunupBars.median()), text_color = chart.fg_color, bgcolor = r6Color)
 
        tbl.cell(2, 1, str.tostring(pullbackPercents.max(), format.percent), text_color = chart.fg_color, bgcolor = r1Color)
        tbl.cell(2, 2, str.tostring(pullbackBars.max()), text_color = chart.fg_color, bgcolor = r2Color)
        tbl.cell(2, 3, str.tostring(runUpPercents.max(), format.percent), text_color = chart.fg_color, bgcolor = r3Color)
        tbl.cell(2, 4, str.tostring(runupBars.max()), text_color = chart.fg_color, bgcolor = r4Color)
        tbl.cell(2, 5, str.tostring(totalRunupPercents.max(), format.percent), text_color = chart.fg_color, bgcolor = r5Color)
        tbl.cell(2, 6, str.tostring(totalRunupBars.max()), text_color = chart.fg_color, bgcolor = r6Color)
 
        tbl.cell(3, 1, str.tostring(currentPullbackPercent, format.percent), text_color = chart.fg_color, bgcolor = r1Color)
        tbl.cell(3, 2, str.tostring(currentPullbackBars), text_color = chart.fg_color, bgcolor = r2Color)
        tbl.cell(3, 3, str.tostring(currentRunupPercent, format.percent), text_color = chart.fg_color, bgcolor = r3Color)
        tbl.cell(3, 4, str.tostring(currentRunupBars), text_color = chart.fg_color, bgcolor = r4Color)
        tbl.cell(3, 5, str.tostring(currentTotalRunupPercent, format.percent), text_color = chart.fg_color, bgcolor = r5Color)
        tbl.cell(3, 6, str.tostring(lastbreakout.data.totalRunupBars), text_color = chart.fg_color, bgcolor = r6Color)
 
        tbl.cell(4, 1, str.tostring(pullbackPercents.percentrank(pullbackPercents.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r1Color)
        tbl.cell(4, 2, str.tostring(pullbackBars.percentrank(pullbackBars.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r2Color)
        tbl.cell(4, 3, str.tostring(runUpPercents.percentrank(runUpPercents.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r3Color)
        tbl.cell(4, 4, str.tostring(runupBars.percentrank(runupBars.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r4Color)
        tbl.cell(4, 5, str.tostring(totalRunupPercents.percentrank(totalRunupPercents.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r5Color)
        tbl.cell(4, 6, str.tostring(totalRunupBars.percentrank(totalRunupBars.size()-1), format.percent), text_color = chart.fg_color, bgcolor = r6Color)

← 返回列表