SMC Fibonacci Navigator PRO

Fayezpq5566 · study · 482 行 · 点赞 10 · TradingView 原页

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

Pine Script

//@version=6
indicator("SMC Fibonacci Navigator PRO", overlay=true, max_lines_count=100, max_labels_count=100, max_boxes_count=30)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
pivotLen = input.int(5, "Pivot Length", minval=2, maxval=20)
obLookback = input.int(12, "OB Search Bars", minval=2, maxval=50)
 
rr = input.float(2.0, "Risk Reward Ratio", minval=1.0, step=0.5)
atrLen = input.int(14, "ATR Length", minval=1)
atrMult = input.float(1.5, "ATR Stop Multiplier", minval=0.1, step=0.1)
liqTolerance = input.float(0.15, "Liquidity Tolerance (ATR)", minval=0.01, step=0.01)
 
showStructure = input.bool(true, "Show BOS / MSS")
showOB = input.bool(true, "Show Order Blocks")
showFVG = input.bool(true, "Show FVG")
showLiquidity = input.bool(true, "Show Liquidity")
showFib = input.bool(true, "Show Fibonacci")
showLevels = input.bool(true, "Show Entry / SL / TP")
showSignals = input.bool(true, "Show Buy / Sell Signals")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SWING STRUCTURE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
atr = ta.atr(atrLen)
 
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
 
var float swingHigh = na
var float swingLow = na
 
var int swingHighBar = na
var int swingLowBar = na
 
var float breakHigh = na
var float breakLow = na
 
var int trend = 0
 
// Detect breaks against previously known levels.
bullBreak = barstate.isconfirmed and not na(breakHigh) and close > breakHigh and close[1] <= breakHigh
bearBreak = barstate.isconfirmed and not na(breakLow) and close < breakLow and close[1] >= breakLow
 
bullMSS = bullBreak and trend == -1
bearMSS = bearBreak and trend == 1
 
if bullBreak
    if showStructure
        label.new(
             bar_index,
             low,
             bullMSS ? "MSS ↑" : "BOS ↑",
             style=label.style_label_up,
             color=color.green,
             textcolor=color.white,
             size=size.tiny
         )
    trend := 1
 
if bearBreak
    if showStructure
        label.new(
             bar_index,
             high,
             bearMSS ? "MSS ↓" : "BOS ↓",
             style=label.style_label_down,
             color=color.red,
             textcolor=color.white,
             size=size.tiny
         )
    trend := -1
 
// Update swing levels after checking the break.
if not na(ph)
    swingHigh := ph
    swingHighBar := bar_index - pivotLen
    breakHigh := ph
 
if not na(pl)
    swingLow := pl
    swingLowBar := bar_index - pivotLen
    breakLow := pl
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LIQUIDITY: APPROXIMATELY EQUAL SWINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float prevPH = na
var float prevPL = na
 
var int prevPHBar = na
var int prevPLBar = na
 
if not na(ph)
    if showLiquidity and not na(prevPH) and math.abs(ph - prevPH) <= atr[pivotLen] * liqTolerance
        line.new(
             prevPHBar,
             prevPH,
             bar_index - pivotLen,
             ph,
             color=color.orange,
             style=line.style_dashed,
             width=2
         )
 
        label.new(
             bar_index - pivotLen,
             ph,
             "BSL",
             style=label.style_label_down,
             color=color.orange,
             textcolor=color.white,
             size=size.tiny
         )
 
    prevPH := ph
    prevPHBar := bar_index - pivotLen
 
if not na(pl)
    if showLiquidity and not na(prevPL) and math.abs(pl - prevPL) <= atr[pivotLen] * liqTolerance
        line.new(
             prevPLBar,
             prevPL,
             bar_index - pivotLen,
             pl,
             color=color.aqua,
             style=line.style_dashed,
             width=2
         )
 
        label.new(
             bar_index - pivotLen,
             pl,
             "SSL",
             style=label.style_label_up,
             color=color.aqua,
             textcolor=color.black,
             size=size.tiny
         )
 
    prevPL := pl
    prevPLBar := bar_index - pivotLen
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ORDER BLOCKS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var box bullOB = na
var box bearOB = na
 
if bullBreak and showOB
    int found = na
 
    for i = 1 to obLookback
        if na(found) and close[i] < open[i]
            found := i
 
    if not na(found)
        if not na(bullOB)
            box.delete(bullOB)
 
        bullOB := box.new(
             bar_index - found,
             high[found],
             bar_index + 20,
             low[found],
             bgcolor=color.new(color.green, 85),
             border_color=color.green,
             text="Bull OB",
             text_color=color.green
         )
 
