Alpha Indicator V6 Pro

mikandoinlove · study · 304 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("Alpha Indicator V6 Pro", shorttitle="Alpha V6 Pro", overlay=true)
 
//────────────────────────────────────────────────────────────────────
// INPUTS
//────────────────────────────────────────────────────────────────────
groupStyle  = "0. Trading Style"
styleIn     = input.string("Auto (by timeframe)", "Trading style", options=["Auto (by timeframe)", "Scalping", "Intraday", "Swing", "Custom"], group=groupStyle, tooltip="Auto = Scalping on 1-5m charts, Intraday on 15m-1H, Swing on 2H and above. Custom uses the manual values in the groups below. Scalping / Intraday / Swing override those manual values.")
 
groupTrend  = "1. Trend"
fastIn      = input.int(21, "Fast EMA (Custom style only)", minval=1, group=groupTrend)
slowIn      = input.int(55, "Slow EMA (Custom style only)", minval=2, group=groupTrend)
useHTF      = input.bool(false, "Use higher-timeframe trend filter", group=groupTrend)
htfAuto     = input.bool(true, "Auto-pick higher timeframe", group=groupTrend, tooltip="1-3m chart -> 15m, 5m -> 1H, 15m-1H -> 4H, 2H-4H -> Daily, Daily -> Weekly, Weekly -> Monthly")
htfManual   = input.timeframe("240", "Manual higher timeframe", group=groupTrend)
 
groupMom    = "2. Momentum & Volume"
rsiLen      = input.int(14, "RSI Length", minval=2, group=groupMom)
adxLen      = input.int(14, "ADX Length", minval=2, group=groupMom)
adxMinIn    = input.float(18.0, "Minimum ADX (Custom style only)", minval=1, step=0.5, group=groupMom)
volLen      = input.int(20, "Volume Average", minval=1, group=groupMom)
volMult     = input.float(1.15, "Strong Volume Multiplier", minval=0.1, step=0.05, group=groupMom)
useVolume   = input.bool(true, "Require volume confirmation (when volume exists)", group=groupMom)
 
groupSignal = "3. Signals"
breakIn     = input.int(20, "Breakout Lookback (Custom style only)", minval=2, group=groupSignal)
cooldownIn  = input.int(8, "Signal Cooldown Bars (Custom style only)", minval=0, group=groupSignal)
showEarly   = input.bool(true, "Show early trend signals", group=groupSignal)
showDip     = input.bool(true, "Show pullback signals", group=groupSignal)
useTrendExit = input.bool(true, "Exit on trend flip (turn off to hold until stop or target)", group=groupSignal)
 
groupRisk   = "4. Risk Management"
atrLen      = input.int(14, "ATR Length", minval=1, group=groupRisk)
stopIn      = input.float(1.8, "Stop Loss ATR Multiplier (Custom style only)", minval=0.1, step=0.1, group=groupRisk)
t1In        = input.float(2.0, "Target 1 ATR Multiplier (Custom style only)", minval=0.1, step=0.1, group=groupRisk)
t2In        = input.float(3.0, "Target 2 ATR Multiplier (Custom style only)", minval=0.1, step=0.1, group=groupRisk)
showLevels  = input.bool(true, "Show active stop and targets", group=groupRisk)
 
groupDisplay = "5. Display"
showDashboard = input.bool(true, "Show dashboard", group=groupDisplay)
showEMAs      = input.bool(true, "Show EMA lines", group=groupDisplay)
 
//────────────────────────────────────────────────────────────────────
// STYLE PRESETS (resolved from the chart timeframe or the dropdown)
//────────────────────────────────────────────────────────────────────
chartSec    = timeframe.in_seconds()
autoStyle   = chartSec <= 300 ? "Scalping" : chartSec <= 3600 ? "Intraday" : "Swing"
activeStyle = styleIn == "Auto (by timeframe)" ? autoStyle : styleIn
isCustom    = activeStyle == "Custom"
isScalp     = activeStyle == "Scalping"
isSwing     = activeStyle == "Swing"
 
