Advanced Intraday Darvas Box (Noise Filtered PRO)

darshakssc · study · 156 行 · 点赞 140 · TradingView 原页

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

Pine Script

//@version=6
indicator("Advanced Intraday Darvas Box (Noise Filtered PRO)", overlay = true, max_lines_count = 500, max_labels_count = 200)
 
// ==============================================================================
// ADVANCED FEATURES (ALL NOISE-FILTERED):
// - Strong pivot detection (configurable)
// - Minimum box height filter (clears tiny/noise boxes)
// - Trend filter (EMA/VWAP selectable)
// - Volume confirmation filter
// - Fake breakout filter (breakout candle must close strongly beyond box)
// - Limited boxes shown (chart stays clean)
// - Non-repainting (confirmed pivots only)
// ==============================================================================
 
//-------------------------------------------
// USER INPUTS
//-------------------------------------------
 
left  = input.int(3, "Left Pivot Bars", minval=1)
right = input.int(3, "Right Pivot Bars", minval=1)
 
maxBoxes = input.int(10, "Max Boxes to Display", minval=1)
 
minBoxATRMult = input.float(0.5, "Minimum Box Height (ATR Multiplier)", step=0.1,
     tooltip="Filters out tiny/noise boxes. Higher = cleaner signals.")
 
useVolumeFilter = input.bool(true, "Enable Volume Filter")
volLen = input.int(20, "Volume MA Length")
volMult = input.float(1.2, "Volume Threshold Multiplier")
 
confirmBreakClose = input.float(0.15, "Breakout Close Strength (%)", step=0.05,
  tooltip="Breakout candle must close 0.X% beyond box to avoid false breakouts")
 
// Trend filter
trendSource = input.string("EMA", "Trend Filter", options=["EMA","VWAP","SMA","None"])
trendLen = input.int(50, "Trend Length", minval=5)
 
//-------------------------------------------
// TREND FILTER LOGIC
//-------------------------------------------
emaTrend = ta.ema(close, trendLen)
smaTrend = ta.sma(close, trendLen)
vwapTrend = ta.vwap(close)
 
var float trendValue = na
trendValue := trendSource == "EMA" ? emaTrend :
              trendSource == "SMA" ? smaTrend :
              trendSource == "VWAP" ? vwapTrend :
              na
 
isBullTrend = trendSource == "None" or close > trendValue
isBearTrend = trendSource == "None" or close < trendValue
 
//-------------------------------------------
// PIVOTS (NON-REPAINTING)
//-------------------------------------------
ph = ta.pivothigh(high, left, right)
pl = ta.pivotlow(low,  left, right)
 
var float lastHigh = na
var float lastLow  = na
var int   lastHighBar = na
var int   lastLowBar  = na
 
if not na(ph)
    lastHigh     := ph
    lastHighBar  := bar_index - right
 
if not na(pl)
    lastLow      := pl
    lastLowBar   := bar_index - right
 
//-------------------------------------------
// DARVAS BOX VALIDATION (STRONG FILTERS)
//-------------------------------------------
 
// ATR filter for minimum box height
atr = ta.atr(14)
validBoxHeight = (lastHigh - lastLow) > (atr * minBoxATRMult)
 
// Volume filter
volMA  = ta.sma(volume, volLen)
volOK  = not useVolumeFilter or (volume > volMA * volMult)
 
// Confirm pivot sequence (High first → Low)
pivotOK = lastLowBar > lastHighBar
 
newBox = pivotOK and validBoxHeight and volOK
 
//-------------------------------------------
// BOX STORAGE (ONLY KEEP FEW BOXES)
//-------------------------------------------
var float[] boxHighs = array.new_float()
var float[] boxLows  = array.new_float()
var int[] boxStarts  = array.new_int()
var box[] boxObjects = array.new_box()
 
var float activeHigh = na
var float activeLow  = na
var int   activeStart = na
var box activeBox = na
 
//-------------------------------------------
// CREATE A NEW BOX (CLEAN, FILTERED)
//-------------------------------------------
if newBox
    activeHigh  := lastHigh
    activeLow   := lastLow
    activeStart := lastHighBar
 
    if not na(activeBox)
        box.delete(activeBox)
 
    activeBox := box.new(activeStart, activeHigh, bar_index,   activeLow, xloc = xloc.bar_index, bgcolor = color.new(color.blue, 85), border_color = color.new(color.blue, 10))
 
    array.unshift(boxHighs, activeHigh)
    array.unshift(boxLows,  activeLow)
    array.unshift(boxStarts,activeStart)
    array.unshift(boxObjects, activeBox)
 
    // Limit boxes
    if array.size(boxHighs) > maxBoxes
        box oldBox = array.pop(boxObjects)
        if not na(oldBox)
            box.delete(oldBox)
        array.pop(boxHighs)
        array.pop(boxLows)
        array.pop(boxStarts)
 
// Extend active box to current bar
if not na(activeBox)
    box.set_right(activeBox, bar_index)
    box.set_top(activeBox, activeHigh)
    box.set_bottom(activeBox, activeLow)
 
//-------------------------------------------
// VERY CLEAN BREAKOUT DETECTION
//-------------------------------------------
 
// Breakout must:
// 1. Break box edge
// 2. Close beyond edge by confirmBreakClose %
// 3. Respect trend direction
// 4. No noise pivots (filtered above)
strongUp = close > activeHigh * (1 + confirmBreakClose/100) and close[1] <= activeHigh and isBullTrend
strongDown = close < activeLow * (1 - confirmBreakClose/100) and close[1] >= activeLow and isBearTrend
 
if strongUp
    label.new(bar_index, high, "Strong Breakout ↑", style=label.style_label_up, color=color.new(color.green,0), textcolor=color.white, size=size.tiny)
 
if strongDown
    label.new(bar_index, low, "Strong Breakdown ↓", style=label.style_label_down, color=color.new(color.red,0), textcolor=color.white, size=size.tiny)
 
// Alerts
alertcondition(strongUp, "Strong Bullish Breakout", "Strong Darvas Bullish Breakout")
alertcondition(strongDown, "Strong Bearish Breakdown", "Strong Darvas Bearish Breakdown")

← 返回列表