if bearBreak and showOB
    int found = na
 
    for i = 1 to obLookback
        if na(found) and close[i] > open[i]
            found := i
 
    if not na(found)
        if not na(bearOB)
            box.delete(bearOB)
 
        bearOB := box.new(
             bar_index - found,
             high[found],
             bar_index + 20,
             low[found],
             bgcolor=color.new(color.red, 85),
             border_color=color.red,
             text="Bear OB",
             text_color=color.red
         )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FAIR VALUE GAPS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var box bullFVG = na
var box bearFVG = na
 
bullGap = barstate.isconfirmed and bar_index >= 2 and low > high[2]
bearGap = barstate.isconfirmed and bar_index >= 2 and high < low[2]
 
if showFVG and bullGap
    if not na(bullFVG)
        box.delete(bullFVG)
 
    bullFVG := box.new(
         bar_index - 2,
         low,
         bar_index + 20,
         high[2],
         bgcolor=color.new(color.teal, 85),
         border_color=color.teal,
         text="Bull FVG",
         text_color=color.teal
     )
 
if showFVG and bearGap
    if not na(bearFVG)
        box.delete(bearFVG)
 
    bearFVG := box.new(
         bar_index - 2,
         low[2],
         bar_index + 20,
         high,
         bgcolor=color.new(color.fuchsia, 85),
         border_color=color.fuchsia,
         text="Bear FVG",
         text_color=color.fuchsia
     )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FIBONACCI FROM LATEST CONFIRMED SWING LEG
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
validLeg = not na(swingHigh) and not na(swingLow) and not na(swingHighBar) and not na(swingLowBar) and swingHigh > swingLow
 
bullLeg = validLeg and swingLowBar < swingHighBar
bearLeg = validLeg and swingHighBar < swingLowBar
 
fibRange = validLeg ? swingHigh - swingLow : na
 
f236 = bullLeg ? swingHigh - fibRange * 0.236 : bearLeg ? swingLow + fibRange * 0.236 : na
f382 = bullLeg ? swingHigh - fibRange * 0.382 : bearLeg ? swingLow + fibRange * 0.382 : na
f500 = bullLeg ? swingHigh - fibRange * 0.5 : bearLeg ? swingLow + fibRange * 0.5 : na
f618 = bullLeg ? swingHigh - fibRange * 0.618 : bearLeg ? swingLow + fibRange * 0.618 : na
f786 = bullLeg ? swingHigh - fibRange * 0.786 : bearLeg ? swingLow + fibRange * 0.786 : na
 
ext127 = bullLeg ? swingLow + fibRange * 1.272 : bearLeg ? swingHigh - fibRange * 1.272 : na
ext162 = bullLeg ? swingLow + fibRange * 1.618 : bearLeg ? swingHigh - fibRange * 1.618 : na
ext200 = bullLeg ? swingLow + fibRange * 2.0 : bearLeg ? swingHigh - fibRange * 2.0 : na
 
plot(showFib ? f236 : na, "Fib 23.6%", color=color.orange)
plot(showFib ? f382 : na, "Fib 38.2%", color=color.green)
plot(showFib ? f500 : na, "Fib 50%", color=color.blue, linewidth=2)
plot(showFib ? f618 : na, "Fib 61.8%", color=color.orange, linewidth=2)
plot(showFib ? f786 : na, "Fib 78.6%", color=color.purple, linewidth=2)
 
plot(showFib ? ext127 : na, "Fib Extension 127.2%", color=color.aqua)
plot(showFib ? ext162 : na, "Fib Extension 161.8%", color=color.fuchsia)
plot(showFib ? ext200 : na, "Fib Extension 200%", color=color.gray)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// FIXED TRADE LEVELS
// 0 = flat, 1 = long, -1 = short
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var int tradeState = 0
var int entryBar = na
 
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na
 
var line entryLine = na
var line stopLine = na
var line targetLine = na
 
// Entry is the signal candle close.
// Stop uses the last confirmed swing or ATR fallback.
 
