Quorum

Prometheu55 · study · 300 行 · 点赞 40 · TradingView 原页

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

Pine Script

//@version=6
// IA-Style Decision Suite (original recreation — not IADSS)
// Combined overlay: Confluence + Trend + Optimized Trend + Mean Reversion
// Transparent 5-factor vote basket. Style presets change thresholds, not formulas.
 
indicator("Quorum", shorttitle="Quorum", overlay=true, max_labels_count=500, max_bars_back=5000)
 
grpS = "Style & Bias"
style = input.string("Neutral", "Trading style", options=["Very Aggressive", "Aggressive", "Neutral", "Conservative", "Very Conservative"], group=grpS)
bias  = input.string("Neutral", "Bias", options=["Bullish", "Neutral", "Bearish"], group=grpS)
 
grpC = "Confluence basket"
rsiLen   = input.int(14, "RSI length", minval=2, group=grpC)
macdF    = input.int(12, "MACD fast", minval=2, group=grpC)
macdS    = input.int(26, "MACD slow", minval=2, group=grpC)
macdSig  = input.int(9,  "MACD signal", minval=2, group=grpC)
stochLen = input.int(14, "Stochastic length", minval=2, group=grpC)
emaFast  = input.int(21, "Vote EMA fast", minval=2, group=grpC)
emaSlow  = input.int(55, "Vote EMA slow", minval=2, group=grpC)
bbLen    = input.int(20, "Bollinger length", minval=2, group=grpC)
bbMult   = input.float(2.0, "Bollinger mult", step=0.1, group=grpC)
confirmBars = input.int(5, "Confirmation window (bars)", minval=1, maxval=50, group=grpC)
 
grpT = "Trend"
trendEngine = input.string("SuperTrend (tight)", "Trend engine", options=["SuperTrend (tight)", "EMA Ribbon"], group=grpT, tooltip="SuperTrend hugs price like Bullmania MoneyLine. EMA Ribbon is the slower 2-line fill.")
tfPreset = input.string("Manual", "EMA preset (Ribbon mode)", options=["Manual", "Scalp 8/21", "Intraday 13/34", "Swing 20/50", "Daily 21/55", "Weekly 10/30"], group=grpT)
tFastIn = input.int(20, "Trend fast EMA (Manual)", minval=2, group=grpT)
tSlowIn = input.int(50, "Trend slow EMA (Manual)", minval=2, group=grpT)
stAtrLen = input.int(10, "SuperTrend ATR length", minval=1, group=grpT)
stMult   = input.float(2.0, "SuperTrend ATR mult", minval=0.5, step=0.1, group=grpT, tooltip="Lower = tighter / more flips. 1.5 aggressive, 2.0 default, 3.0 smoother.")
 
grpO = "Optimized Trend"
oLook = input.int(250, "Opt lookback (bars)", minval=50, group=grpO)
 
grpM = "Mean Reversion"
mrLen = input.int(50, "MR mean length", minval=5, group=grpM)
noiseSup = input.int(0, "Noise suppression (bars)", minval=0, maxval=100, group=grpM, tooltip="Ignore a new MR dot if another printed within N bars.")
 
grpV = "Display"
showConf   = input.bool(true, "Show confluence triangles", group=grpV)
showTrend  = input.bool(true, "Show trend ribbon", group=grpV)
showOpt    = input.bool(true, "Show optimized trend", group=grpV)
showMR     = input.bool(true, "Show MR dots on price", group=grpV)
showTable  = input.bool(true, "Show dashboard / mini backtest", group=grpV)
btStart    = input.time(timestamp("2023-01-01"), "Backtest start", group=grpV)
 