//                        Custom       Scalping  Swing   Intraday
fastLen     = isCustom ? fastIn     : isScalp ? 9    : isSwing ? 34   : 21
slowLen     = isCustom ? slowIn     : isScalp ? 21   : isSwing ? 89   : 55
adxMin      = isCustom ? adxMinIn   : isScalp ? 20.0 : isSwing ? 20.0 : 18.0
breakLen    = isCustom ? breakIn    : isScalp ? 12   : isSwing ? 30   : 20
cooldown    = isCustom ? cooldownIn : isScalp ? 5    : isSwing ? 10   : 8
stopAtr     = isCustom ? stopIn     : isScalp ? 1.2  : isSwing ? 2.5  : 1.8
target1Atr  = isCustom ? t1In       : isScalp ? 1.5  : isSwing ? 3.0  : 2.0
target2Atr  = isCustom ? t2In       : isScalp ? 2.5  : isSwing ? 5.0  : 3.0
 
htf         = htfAuto ? (chartSec <= 180 ? "15" : chartSec <= 300 ? "60" : chartSec <= 3600 ? "240" : chartSec <= 14400 ? "D" : chartSec <= 86400 ? "W" : "M") : htfManual
htfValid    = timeframe.in_seconds(htf) > chartSec   // a filter timeframe at or below the chart timeframe is ignored
 
//────────────────────────────────────────────────────────────────────
// CORE CALCULATIONS
//────────────────────────────────────────────────────────────────────
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
rsiValue = ta.rsi(close, rsiLen)
atr = ta.atr(atrLen)
atrAvg = ta.sma(atr, 100)
volAvg = ta.sma(volume, volLen)
[plusDI, minusDI, adx] = ta.dmi(adxLen, adxLen)
 
recentHigh = ta.highest(high, breakLen)[1]
recentLow  = ta.lowest(low, breakLen)[1]
 
hasVolume = not na(volume) and not na(volAvg) and volAvg > 0
volumeOK = not useVolume or not hasVolume or volume > volAvg * volMult
trendOK = adx >= adxMin
 
// [1] + lookahead_on = last COMPLETED higher-timeframe bar, so it never repaints
htfFast = request.security(syminfo.tickerid, htf, ta.ema(close, fastLen)[1], lookahead=barmerge.lookahead_on)
htfSlow = request.security(syminfo.tickerid, htf, ta.ema(close, slowLen)[1], lookahead=barmerge.lookahead_on)
htfBull = htfFast > htfSlow
htfBear = htfFast < htfSlow
htfLongOK = not useHTF or not htfValid or htfBull
htfShortOK = not useHTF or not htfValid or htfBear
 
bullTrend = fast > slow and fast > fast[1] and slow >= slow[1]
bearTrend = fast < slow and fast < fast[1] and slow <= slow[1]
 
// Confidence score. Volume only earns points when real volume data exists,
// and the score is rescaled to 100 when it does not (many forex/CFD feeds).
volPts   = hasVolume and volume > volAvg * volMult ? 20 : 0
maxScore = hasVolume ? 100.0 : 80.0
 
bullRaw = (close > fast ? 20 : 0) +
     (fast > slow ? 20 : 0) +
     (rsiValue > 55 ? 20 : 0) +
     (adx >= adxMin and plusDI > minusDI ? 20 : 0) +
     volPts
 
bearRaw = (close < fast ? 20 : 0) +
     (fast < slow ? 20 : 0) +
     (rsiValue < 45 ? 20 : 0) +
     (adx >= adxMin and minusDI > plusDI ? 20 : 0) +
     volPts
 
bullScore = math.round(bullRaw * 100.0 / maxScore)
bearScore = math.round(bearRaw * 100.0 / maxScore)
 