buySignal = bullBreak and tradeState == 0 and not na(atr)
sellSignal = bearBreak and tradeState == 0 and not na(atr)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUY SETUP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if buySignal
    float candidateStop = not na(swingLow) and swingLow < close ? swingLow : close - atr * atrMult
    float risk = close - candidateStop
 
    if risk > syminfo.mintick
        tradeState := 1
        entryBar := bar_index
        entryPrice := close
        stopPrice := candidateStop
        targetPrice := entryPrice + risk * rr
 
        if showSignals
            label.new(
                 bar_index,
                 low,
                 "BUY\nEntry: " + str.tostring(entryPrice, format.mintick) +
                 "\nSL: " + str.tostring(stopPrice, format.mintick) +
                 "\nTP: " + str.tostring(targetPrice, format.mintick),
                 style=label.style_label_up,
                 color=color.green,
                 textcolor=color.white,
                 size=size.small
             )
 
        if showLevels
            entryLine := line.new(
                 bar_index, entryPrice,
                 bar_index + 1, entryPrice,
                 extend=extend.right,
                 color=color.blue,
                 width=2
             )
 
            stopLine := line.new(
                 bar_index, stopPrice,
                 bar_index + 1, stopPrice,
                 extend=extend.right,
                 color=color.red,
                 width=2
             )
 
            targetLine := line.new(
                 bar_index, targetPrice,
                 bar_index + 1, targetPrice,
                 extend=extend.right,
                 color=color.green,
                 width=2
             )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// SELL SETUP
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if sellSignal
    float candidateStop = not na(swingHigh) and swingHigh > close ? swingHigh : close + atr * atrMult
    float risk = candidateStop - close
 
    if risk > syminfo.mintick
        tradeState := -1
        entryBar := bar_index
        entryPrice := close
        stopPrice := candidateStop
        targetPrice := entryPrice - risk * rr
 
        if showSignals
            label.new(
                 bar_index,
                 high,
                 "SELL\nEntry: " + str.tostring(entryPrice, format.mintick) +
                 "\nSL: " + str.tostring(stopPrice, format.mintick) +
                 "\nTP: " + str.tostring(targetPrice, format.mintick),
                 style=label.style_label_down,
                 color=color.red,
                 textcolor=color.white,
                 size=size.small
             )
 
        if showLevels
            entryLine := line.new(
                 bar_index, entryPrice,
                 bar_index + 1, entryPrice,
                 extend=extend.right,
                 color=color.blue,
                 width=2
             )
 
            stopLine := line.new(
                 bar_index, stopPrice,
                 bar_index + 1, stopPrice,
                 extend=extend.right,
                 color=color.red,
                 width=2
             )
 
            targetLine := line.new(
                 bar_index, targetPrice,
                 bar_index + 1, targetPrice,
                 extend=extend.right,
                 color=color.green,
                 width=2
             )
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// EXIT CHECKS: STOP-FIRST RULE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
longStopHit = tradeState == 1 and bar_index > entryBar and low <= stopPrice
longTargetHit = tradeState == 1 and bar_index > entryBar and high >= targetPrice
 
shortStopHit = tradeState == -1 and bar_index > entryBar and high >= stopPrice
shortTargetHit = tradeState == -1 and bar_index > entryBar and low <= targetPrice
 
exitStop = barstate.isconfirmed and (longStopHit or shortStopHit)
exitTarget = barstate.isconfirmed and not exitStop and (longTargetHit or shortTargetHit)
 
if exitStop or exitTarget
    string resultText = exitStop ? "INVALIDATED / SL" : "TARGET HIT"
    color resultColor = exitStop ? color.red : color.green
    float resultY = tradeState == 1 ? high : low
 
    label.new(
         bar_index,
         resultY,
         resultText,
         style=tradeState == 1 ? label.style_label_down : label.style_label_up,
         color=resultColor,
         textcolor=color.white,
         size=size.small
     )
 
    if not na(entryLine)
        line.set_x2(entryLine, bar_index)
        line.set_extend(entryLine, extend.none)
 
    if not na(stopLine)
        line.set_x2(stopLine, bar_index)
        line.set_extend(stopLine, extend.none)
 
    if not na(targetLine)
        line.set_x2(targetLine, bar_index)
        line.set_extend(targetLine, extend.none)
 
    tradeState := 0
    entryBar := na
    entryPrice := na
    stopPrice := na
    targetPrice := na
 
    entryLine := na
    stopLine := na
    targetLine := na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ACTIVE TRADE LEVELS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plot(showLevels and tradeState != 0 ? entryPrice : na, "Active Entry", color=color.blue, style=plot.style_linebr)
plot(showLevels and tradeState != 0 ? stopPrice : na, "Active Stop", color=color.red, style=plot.style_linebr)
plot(showLevels and tradeState != 0 ? targetPrice : na, "Active Target", color=color.green, style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(
     buySignal and not na(swingLow),
     "BUY Signal",
     "Confirmed BUY signal on {{ticker}}. Check the chart for fixed entry, stop and target."
 )
 
alertcondition(
     sellSignal and not na(swingHigh),
     "SELL Signal",
     "Confirmed SELL signal on {{ticker}}. Check the chart for fixed entry, stop and target."
 )
 
alertcondition(
     exitStop,
     "Trade Invalidated / SL",
     "Stop-loss or invalidation level reached on {{ticker}}."
 )
 
alertcondition(
     exitTarget,
     "Target Reached",
     "Target level reached on {{ticker}}."
 )

← 返回列表