rsiBuyBase  = style == "Very Aggressive" ? 40 : style == "Aggressive" ? 35 : style == "Neutral" ? 30 : style == "Conservative" ? 25 : 20
rsiSellBase = style == "Very Aggressive" ? 60 : style == "Aggressive" ? 65 : style == "Neutral" ? 70 : style == "Conservative" ? 75 : 80
bbBuyMax    = style == "Very Aggressive" ? 0.35 : style == "Aggressive" ? 0.25 : style == "Neutral" ? 0.20 : style == "Conservative" ? 0.12 : 0.08
bbSellMin   = 1.0 - bbBuyMax
mrSigma     = style == "Very Aggressive" ? 1.00 : style == "Aggressive" ? 1.25 : style == "Neutral" ? 1.50 : style == "Conservative" ? 1.75 : 2.00
tightMult   = style == "Very Aggressive" ? 0.70 : style == "Aggressive" ? 0.85 : style == "Neutral" ? 1.00 : style == "Conservative" ? 1.20 : 1.40
votesNeedUnc = 3
votesNeedCnf = style == "Very Aggressive" ? 3 : 4
 
rsiBuy  = bias == "Bullish" ? math.min(50, rsiBuyBase + 8) : bias == "Bearish" ? math.max(10, rsiBuyBase - 8) : rsiBuyBase
rsiSell = bias == "Bearish" ? math.max(50, rsiSellBase - 8) : bias == "Bullish" ? math.min(90, rsiSellBase + 8) : rsiSellBase
stochBuy  = rsiBuy
stochSell = rsiSell
 
rsi = ta.rsi(close, rsiLen)
[macdLine, macdSignal, macdHist] = ta.macd(close, macdF, macdS, macdSig)
stochK = ta.sma(ta.stoch(close, high, low, stochLen), 3)
emaF = ta.ema(close, emaFast)
emaS = ta.ema(close, emaSlow)
basis = ta.sma(close, bbLen)
dev   = bbMult * ta.stdev(close, bbLen)
pctB  = dev == 0 ? 0.5 : (close - (basis - dev)) / (2 * dev)
 
voteRsiBuy  = rsi <= rsiBuy and rsi > rsi[1]
voteRsiSell = rsi >= rsiSell and rsi < rsi[1]
voteMacdBuy  = macdHist > 0 and macdHist > macdHist[1]
voteMacdSell = macdHist < 0 and macdHist < macdHist[1]
voteStoBuy  = stochK <= stochBuy
voteStoSell = stochK >= stochSell
voteEmaBuy  = emaF > emaS
voteEmaSell = emaF < emaS
voteBbBuy   = pctB <= bbBuyMax
voteBbSell  = pctB >= bbSellMin
 
buyVotes  = (voteRsiBuy ? 1 : 0) + (voteMacdBuy ? 1 : 0) + (voteStoBuy ? 1 : 0) + (voteEmaBuy ? 1 : 0) + (voteBbBuy ? 1 : 0)
sellVotes = (voteRsiSell ? 1 : 0) + (voteMacdSell ? 1 : 0) + (voteStoSell ? 1 : 0) + (voteEmaSell ? 1 : 0) + (voteBbSell ? 1 : 0)
 
rawBuy  = buyVotes  >= votesNeedUnc and buyVotes  > sellVotes
rawSell = sellVotes >= votesNeedUnc and sellVotes > buyVotes
 
buyCountWin  = math.sum(rawBuy ? 1 : 0, confirmBars)
sellCountWin = math.sum(rawSell ? 1 : 0, confirmBars)
confirmedBuy  = rawBuy  and buyCountWin  >= 2 and buyVotes  >= votesNeedCnf
confirmedSell = rawSell and sellCountWin >= 2 and sellVotes >= votesNeedCnf
unconfBuy  = rawBuy  and not confirmedBuy
unconfSell = rawSell and not confirmedSell
 