//────────────────────────────────────────────────────────────────────
// SIGNAL CONDITIONS
//────────────────────────────────────────────────────────────────────
var int direction = 0              // 1 = long, -1 = short, 0 = flat
var int lastEntryBar = na          // entries and exits
var int lastMinorBar = na          // early / pullback labels (own cooldown so they never block a real entry)
var float entryPrice = na
var float activeStop = na
var float target1 = na
var float target2 = na
var bool tp1Done = false
 
canEntry = na(lastEntryBar) or bar_index - lastEntryBar > cooldown
canMinor = na(lastMinorBar) or bar_index - lastMinorBar > cooldown
 
earlyLong = showEarly and direction == 0 and bullTrend and close > fast and rsiValue > 50 and htfLongOK
earlyShort = showEarly and direction == 0 and bearTrend and close < fast and rsiValue < 50 and htfShortOK
 
longBreakout = direction == 0 and close > recentHigh and bullTrend and rsiValue > 55 and trendOK and volumeOK and htfLongOK
shortBreakout = direction == 0 and close < recentLow and bearTrend and rsiValue < 45 and trendOK and volumeOK and htfShortOK
 
longDip = showDip and direction == 1 and low <= fast and close > fast and close > open and rsiValue > 50
shortRally = showDip and direction == -1 and high >= fast and close < fast and close < open and rsiValue < 50
 
longStopHit = direction == 1 and not na(activeStop) and low <= activeStop
shortStopHit = direction == -1 and not na(activeStop) and high >= activeStop
longTargetHit = direction == 1 and not na(target2) and high >= target2
shortTargetHit = direction == -1 and not na(target2) and low <= target2
 
longTp1Hit = direction == 1 and not tp1Done and not na(target1) and high >= target1
shortTp1Hit = direction == -1 and not tp1Done and not na(target1) and low <= target1
 
longTrendExit = useTrendExit and direction == 1 and close < fast and rsiValue < 45
shortTrendExit = useTrendExit and direction == -1 and close > fast and rsiValue > 55
 
// All events are mutually exclusive. Stop takes priority over target if both
// are touched within the same candle (conservative assumption).
bool earlyLongSignal = false
bool earlyShortSignal = false
bool longEntrySignal = false
bool shortEntrySignal = false
bool longDipSignal = false
bool shortRallySignal = false
bool longExitSignal = false
bool shortExitSignal = false
bool longTakeProfitSignal = false
bool shortTakeProfitSignal = false
bool longTp1Signal = false
bool shortTp1Signal = false
 
if barstate.isconfirmed
    if longStopHit
        longExitSignal := true
    else if shortStopHit
        shortExitSignal := true
    else if longTargetHit
        longTakeProfitSignal := true
    else if shortTargetHit
        shortTakeProfitSignal := true
    else if longTrendExit
        longExitSignal := true
    else if shortTrendExit
        shortExitSignal := true
 
    tradeClosed = longExitSignal or shortExitSignal or longTakeProfitSignal or shortTakeProfitSignal
 
    if tradeClosed
        direction := 0
        entryPrice := na
        activeStop := na
        target1 := na
        target2 := na
        tp1Done := false
        lastEntryBar := bar_index
    else if longTp1Hit
        longTp1Signal := true
        tp1Done := true
    else if shortTp1Hit
        shortTp1Signal := true
        tp1Done := true
    else if canEntry and longBreakout
        longEntrySignal := true
        direction := 1
        entryPrice := close
        activeStop := close - atr * stopAtr
        target1 := close + atr * target1Atr
        target2 := close + atr * target2Atr
        tp1Done := false
        lastEntryBar := bar_index
    else if canEntry and shortBreakout
        shortEntrySignal := true
        direction := -1
        entryPrice := close
        activeStop := close + atr * stopAtr
        target1 := close - atr * target1Atr
        target2 := close - atr * target2Atr
        tp1Done := false
        lastEntryBar := bar_index
    else if canMinor and longDip
        longDipSignal := true
        lastMinorBar := bar_index
    else if canMinor and shortRally
        shortRallySignal := true
        lastMinorBar := bar_index
    else if canMinor and earlyLong
        earlyLongSignal := true
        lastMinorBar := bar_index
    else if canMinor and earlyShort
        earlyShortSignal := true
        lastMinorBar := bar_index
 
