Professional HA Range Filter - 0.80Δ

Trader_Monk8 · study · 119 行 · 点赞 66 · TradingView 原页

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

Pine Script

//@version=6
indicator(title = 'Professional HA Range Filter - 0.80Δ', shorttitle = 'PRO_HA_RF', overlay = true)
 
// ==========================================
// 1. INPUTS & CONFIGURATION
// ==========================================
per = input.int(70, minval = 1, title = 'Sampling Period')
mult = input.float(2.5, minval = 0.1, title = 'Range Multiplier')
tp_pts = input.float(30.0, title = 'Take Profit (Points)')
sl_pts = input.float(30.0, title = 'Stop Loss (Points)')
delta = 0.80
 
// ==========================================
// 2. HEIKIN ASHI LOGIC
// ==========================================
// We calculate HA values internally so the indicator reacts to smoothed trends
haTicker = ticker.heikinashi(syminfo.tickerid)
haClose = request.security(haTicker, timeframe.period, close)
haOpen = request.security(haTicker, timeframe.period, open)
haHigh = request.security(haTicker, timeframe.period, high)
haLow = request.security(haTicker, timeframe.period, low)
 
// Source for the filter is now Heikin Ashi Close
haSrc = haClose
 
// ==========================================
// 3. CORE RANGE FILTER CALCULATIONS
// ==========================================
smoothrng(x, t, m) =>
    wper = t * 2 - 1
    avrng = ta.ema(math.abs(x - x[1]), t)
    ta.ema(avrng, wper) * m
 
smrng = smoothrng(haSrc, per, mult)
 
rngfilt(x, r) =>
    var float rf = na
    rf := x > nz(rf[1]) ? x - r < nz(rf[1]) ? nz(rf[1]) : x - r : x + r > nz(rf[1]) ? nz(rf[1]) : x + r
    rf
 
filt = rngfilt(haSrc, smrng)
 
// Directional Movement
upward = 0.0
upward := filt > filt[1] ? nz(upward[1]) + 1 : filt < filt[1] ? 0 : nz(upward[1])
downward = 0.0
downward := filt < filt[1] ? nz(downward[1]) + 1 : filt > filt[1] ? 0 : nz(downward[1])
 
// ==========================================
// 4. SIGNAL & TRADE TRACKING
// ==========================================
// Signals based on Heikin Ashi crossing the filter
longSignal = ta.crossover(haSrc, filt) and upward > 0
shortSignal = ta.crossunder(haSrc, filt) and downward > 0
 
var float entryPrice = na
var float targetPrice = na
var float slPrice = na
var string tradeType = 'None'
 
// Using standard 'close' for entry to reflect real-world execution
if longSignal
    entryPrice := close
    targetPrice := close + tp_pts
    slPrice := close - sl_pts
    tradeType := 'Long'
    tradeType
else if shortSignal
    entryPrice := close
    targetPrice := close - tp_pts
    slPrice := close + sl_pts
    tradeType := 'Short'
    tradeType
 
// Real-time PnL Tracking
float currentPnL = tradeType == 'Long' ? close - entryPrice : tradeType == 'Short' ? entryPrice - close : 0
 
// ==========================================
// 5. PROFESSIONAL DASHBOARD
// ==========================================
var table dash = table.new(position.top_right, 2, 5, bgcolor = color.new(color.black, 15), border_width = 2, border_color = color.gray)
 
if barstate.islast
    // Header
    table.cell(dash, 0, 0, 'SIGNAL STATUS', text_color = color.white, text_size = size.small)
    table.cell(dash, 1, 0, tradeType, bgcolor = tradeType == 'Long' ? color.green : tradeType == 'Short' ? color.yellow : color.gray, text_color = color.white)
 
    // Underlying Points
    table.cell(dash, 0, 1, 'Points PnL', text_color = color.white, text_halign = text.align_left)
    table.cell(dash, 1, 1, str.tostring(currentPnL, '#.#'), text_color = currentPnL >= 0 ? color.lime : color.red)
 
    // Option Delta Tracking
    table.cell(dash, 0, 2, 'Option 0.8Δ PnL', text_color = #90bff9, text_halign = text.align_left)
    table.cell(dash, 1, 2, str.tostring(currentPnL * delta, '#.#'), text_color = currentPnL >= 0 ? color.lime : color.red)
 
    // Target and SL Info
    table.cell(dash, 0, 3, 'Goal (+30)', text_color = color.white, text_halign = text.align_left)
    table.cell(dash, 1, 3, str.tostring(targetPrice, '#.#'), text_color = color.lime)
    table.cell(dash, 0, 4, 'Stop (-30)', text_color = color.white, text_halign = text.align_left)
    table.cell(dash, 1, 4, str.tostring(slPrice, '#.#'), text_color = color.red)
 
// ==========================================
// 6. VISUALS & PLOTTING
// ==========================================
// Trend Ribbon
plot(filt, color = upward > 0 ? color.lime : downward > 0 ? color.yellow : color.gray, linewidth = 3, title = 'HA Filter Path')
 
// Big Box Signals as requested
// BUY: Light Green Box | SELL: Yellow Box
plotshape(longSignal, style = shape.labelup, location = location.belowbar, color = color.new(color.lime, 10), text = 'BUY', textcolor = color.white, size = size.large)
plotshape(shortSignal, style = shape.labeldown, location = location.abovebar, color = color.new(color.yellow, 10), text = 'SELL', textcolor = color.white, size = size.large)
 
// Target & SL Lines (Dynamic)
plot(targetPrice, title = 'TP Line', color = color.new(color.lime, 40), style = plot.style_linebr, linewidth = 2)
plot(slPrice, title = 'SL Line', color = color.new(color.red, 40), style = plot.style_linebr, linewidth = 2)
 
// Fill between filter and price for trend visualization
fillColor = upward > 0 ? color.new(color.lime, 90) : downward > 0 ? color.new(color.yellow, 90) : na
fill(plot(filt), plot(haSrc), color = fillColor)

← 返回列表