Narrow Range Bar Breakout Tracker (NR-N)

cfdtools · study · 65 行 · 点赞 4 · TradingView 原页

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

Pine Script

//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0
// © cfdtools
//
// Narrow Range Bar Breakout Tracker (NR-N)
// Finds the bar with the narrowest high-low range of the last N bars (NR4/NR7 style),
// draws its high and low, and marks when price closes beyond them.
 
indicator("Narrow Range Bar Breakout Tracker (NR-N)", shorttitle = "NR-N Tracker", overlay = true, max_lines_count = 100, max_labels_count = 100)
 
lookback  = input.int(7, "Narrow range lookback (N bars)", minval = 2, maxval = 50)
extendMax = input.int(20, "Max bars to keep levels", minval = 1, maxval = 200)
showBreak = input.bool(true, "Mark closes beyond the levels")
upCol     = input.color(color.new(color.teal, 0), "Upper level color")
dnCol     = input.color(color.new(color.red, 0), "Lower level color")
nrCol     = input.color(color.new(color.orange, 80), "Narrow bar highlight")
 
rng  = high - low
isNR = rng == ta.lowest(rng, lookback) and bar_index >= lookback
 
var line  upLine   = na
var line  dnLine   = na
var int   startBar = na
var bool  active   = false
var float upLvl    = na
var float dnLvl    = na
 
bgcolor(isNR ? nrCol : na)
 
deleteOld = isNR and not na(upLine)
expire    = active and not isNR and (bar_index - startBar > extendMax)
keepExt   = active and not isNR and (bar_index - startBar <= extendMax)
 
if deleteOld
    line.delete(upLine)
    line.delete(dnLine)
 
if isNR
    upLvl    := high
    dnLvl    := low
    startBar := bar_index
    upLine   := line.new(bar_index, upLvl, bar_index + 1, upLvl, color = upCol, width = 1, extend = extend.none)
    dnLine   := line.new(bar_index, dnLvl, bar_index + 1, dnLvl, color = dnCol, width = 1, extend = extend.none)
    active   := true
 
if expire
    active := false
 
if keepExt
    line.set_x2(upLine, bar_index)
    line.set_x2(dnLine, bar_index)
 
upBreak = active and not isNR and close > upLvl
dnBreak = active and not isNR and close < dnLvl
 
if upBreak and showBreak
    label.new(bar_index, low, "▲", style = label.style_label_up, color = upCol, textcolor = color.white, size = size.tiny)
    active := false
 
if dnBreak and showBreak
    label.new(bar_index, high, "▼", style = label.style_label_down, color = dnCol, textcolor = color.white, size = size.tiny)
    active := false
 
alertcondition(upBreak, "Close above narrow bar high", "Price closed above the narrow range bar high")
alertcondition(dnBreak, "Close below narrow bar low", "Price closed below the narrow range bar low")

← 返回列表