tFast = tfPreset == "Scalp 8/21" ? 8 : tfPreset == "Intraday 13/34" ? 13 : tfPreset == "Swing 20/50" ? 20 : tfPreset == "Daily 21/55" ? 21 : tfPreset == "Weekly 10/30" ? 10 : tFastIn
tSlow = tfPreset == "Scalp 8/21" ? 21 : tfPreset == "Intraday 13/34" ? 34 : tfPreset == "Swing 20/50" ? 50 : tfPreset == "Daily 21/55" ? 55 : tfPreset == "Weekly 10/30" ? 30 : tSlowIn
 
slowAdj = math.max(2, math.round(tSlow * tightMult))
trFast = ta.ema(close, tFast)
trSlow = ta.ema(close, int(slowAdj))
emaTrendUp = trFast > trSlow
 
atrST = ta.atr(stAtrLen)
srcST = hl2
upBand = srcST - stMult * atrST
dnBand = srcST + stMult * atrST
 
var float stLower = na
var float stUpper = na
var int   stDir = 1
stLower := na(stLower[1]) ? upBand : (close[1] > stLower[1] ? math.max(upBand, stLower[1]) : upBand)
stUpper := na(stUpper[1]) ? dnBand : (close[1] < stUpper[1] ? math.min(dnBand, stUpper[1]) : dnBand)
stDir := close > nz(stUpper[1]) ? 1 : close < nz(stLower[1]) ? -1 : nz(stDir[1], 1)
stLine = stDir == 1 ? stLower : stUpper
stTrendUp = stDir == 1
 
useST = trendEngine == "SuperTrend (tight)"
trendUp = useST ? stTrendUp : emaTrendUp
trendFlipUp   = trendUp and not trendUp[1]
trendFlipDown = not trendUp and trendUp[1]
 
