London Multi MA Signals V4 Heikin Ashi

JamesK318 · study · 324 行 · 点赞 21 · TradingView 原页

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

Pine Script

//@version=6
indicator("London Multi MA Signals V4 Heikin Ashi", overlay=true, max_labels_count=2)
 
// Moving averages
g1 = "Moving averages"
fastLen = input.int(5, "Fast length", minval=1, group=g1)
slowLen = input.int(13, "Slow length", minval=2, group=g1)
useEMA = input.bool(true, "EMA vote", group=g1)
useSMA = input.bool(true, "SMA vote", group=g1)
useWMA = input.bool(true, "WMA vote", group=g1)
useVMA = input.bool(true, "Volatility-adaptive MA vote", group=g1)
votesRequired = input.int(2, "Minimum agreeing votes", minval=1, maxval=4, group=g1)
showMA = input.bool(false, "Show fast/slow EMAs", group=g1)
 
// Signal settings
g6 = "Heikin Ashi signals"
enableBuy = input.bool(true, "Show BUY signals", group=g6)
buyAmount = input.string("Medium", "BUY signal amount", options=["High", "Medium", "Low"], group=g6)
enableSell = input.bool(true, "Show SELL signals", group=g6)
sellAmount = input.string("Medium", "SELL signal amount", options=["High", "Medium", "Low"], group=g6)
 
// Speed and volatility
g2 = "Speed and volatility"
cooldown = input.int(1, "Full bars between signals", minval=0, group=g2)
window = input.int(3, "Crossover entry window", minval=0, group=g2)
allowPullbacks = input.bool(true, "Pullback continuation signals", group=g2)
useAdaptive = input.bool(true, "Adapt EMA speed to volatility", group=g2)
adaptStrength = input.float(0.5, "Volatility speed adjustment", minval=0, maxval=1, step=0.1, group=g2)
atrLen = input.int(14, "ATR length", minval=2, group=g2)
volBaseLen = input.int(50, "Volatility baseline length", minval=5, group=g2)
minGap = input.float(0.01, "Minimum EMA separation / ATR", minval=0, step=0.01, group=g2)
maxStretch = input.float(2.5, "Maximum entry distance / ATR", minval=0.1, step=0.1, group=g2)
useShock = input.bool(true, "Block oversized signal candles", group=g2)
maxRange = input.float(3.0, "Maximum candle range / previous ATR", minval=0.5, step=0.1, group=g2)
 
// Trend filtering
g3 = "Trend filtering"
useTrend = input.bool(false, "EMA 200 direction filter", group=g3)
trendLen = input.int(200, "Trend EMA length", minval=2, group=g3)
useADX = input.bool(false, "ADX filter", group=g3)
adxMin = input.float(14, "Minimum ADX", minval=0, group=g3)
colorBars = input.bool(true, "Trend bar colors", group=g3)
 
// London session
g4 = "London session"
useSession = input.bool(true, "Use London session", group=g4)
londonHours = input.session("0800-1630", "London hours", group=g4)
sessionDays = input.string("23456", "Days: 1=Sun ... 7=Sat", group=g4)
 
// Display
g5 = "Display"
markerGap = input.float(0.2, "Signal distance / ATR", minval=0, step=0.1, group=g5)
showTable = input.bool(true, "Show top status table", group=g5)
 
g7 = "Previous day levels"
showPriorDay = input.bool(true, "Show PDH and PDL", group=g7)
pdhColor = input.color(color.red, "PDH color", group=g7)
pdlColor = input.color(color.lime, "PDL color", group=g7)
 
// Request Heikin Ashi data on the chart timeframe.
// Keep chart confirmation outside request.security().
haTicker = ticker.heikinashi(syminfo.tickerid)
[haOpen, haHigh, haLow, haClose] = request.security(
     haTicker, timeframe.period, [open, high, low, close],
     gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off)
 