//────────────────────────────────────────────────────────────────────
// CHART DISPLAY
//────────────────────────────────────────────────────────────────────
trendColor = direction == 1 ? color.lime : direction == -1 ? color.red : color.gray
plot(showEMAs ? fast : na, "Fast EMA", color=trendColor, linewidth=2)
plot(showEMAs ? slow : na, "Slow EMA", color=color.new(color.gray, 45), linewidth=2)
 
plot(showLevels and direction != 0 ? activeStop : na, "Active Stop", color=color.red, linewidth=2, style=plot.style_linebr)
plot(showLevels and direction != 0 ? target1 : na, "Target 1", color=color.new(color.aqua, 10), linewidth=1, style=plot.style_linebr)
plot(showLevels and direction != 0 ? target2 : na, "Target 2", color=color.new(color.green, 10), linewidth=2, style=plot.style_linebr)
 
plotshape(earlyLongSignal, title="EARLY LONG", text="EARLY\nLONG", style=shape.labelup, location=location.belowbar, color=color.new(color.lime, 20), textcolor=color.black, size=size.tiny)
plotshape(earlyShortSignal, title="EARLY SHORT", text="EARLY\nSHORT", style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 20), textcolor=color.white, size=size.tiny)
plotshape(longEntrySignal, title="LONG ENTRY", text="LONG", style=shape.labelup, location=location.belowbar, color=color.lime, textcolor=color.black, size=size.small)
plotshape(shortEntrySignal, title="SHORT ENTRY", text="SHORT", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)
plotshape(longDipSignal, title="BUY DIP", text="BUY DIP", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(shortRallySignal, title="SELL RALLY", text="SELL RALLY", style=shape.labeldown, location=location.abovebar, color=color.maroon, textcolor=color.white, size=size.tiny)
plotshape(longTp1Signal, title="LONG TP1", text="TP1", style=shape.labeldown, location=location.abovebar, color=color.new(color.aqua, 10), textcolor=color.black, size=size.tiny)
plotshape(shortTp1Signal, title="SHORT TP1", text="TP1", style=shape.labelup, location=location.belowbar, color=color.new(color.aqua, 10), textcolor=color.black, size=size.tiny)
plotshape(longTakeProfitSignal, title="LONG TAKE PROFIT", text="LONG TP", style=shape.labeldown, location=location.abovebar, color=color.yellow, textcolor=color.black, size=size.small)
plotshape(shortTakeProfitSignal, title="SHORT TAKE PROFIT", text="SHORT TP", style=shape.labelup, location=location.belowbar, color=color.yellow, textcolor=color.black, size=size.small)
plotshape(longExitSignal, title="LONG EXIT", text="EXIT LONG", style=shape.labeldown, location=location.abovebar, color=color.orange, textcolor=color.black, size=size.small)
plotshape(shortExitSignal, title="SHORT EXIT", text="EXIT SHORT", style=shape.labelup, location=location.belowbar, color=color.orange, textcolor=color.black, size=size.small)
 
//────────────────────────────────────────────────────────────────────
// DASHBOARD
//────────────────────────────────────────────────────────────────────
trendText = direction == 1 ? "BULLISH / LONG" : direction == -1 ? "BEARISH / SHORT" : bullTrend ? "BULLISH WATCH" : bearTrend ? "BEARISH WATCH" : "NEUTRAL / WAIT"
actionText = direction == 1 ? "HOLD LONG / BUY DIP" : direction == -1 ? "HOLD SHORT / SELL RALLY" : longBreakout ? "WATCH LONG BREAKOUT" : shortBreakout ? "WATCH SHORT BREAKDOWN" : "WAIT FOR SETUP"
 
leanShort  = direction == -1 or (direction == 0 and bearScore > bullScore)
confidence = leanShort ? bearScore : bullScore
 
// Volatility is judged against this instrument's own recent ATR, so the label means the same on every pair and timeframe
atrRatio = na(atrAvg) or atrAvg == 0 ? 1.0 : atr / atrAvg
riskText = na(atr) ? "N/A" : atrRatio > 1.5 ? "HIGH VOLATILITY" : atrRatio < 0.7 ? "LOW VOLATILITY" : "NORMAL VOLATILITY"
 
htfText = not useHTF ? "OFF" : not htfValid ? "SKIPPED (HTF <= chart)" : (htfBull ? "BULLISH" : htfBear ? "BEARISH" : "NEUTRAL") + " · " + htf
 
var table dash = table.new(position.top_right, 2, 11, border_width=1)
if barstate.islast and showDashboard
    table.cell(dash, 0, 0, "ALPHA V6 PRO", text_color=color.white, bgcolor=color.black)
    table.cell(dash, 1, 0, syminfo.ticker + " · " + timeframe.period, text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 1, "Trend", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 1, trendText, text_color=color.white, bgcolor=direction == 1 ? color.green : direction == -1 ? color.red : color.gray)
    table.cell(dash, 0, 2, "Action", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 2, actionText, text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 3, "Confidence", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 3, str.tostring(confidence) + "/100 (" + (leanShort ? "SHORT" : "LONG") + ")", text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 4, "RSI / ADX", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 4, str.tostring(rsiValue, "#.0") + " / " + str.tostring(adx, "#.0"), text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 5, "Volatility", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 5, riskText, text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 6, "Entry", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 6, na(entryPrice) ? "—" : str.tostring(entryPrice, format.mintick), text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 7, "Stop", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 7, na(activeStop) ? "—" : str.tostring(activeStop, format.mintick), text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 8, "Target 1 / 2", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 8, na(target1) ? "—" : str.tostring(target1, format.mintick) + " / " + str.tostring(target2, format.mintick), text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 9, "HTF Filter", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 9, htfText, text_color=color.white, bgcolor=color.black)
    table.cell(dash, 0, 10, "Style", text_color=color.white, bgcolor=color.new(color.gray, 65))
    table.cell(dash, 1, 10, activeStyle + " · EMA " + str.tostring(fastLen) + "/" + str.tostring(slowLen), text_color=color.white, bgcolor=color.black)
else if barstate.islast and not showDashboard
    table.clear(dash, 0, 0, 1, 10)
 
//────────────────────────────────────────────────────────────────────
// ALERTS  (create them with "Once Per Bar Close")
//────────────────────────────────────────────────────────────────────
alertcondition(longEntrySignal, "Alpha Long Entry", "Alpha V6 Pro: LONG entry signal")
alertcondition(shortEntrySignal, "Alpha Short Entry", "Alpha V6 Pro: SHORT entry signal")
alertcondition(longDipSignal, "Alpha Buy Dip", "Alpha V6 Pro: BUY DIP signal")
alertcondition(shortRallySignal, "Alpha Sell Rally", "Alpha V6 Pro: SELL RALLY signal")
alertcondition(longTp1Signal, "Alpha Long TP1", "Alpha V6 Pro: LONG target 1 reached")
alertcondition(shortTp1Signal, "Alpha Short TP1", "Alpha V6 Pro: SHORT target 1 reached")
alertcondition(longTakeProfitSignal, "Alpha Long Take Profit", "Alpha V6 Pro: LONG take-profit target reached")
alertcondition(shortTakeProfitSignal, "Alpha Short Take Profit", "Alpha V6 Pro: SHORT take-profit target reached")
alertcondition(longExitSignal, "Alpha Long Exit", "Alpha V6 Pro: LONG exit / stop signal")
alertcondition(shortExitSignal, "Alpha Short Exit", "Alpha V6 Pro: SHORT exit / stop signal")

← 返回列表