pFast = plot(showTrend and not useST ? trFast : na, "Trend fast", color=color.new(color.teal, 40), linewidth=1)
pSlow = plot(showTrend and not useST ? trSlow : na, "Trend slow", color=color.new(color.orange, 40), linewidth=1)
fill(pFast, pSlow, color=showTrend and not useST ? (emaTrendUp ? color.new(color.teal, 80) : color.new(color.orange, 80)) : na, title="Trend fill")
plot(showTrend and useST ? stLine : na, "SuperTrend", color=stTrendUp ? color.new(color.lime, 0) : color.new(#ff00aa, 0), linewidth=3, style=plot.style_line)
 
ema8  = ta.ema(close, 8)
ema13 = ta.ema(close, 13)
ema21 = ta.ema(close, 21)
ema34 = ta.ema(close, 34)
ema55 = ta.ema(close, 55)
ema89 = ta.ema(close, 89)
 
scorePair(bool pos, int look) =>
    float rets = 0.0
    float sq = 0.0
    float n = 0.0
    for k = 1 to look
        r = (pos[k] ? 1.0 : 0.0) * (close[k-1] / close[k] - 1.0)
        rets += r
        sq += r * r
        n += 1.0
    mu = n == 0 ? 0.0 : rets / n
    vari = n == 0 ? 0.0 : math.max(sq / n - mu * mu, 0.0)
    vari == 0 ? mu * 100.0 : mu / math.sqrt(vari)
 
sc813  = scorePair(ema8  > ema13, oLook)
sc821  = scorePair(ema8  > ema21, oLook)
sc834  = scorePair(ema8  > ema34, oLook)
sc855  = scorePair(ema8  > ema55, oLook)
sc889  = scorePair(ema8  > ema89, oLook)
sc1321 = scorePair(ema13 > ema21, oLook)
sc1334 = scorePair(ema13 > ema34, oLook)
sc1355 = scorePair(ema13 > ema55, oLook)
sc1389 = scorePair(ema13 > ema89, oLook)
sc2134 = scorePair(ema21 > ema34, oLook)
sc2155 = scorePair(ema21 > ema55, oLook)
sc2189 = scorePair(ema21 > ema89, oLook)
 
bestScore = sc813
bestF = 8
bestS = 13
if sc821 > bestScore
    bestScore := sc821
    bestF := 8
    bestS := 21
if sc834 > bestScore
    bestScore := sc834
    bestF := 8
    bestS := 34
if sc855 > bestScore
    bestScore := sc855
    bestF := 8
    bestS := 55
if sc889 > bestScore
    bestScore := sc889
    bestF := 8
    bestS := 89
if sc1321 > bestScore
    bestScore := sc1321
    bestF := 13
    bestS := 21
if sc1334 > bestScore
    bestScore := sc1334
    bestF := 13
    bestS := 34
if sc1355 > bestScore
    bestScore := sc1355
    bestF := 13
    bestS := 55
if sc1389 > bestScore
    bestScore := sc1389
    bestF := 13
    bestS := 89
if sc2134 > bestScore
    bestScore := sc2134
    bestF := 21
    bestS := 34
if sc2155 > bestScore
    bestScore := sc2155
    bestF := 21
    bestS := 55
if sc2189 > bestScore
    bestScore := sc2189
    bestF := 21
    bestS := 89
 
optFast = bestF == 8 ? ema8 : bestF == 13 ? ema13 : ema21
optSlow = bestS == 13 ? ema13 : bestS == 21 ? ema21 : bestS == 34 ? ema34 : bestS == 55 ? ema55 : ema89
optUp = optFast > optSlow
optFlipUp   = optUp and not optUp[1]
optFlipDown = not optUp and optUp[1]
 
plot(showOpt ? optFast : na, "Opt fast", color=color.new(color.blue, 20), linewidth=2)
plot(showOpt ? optSlow : na, "Opt slow", color=color.new(color.purple, 30), linewidth=1)
 
mrMean = ta.sma(close, mrLen)
mrSd   = ta.stdev(close, mrLen)
z = mrSd == 0 ? 0.0 : (close - mrMean) / mrSd
 
mrBuyRaw  = z[1] <= -mrSigma and z > z[1] and z > -mrSigma
mrSellRaw = z[1] >=  mrSigma and z < z[1] and z <  mrSigma
 
var int barsSinceMrBuy = 999
var int barsSinceMrSell = 999
barsSinceMrBuy := mrBuyRaw[1] ? 0 : barsSinceMrBuy + 1
barsSinceMrSell := mrSellRaw[1] ? 0 : barsSinceMrSell + 1
mrBuy  = mrBuyRaw  and barsSinceMrBuy > noiseSup
mrSell = mrSellRaw and barsSinceMrSell > noiseSup
 
plotshape(showMR and mrBuy,  title="MR buy",  style=shape.circle, location=location.belowbar, color=color.new(color.lime, 0), size=size.tiny)
plotshape(showMR and mrSell, title="MR sell", style=shape.circle, location=location.abovebar, color=color.new(color.red, 0), size=size.tiny)
 
plotshape(showConf and confirmedBuy,  title="Confirmed buy",  style=shape.triangleup,   location=location.belowbar, color=color.new(color.lime, 0), size=size.small, text="BUY")
plotshape(showConf and unconfBuy,     title="Unconfirmed buy", style=shape.triangleup,   location=location.belowbar, color=color.new(color.lime, 65), size=size.tiny)
plotshape(showConf and confirmedSell, title="Confirmed sell", style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small, text="SELL")
plotshape(showConf and unconfSell,    title="Unconfirmed sell",style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 65), size=size.tiny)
 
stackBuy  = confirmedBuy  and trendUp and optUp and (mrBuy or z < 0)
stackSell = confirmedSell and (not trendUp) and (not optUp) and (mrSell or z > 0)
 
plotshape(stackBuy,  title="Stack buy",  style=shape.diamond, location=location.belowbar, color=color.new(color.aqua, 0), size=size.small)
plotshape(stackSell, title="Stack sell", style=shape.diamond, location=location.abovebar, color=color.new(color.fuchsia, 0), size=size.small)
 
var float inPrice = na
var int   wins = 0
var int   losses = 0
var float pnlSum = 0.0
inTrade = not na(inPrice)
eligible = time >= btStart
 
if eligible and stackBuy and not inTrade
    inPrice := close
if eligible and stackSell and inTrade
    r = (close - inPrice) / inPrice
    pnlSum += r
    if r > 0
        wins += 1
    else
        losses += 1
    inPrice := na
 
closed = wins + losses
winPct = closed == 0 ? na : 100.0 * wins / closed
avgR   = closed == 0 ? na : 100.0 * pnlSum / closed
 
if barstate.islast and showTable
    t = table.new(position.top_right, 2, 12, bgcolor=color.new(color.black, 20), border_width=1)
    table.cell(t, 0, 0, "IADS-Rec", text_color=color.white, text_size=size.small)
    table.cell(t, 1, 0, style + " / " + bias, text_color=color.yellow, text_size=size.small)
    table.cell(t, 0, 1, "Buy votes", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 1, str.tostring(buyVotes) + " / 5", text_color=color.lime, text_size=size.tiny)
    table.cell(t, 0, 2, "Sell votes", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 2, str.tostring(sellVotes) + " / 5", text_color=color.red, text_size=size.tiny)
    table.cell(t, 0, 3, "Z-score", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 3, str.tostring(z, "#.00") + "  σ=" + str.tostring(mrSigma), text_color=color.white, text_size=size.tiny)
    table.cell(t, 0, 4, "Trend", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 4, trendUp ? "BLUE up" : "ORANGE dn", text_color=trendUp ? color.teal : color.orange, text_size=size.tiny)
    table.cell(t, 0, 5, "Opt trend", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 5, optUp ? "BLUE up" : "ORANGE dn", text_color=optUp ? color.aqua : color.purple, text_size=size.tiny)
    table.cell(t, 0, 6, "Opt lens", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 6, str.tostring(bestF) + " / " + str.tostring(bestS), text_color=color.white, text_size=size.tiny)
    table.cell(t, 0, 7, "Stack longs", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 7, inTrade ? "IN @ " + str.tostring(inPrice, format.mintick) : "FLAT", text_color=inTrade ? color.aqua : color.gray, text_size=size.tiny)
    table.cell(t, 0, 8, "Closed trades", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 8, str.tostring(closed), text_color=color.white, text_size=size.tiny)
    table.cell(t, 0, 9, "Win %", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 9, na(winPct) ? "—" : str.tostring(winPct, "#.0") + "%", text_color=color.white, text_size=size.tiny)
    table.cell(t, 0, 10, "Avg trade", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 10, na(avgR) ? "—" : str.tostring(avgR, "#.20") + "%", text_color=color.white, text_size=size.tiny)
    table.cell(t, 0, 11, "Basket", text_color=color.gray, text_size=size.tiny)
    table.cell(t, 1, 11, "RSI MACD Stoch EMA BB", text_color=color.silver, text_size=size.tiny)
 
alertcondition(confirmedBuy,  "Confirmed buy",  "IADS-Rec confirmed BUY")
alertcondition(confirmedSell, "Confirmed sell", "IADS-Rec confirmed SELL")
alertcondition(stackBuy,  "Stack buy",  "IADS-Rec STACK BUY (conf + trends up + MR/z support)")
alertcondition(stackSell, "Stack sell", "IADS-Rec STACK SELL (conf + trends down + MR/z resistance)")
alertcondition(mrBuy,  "MR buy turn",  "IADS-Rec mean-reversion buy turn")
alertcondition(mrSell, "MR sell turn", "IADS-Rec mean-reversion sell turn")
alertcondition(trendFlipUp,   "Trend flip up",   "IADS-Rec trend flipped BLUE")
alertcondition(trendFlipDown, "Trend flip down", "IADS-Rec trend flipped ORANGE")

← 返回列表