// Daily levels come from standard prices, including on an HA chart.
// The one-bar offset selects the last completed daily candle.
[priorHigh, priorLow] = request.security(
     ticker.standard(syminfo.tickerid), "1D", [high[1], low[1]],
     gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
 
showLevels = showPriorDay and (timeframe.isintraday or timeframe.isdaily and timeframe.multiplier == 1)
 
plot(showLevels ? priorHigh : na, title="Previous Day High", color=pdhColor, style=plot.style_stepline)
plot(showLevels ? priorLow : na, title="Previous Day Low", color=pdlColor, style=plot.style_stepline)
 
var label pdhLabel = na
var label pdlLabel = na
 
if barstate.islast and showLevels and not na(priorHigh) and not na(priorLow)
    if na(pdhLabel)
        pdhLabel := label.new(bar_index, priorHigh, "PDH", style=label.style_label_left, color=pdhColor, textcolor=color.white, size=size.tiny)
        pdlLabel := label.new(bar_index, priorLow, "PDL", style=label.style_label_left, color=pdlColor, textcolor=color.black, size=size.tiny)
    else
        label.set_xy(pdhLabel, bar_index, priorHigh)
        label.set_xy(pdlLabel, bar_index, priorLow)
 
// CMO-weighted volatility-adaptive moving average.
f_vma(float src, int length) =>
    float momentum = math.abs(nz(ta.cmo(src, length), 0)) / 100.0
    float alpha = 2.0 / (length + 1.0) * momentum
    var float average = na
    average := na(average[1]) ? src : alpha * src + (1.0 - alpha) * average[1]
    average
 
// EMA-style smoothing with volatility-adjusted speed.
f_adaptive(float src, int length, float speed) =>
    float alpha = math.min(1.0, 2.0 / (length + 1.0) * speed)
    var float average = na
    average := na(average[1]) ? src : alpha * src + (1.0 - alpha) * average[1]
    average
 
atr = ta.atr(atrLen)
atrBase = ta.sma(atr, volBaseLen)
volRatio = not na(atrBase) and atrBase > 0 ? atr / atrBase : 1.0
speed = useAdaptive ? math.max(0.65, math.min(1.8, 1.0 + (volRatio - 1.0) * adaptStrength)) : 1.0
 
// V3 moving averages and volatility calculations retain the chart source.
ef = f_adaptive(close, fastLen, speed)
es = f_adaptive(close, slowLen, speed)
sf = ta.sma(close, fastLen)
ss = ta.sma(close, slowLen)
wf = ta.wma(close, fastLen)
ws = ta.wma(close, slowLen)
vf = f_vma(close, fastLen)
vs = f_vma(close, slowLen)
trend = ta.ema(close, trendLen)
 
[diPlus, diMinus, adx] = ta.dmi(14, 14)
 
active = (
     (useEMA ? 1 : 0) +
     (useSMA ? 1 : 0) +
     (useWMA ? 1 : 0) +
     (useVMA ? 1 : 0))
 
needed = math.min(votesRequired, active)
 
bulls = (
     (useEMA and ef > es ? 1 : 0) +
     (useSMA and sf > ss ? 1 : 0) +
     (useWMA and wf > ws ? 1 : 0) +
     (useVMA and vf > vs ? 1 : 0))
 
bears = (
     (useEMA and ef < es ? 1 : 0) +
     (useSMA and sf < ss ? 1 : 0) +
     (useWMA and wf < ws ? 1 : 0) +
     (useVMA and vf < vs ? 1 : 0))
 
bullConsensus = active > 0 and bulls >= needed and bulls > bears
bearConsensus = active > 0 and bears >= needed and bears > bulls
 
eUp = ta.crossover(ef, es)
eDn = ta.crossunder(ef, es)
sUp = ta.crossover(sf, ss)
sDn = ta.crossunder(sf, ss)
wUp = ta.crossover(wf, ws)
wDn = ta.crossunder(wf, ws)
vUp = ta.crossover(vf, vs)
vDn = ta.crossunder(vf, vs)
 
upCross = (useEMA and eUp) or (useSMA and sUp) or (useWMA and wUp) or (useVMA and vUp)
dnCross = (useEMA and eDn) or (useSMA and sDn) or (useWMA and wDn) or (useVMA and vDn)
 
upAge = ta.barssince(upCross)
dnAge = ta.barssince(dnCross)
upID = not na(upAge) ? bar_index - upAge : na
dnID = not na(dnAge) ? bar_index - dnAge : na
 
sessionInside = not na(time(timeframe.period, londonHours + ":" + sessionDays, "Europe/London"))
sessionOK = not useSession or sessionInside
 
shockOK = not useShock or (
     not na(atr[1]) and
     atr[1] > 0 and
     high - low <= atr[1] * maxRange)
 
gapOK = atr > 0 and math.abs(ef - es) >= atr * minGap
stretchOK = atr > 0 and math.abs(close - es) <= atr * maxStretch
strengthOK = not useADX or adx >= adxMin
filtersOK = gapOK and stretchOK and strengthOK and shockOK
 
bullTrendOK = not useTrend or close > trend
bearTrendOK = not useTrend or close < trend
longTrend = bullConsensus and bullTrendOK
shortTrend = bearConsensus and bearTrendOK
 
// Heikin Ashi candle confirmation and pullbacks.
haBull = haClose > haOpen
haBear = haClose < haOpen
haRange = haHigh - haLow
strongBody = haRange > 0 and math.abs(haClose - haOpen) >= haRange * 0.55
 
pullLong = allowPullbacks and haLow <= ef and haClose > ef and haClose > haHigh[1]
pullShort = allowPullbacks and haHigh >= ef and haClose < ef and haClose < haLow[1]
 
haReclaim = ta.crossover(haClose, ef)
haReject = ta.crossunder(haClose, ef)
fastLong = haReclaim or (haBear[1] and haClose > ef)
fastShort = haReject or (haBull[1] and haClose < ef)
 
var int lastSignal = na
var int lastUpCross = na
var int lastDnCross = na
var int lastDirection = 0
 
freshUp = not na(upAge) and upAge <= window and (na(lastUpCross) or upID != lastUpCross)
freshDn = not na(dnAge) and dnAge <= window and (na(lastDnCross) or dnID != lastDnCross)
 
highVotes = math.max(1, needed - 1)
lowVotes = math.min(active, needed + 1)
 
buySetup = switch buyAmount
    "High" => bulls >= highVotes and (freshUp or pullLong or fastLong)
    "Low" => bullConsensus and bulls >= lowVotes and strongBody and (freshUp or pullLong)
    => bullConsensus and (freshUp or pullLong)
 
sellSetup = switch sellAmount
    "High" => bears >= highVotes and (freshDn or pullShort or fastShort)
    "Low" => bearConsensus and bears >= lowVotes and strongBody and (freshDn or pullShort)
    => bearConsensus and (freshDn or pullShort)
 
ready = na(lastSignal) or bar_index - lastSignal > cooldown
 
// Alternate while both sides are enabled. If only one side is
// enabled, allow subsequent signals on that side after cooldown.
canBuy = enableBuy and (not enableSell or lastDirection != 1)
canSell = enableSell and (not enableBuy or lastDirection != -1)
 
signalReady = barstate.isconfirmed and active > 0 and sessionOK and filtersOK and ready
 
buy = signalReady and canBuy and bullTrendOK and haBull and buySetup
sell = signalReady and canSell and bearTrendOK and haBear and sellSetup
 
if buy
    lastSignal := bar_index
    lastDirection := 1
    lastUpCross := upID
else if sell
    lastSignal := bar_index
    lastDirection := -1
    lastDnCross := dnID
 
plotshape(
     buy ? low - atr * markerGap : na, title="BUY",
     style=shape.labelup, location=location.absolute,
     color=color.blue, text="BUY", textcolor=color.white, size=size.tiny)
 
plotshape(
     sell ? high + atr * markerGap : na, title="SELL",
     style=shape.labeldown, location=location.absolute,
     color=color.purple, text="SELL", textcolor=color.white, size=size.tiny)
 
var color stableColor = color.gray
if barstate.isconfirmed
    stableColor := longTrend and filtersOK ? color.teal : shortTrend and filtersOK ? color.red : color.gray
 
barcolor(colorBars ? stableColor : na)
plot(showMA ? ef : na, title="Fast adaptive EMA", color=color.aqua)
plot(showMA ? es : na, title="Slow adaptive EMA", color=color.orange)
 
// Top-right status table.
var table panel = table.new(
     position.top_right, 2, 10, bgcolor=color.new(color.black, 15),
     frame_color=color.new(color.white, 65), frame_width=1)
 
trendText = longTrend ? "BULLISH" : shortTrend ? "BEARISH" : "MIXED"
trendColor = longTrend ? color.teal : shortTrend ? color.red : color.gray
volText = volRatio > 1.2 ? "HIGH" : volRatio < 0.8 ? "LOW" : "NORMAL"
lastText = buy ? "BUY" : sell ? "SELL" : lastDirection == 1 ? "LAST: BUY" : lastDirection == -1 ? "LAST: SELL" : "NONE"
signalColor = buy ? color.blue : sell ? color.purple : color.gray
 
nextText = "BUY OR SELL"
if not enableBuy and not enableSell
    nextText := "OFF"
else if not enableBuy or (enableSell and lastDirection == 1)
    nextText := "SELL ONLY"
else if not enableSell or lastDirection == -1
    nextText := "BUY ONLY"
 
filterText = "FILTERS PASSED"
if active == 0
    filterText := "ENABLE AN MA"
else if not sessionOK
    filterText := "OUTSIDE SESSION"
else if not shockOK
    filterText := "RANGE TOO LARGE"
else if not gapOK
    filterText := "MA GAP TOO SMALL"
else if not stretchOK
    filterText := "PRICE EXTENDED"
else if not strengthOK
    filterText := "WEAK ADX"
else if not ready
    filterText := "COOLDOWN"
 
if barstate.islast
    if showTable
        table.cell(panel, 0, 0, "ALTERNATING SIGNALS", text_color=color.white, bgcolor=color.navy)
        table.cell(panel, 1, 0, syminfo.ticker, text_color=color.white, bgcolor=color.navy)
 
        table.cell(panel, 0, 1, "Timing", text_color=color.white)
        table.cell(panel, 1, 1, "HA CLOSE CONFIRMED", text_color=color.aqua)
 
        table.cell(panel, 0, 2, "Trend", text_color=color.white)
        table.cell(panel, 1, 2, trendText, text_color=trendColor)
 
        table.cell(panel, 0, 3, "Votes", text_color=color.white)
        table.cell(panel, 1, 3, "B " + str.tostring(bulls) + " / S " + str.tostring(bears) + " | Base " + str.tostring(needed), text_color=color.white)
 
        table.cell(panel, 0, 4, "Volatility", text_color=color.white)
        table.cell(panel, 1, 4, volText + " " + str.tostring(volRatio, "#.##") + "x", text_color=volRatio > 1.2 ? color.orange : color.aqua)
 
        table.cell(panel, 0, 5, "ATR", text_color=color.white)
        table.cell(panel, 1, 5, str.tostring(atr, format.mintick), text_color=color.white)
 
        table.cell(panel, 0, 6, "London filter", text_color=color.white)
        table.cell(panel, 1, 6, not useSession ? "OFF" : sessionInside ? "IN SESSION" : "CLOSED", text_color=sessionOK ? color.aqua : color.gray)
 
        table.cell(panel, 0, 7, "Status", text_color=color.white)
        table.cell(panel, 1, 7, filterText, text_color=filterText == "FILTERS PASSED" ? color.aqua : color.orange)
 
        table.cell(panel, 0, 8, "Signal", text_color=color.white)
        table.cell(panel, 1, 8, lastText, text_color=color.white, bgcolor=signalColor)
 
        table.cell(panel, 0, 9, "Next allowed", text_color=color.white)
        table.cell(panel, 1, 9, nextText, text_color=color.yellow)
    else
        table.clear(panel, 0, 0, 1, 9)
 
alertcondition(buy, title="BUY signal", message="BUY | {{ticker}} | {{interval}} | Chart close {{close}}")
alertcondition(sell, title="SELL signal", message="SELL | {{ticker}} | {{interval}} | Chart close {{close}}")

← 返回列表