Khajanchi Smart Swing Engine [Auto-Fibs]

rishabfinances · study · 201 行 · 点赞 60 · TradingView 原页

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

Pine Script

//@version=6
indicator('Khajanchi Smart Swing Engine [Auto-Fibs]', shorttitle = 'KW Swing', overlay = true, max_lines_count = 100, max_labels_count = 50)
 
// =============================================================================
// SETTINGS TAB: ORGANIZED CONTROL PANELS
// =============================================================================
 
// --- 1. Moving Averages ---
GRP_MA        = "1. Institutional MAs (White Chart)"
showMAs       = input.bool(true, "Show 50 EMA / 200 SMA", group = GRP_MA)
lenFast       = input.int(50, "Fast EMA Length", minval = 1, group = GRP_MA)
lenSlow       = input.int(200, "Slow SMA Length", minval = 1, group = GRP_MA)
colFast       = input.color(color.new(#2962FF, 0), "50 EMA (Deep Blue)", group = GRP_MA)
colSlow       = input.color(color.new(#212121, 0), "200 SMA (Solid Black)", group = GRP_MA)
 
// --- 2. Dynamic ATR Swing Structure ---
GRP_SR        = "2. ATR Dynamic Swings (Active Only)"
showSR        = input.bool(true, "Show Current Swing Pivots", group = GRP_SR)
baseLookback  = input.int(10, "Base Swing Lookback", minval = 3, group = GRP_SR, tooltip = "Use 10 for 4H. Use 5 to 7 for Daily.")
colResist     = input.color(color.new(#D50000, 0), "Resistance (Deep Red)", group = GRP_SR)
colSupport    = input.color(color.new(#00C853, 0), "Support (Deep Green)", group = GRP_SR)
 
// --- 3. Fibonacci Retracement Zones ---
GRP_FIB       = "3. Filtered Fibonacci Retracements"
showFib       = input.bool(true, "Enable Fibonacci Zones", group = GRP_FIB)
colFib0       = input.color(color.new(#424242, 40), "0.0 / 1.0 (Dark Grey)", group = GRP_FIB)
colFib236     = input.color(color.new(#9E9E9E, 30), "0.236 (Grey)", group = GRP_FIB)
colFib382     = input.color(color.new(#00897B, 10), "0.382 (Teal)", group = GRP_FIB)
colFibPocket  = input.color(color.new(#FF8F00, 75), "0.5 - 0.618 (Amber Pocket)", group = GRP_FIB)
colFib786     = input.color(color.new(#1565C0, 20), "0.786 (Deep Blue)", group = GRP_FIB)
 
// --- 4. RSI Divergence Engine ---
GRP_RSI       = "4. RSI Divergence (Trend Filtered)"
showDiv       = input.bool(true, "Show RSI Divergence Signals", group = GRP_RSI)
 
// --- 5. Telemetry HUD ---
GRP_SIG       = "5. Signals & Dashboard"
showHUD       = input.bool(true, "Show Top-Right HUD Dashboard", group = GRP_SIG)
 
 
// =============================================================================
// 1. INSTITUTIONAL MAs & REGIME FILTER
// =============================================================================
ema50  = ta.ema(close, lenFast)
sma200 = ta.sma(close, lenSlow)
 
plot(showMAs ? ema50 : na,  "Fast EMA", color = colFast, linewidth = 2, style = plot.style_line)
plot(showMAs ? sma200 : na, "Slow SMA", color = colSlow, linewidth = 2, style = plot.style_line)
 
isUptrend   = close > sma200
isDowntrend = close < sma200
 
lowCrossUnder = ta.crossunder(low, ema50)
highCrossOver = ta.crossover(high, ema50)
pullbackBull = isUptrend and lowCrossUnder and close > ema50
pullbackBear = isDowntrend and highCrossOver and close < ema50
 
alertcondition(pullbackBull, "Bullish Pullback", "Price in uptrend touched the 50 EMA.")
alertcondition(pullbackBear, "Bearish Pullback", "Price in downtrend touched the 50 EMA.")
 
 
// =============================================================================
// 2. ATR DYNAMIC PIVOTS (Clean Chart Single Lines)
// =============================================================================
atr = ta.atr(14)
atrAvg = ta.sma(atr, 50)
volatilityRatio = atrAvg == 0 ? 1 : atr / atrAvg
dynLookback = math.max(3, math.round(baseLookback / volatilityRatio))
 
ph = ta.pivothigh(high, dynLookback, dynLookback)
pl = ta.pivotlow(low, dynLookback, dynLookback)
 
var float last_high = na
var float last_low  = na
var int   last_high_time = na
var int   last_low_time  = na
var line  resLine = na
var line  supLine = na
 
if not na(ph)
    last_high := ph
    last_high_time := time[dynLookback]
    if showSR
        if na(resLine)
            resLine := line.new(last_high_time, last_high, time, last_high, xloc=xloc.bar_time, color=colResist, width=2)
        else
            line.set_xy1(resLine, last_high_time, last_high)
            line.set_xy2(resLine, time, last_high)
            line.set_color(resLine, colResist)
 
if not na(pl)
    last_low := pl
    last_low_time := time[dynLookback]
    if showSR
        if na(supLine)
            supLine := line.new(last_low_time, last_low, time, last_low, xloc=xloc.bar_time, color=colSupport, width=2)
        else
            line.set_xy1(supLine, last_low_time, last_low)
            line.set_xy2(supLine, time, last_low)
            line.set_color(supLine, colSupport)
 
// Extend the active support/resistance lines to the right edge
if barstate.islast
    int extTime = time + (time - time[1]) * 10
    if not na(resLine) and showSR
        line.set_x2(resLine, extTime)
    if not na(supLine) and showSR
        line.set_x2(supLine, extTime)
 
 
// =============================================================================
// 3. FIBONACCI RETRACEMENT ENGINE (Color Filled Zones)
// =============================================================================
var line line_0   = na
var line line_236 = na
var line line_382 = na
var line line_500 = na
var line line_618 = na
var line line_786 = na
var line line_100 = na
var linefill fill_pocket = na
 
f_draw_line(ln, x1, x2, y, col) =>
    if na(ln)
        line.new(x1, y, x2, y, xloc=xloc.bar_time, color=col, style=line.style_dotted)
    else
        line.set_xy1(ln, x1, y)
        line.set_xy2(ln, x2, y)
        line.set_color(ln, col)
        ln
 
if showFib and barstate.islast and not na(last_high) and not na(last_low)
    if (isUptrend and last_high_time > last_low_time) or (isDowntrend and last_low_time > last_high_time)
        bool isBull = isUptrend
        
        float diff = last_high - last_low
        int startTime = math.min(last_high_time, last_low_time)
        int extTimeFibs = time + (time - time[1]) * 10
        
        float p0   = isBull ? last_high : last_low
        float p100 = isBull ? last_low  : last_high
        
        float l236 = isBull ? last_high - diff * 0.236 : last_low + diff * 0.236
        float l382 = isBull ? last_high - diff * 0.382 : last_low + diff * 0.382
        float l500 = isBull ? last_high - diff * 0.500 : last_low + diff * 0.500
        float l618 = isBull ? last_high - diff * 0.618 : last_low + diff * 0.618
        float l786 = isBull ? last_high - diff * 0.786 : last_low + diff * 0.786
 
        line_0   := f_draw_line(line_0, startTime, extTimeFibs, p0, colFib0)
        line_236 := f_draw_line(line_236, startTime, extTimeFibs, l236, colFib236)
        line_382 := f_draw_line(line_382, startTime, extTimeFibs, l382, colFib382)
        line_500 := f_draw_line(line_500, startTime, extTimeFibs, l500, color.new(colFibPocket, 0))
        line_618 := f_draw_line(line_618, startTime, extTimeFibs, l618, color.new(colFibPocket, 0))
        line_786 := f_draw_line(line_786, startTime, extTimeFibs, l786, colFib786)
        line_100 := f_draw_line(line_100, startTime, extTimeFibs, p100, colFib0)
        
        if na(fill_pocket)
            fill_pocket := linefill.new(line_500, line_618, colFibPocket)
        else
            linefill.set_color(fill_pocket, colFibPocket)
 
 
// =============================================================================
// 4. RSI (7) DIVERGENCE ENGINE
// =============================================================================
rsi7 = ta.rsi(close, 7)
 
rsi_ph = ta.pivothigh(rsi7, dynLookback, dynLookback)
rsi_pl = ta.pivotlow(rsi7, dynLookback, dynLookback)
 
prev_rsi_pl_val = ta.valuewhen(not na(rsi_pl), rsi7[dynLookback], 1)
prev_rsi_ph_val = ta.valuewhen(not na(rsi_ph), rsi7[dynLookback], 1)
 
bullDiv = isUptrend and not na(pl) and not na(rsi_pl) and low[dynLookback] < last_low[1] and rsi7[dynLookback] > prev_rsi_pl_val
plotshape(showDiv and bullDiv ? low[dynLookback] : na, "Bullish Divergence", shape.arrowup, location.belowbar, color.new(#00C853,0), size=size.small)
 
bearDiv = isDowntrend and not na(ph) and not na(rsi_ph) and high[dynLookback] > last_high[1] and rsi7[dynLookback] < prev_rsi_ph_val
plotshape(showDiv and bearDiv ? high[dynLookback] : na, "Bearish Divergence", shape.arrowdown, location.abovebar, color.new(#D50000,0), size=size.small)
 
 
// =============================================================================
// 5. TELEMETRY HUD
// =============================================================================
var table hud = table.new(position.top_right, 2, 4, bgcolor = color.new(color.white, 5), border_width = 1, border_color = color.new(#E0E0E0, 0))
 
if showHUD and barstate.islast
    bool isBull = close > sma200
    rsiStatus = rsi7 > 60 ? "Bull Power" : rsi7 < 40 ? "Bear Power" : "Neutral / Consolidation"
    rsiColor  = rsi7 > 60 ? color.new(#00C853,0) : rsi7 < 40 ? color.new(#D50000,0) : color.new(#757575,0)
 
    table.cell(hud, 0, 0, "Regime Bias", text_color=color.black, text_size=size.small, text_halign=text.align_left)
    table.cell(hud, 1, 0, isBull ? "BULLISH" : "BEARISH", text_color=isBull ? color.new(#00C853,0) : color.new(#D50000,0), text_size=size.small, text_halign=text.align_right)
 
    table.cell(hud, 0, 1, "RSI (7) State", text_color=color.black, text_size=size.small, text_halign=text.align_left)
    table.cell(hud, 1, 1, rsiStatus + " (" + str.tostring(rsi7, "#.0") + ")", text_color=rsiColor, text_size=size.small, text_halign=text.align_right)
 
    table.cell(hud, 0, 2, "Swing Lookback", text_color=color.black, text_size=size.small, text_halign=text.align_left)
    table.cell(hud, 1, 2, str.tostring(dynLookback) + " Bars (ATR Adj)", text_color=color.black, text_size=size.small, text_halign=text.align_right)
 
    table.cell(hud, 0, 3, "200 SMA Ref", text_color=color.black, text_size=size.small, text_halign=text.align_left)
    table.cell(hud, 1, 3, str.tostring(sma200, "#.##"), text_color=color.gray, text_size=size.small, text_halign=text.align_right)

← 返回列表