Valence MACE | WMA Consensus Engine

CryptoSwing-Systems · study · 205 行 · 点赞 157 · TradingView 原页

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

Pine Script

//@version=6
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
// https://mozilla.org/MPL/2.0/
// © CryptoSwing-Systems
 
indicator("Valence MACE | WMA Consensus Engine", overlay=true, max_bars_back=5000)
 
// ─────────────────────────────────────────────────────
// INPUTS
// ─────────────────────────────────────────────────────
groupMA = "WMA Consensus Engine"
maCount = input.int(50, "Number of WMAs", minval=2, maxval=100, group=groupMA)
minLength = input.int(40, "Shortest WMA Length", minval=1, maxval=5000, group=groupMA)
maxLength = input.int(300, "Longest WMA Length", minval=2, maxval=5000, group=groupMA)
 
groupFilter = "ATR Noise Filter"
useFilter = input.bool(true, "Enable ATR Filter", group=groupFilter)
atrLength = input.int(14, "ATR Length", minval=1, group=groupFilter)
atrMultiplier = input.float(1.0, "ATR Multiplier", minval=0.0, step=0.1, group=groupFilter)
 
groupRSI = "RSI Reversion"
rsiLength = input.int(33, "RSI Length", minval=2, group=groupRSI)
rsiLower = input.float(33.0, "RSI Lower Bound", minval=0, maxval=100, group=groupRSI)
showReversion = input.bool(true, "Show PROBABLE REVERSION Labels", group=groupRSI)
 
groupVisual = "Visual Settings"
showAverage = input.bool(true, "Show Composite WMA", group=groupVisual)
colourBars = input.bool(true, "Colour Candles", group=groupVisual)
colourBackground = input.bool(false, "Colour Background", group=groupVisual)
showATRBands = input.bool(true, "Show ATR Bands", group=groupVisual)
 
// ─────────────────────────────────────────────────────
// START DATE
// ─────────────────────────────────────────────────────
int startDate = timestamp("01 Nov 2017 00:00 +0000")
bool inPeriod = time >= startDate
 
// ─────────────────────────────────────────────────────
// CLASSIC2 COLOUR SCHEME
// ─────────────────────────────────────────────────────
color color_up = #17dfad
color color_dn = #dd326b
color color_reversion = #9333EA
 
color col_up1 = color_up
color col_up2 = color.new(color_up, 75)
color col_dn1 = color_dn
color col_dn2 = color.new(color_dn, 75)
 
// ─────────────────────────────────────────────────────
// WMA CONSENSUS ENGINE
// ─────────────────────────────────────────────────────
// Each WMA uses an integer lookback length.
// Lengths are geometrically spaced between min and max.
//
// WMA assigns greater weight to more recent closes.
//
// Composite WMA is the equal-weighted average of
// all selected individual WMAs.
//
// The calculation below uses weighted rolling sums
// to reproduce the standard WMA formula.
// ─────────────────────────────────────────────────────
 
// Cumulative closing-price sum.
float cumulativeClose = ta.cum(close)
 
// Cumulative sum of bar-index-weighted closes.
float cumulativeWeightedClose = ta.cum(close * (bar_index + 1.0))
 
float compositeSum = 0.0
int validCount = 0
 
for i = 0 to maCount - 1
    float fraction = float(i) / float(maCount - 1)
 
    float rawLength = minLength * math.pow(float(maxLength) / float(minLength), fraction)
    int length = math.max(1, int(math.round(rawLength)))
 
    if bar_index + 1 >= length
        float previousCum = length == bar_index + 1 ? 0.0 : cumulativeClose[length]
        float previousWeightedCum = length == bar_index + 1 ? 0.0 : cumulativeWeightedClose[length]
 
        float windowSum = cumulativeClose - previousCum
        float weightedWindowSum = cumulativeWeightedClose - previousWeightedCum
 
        // Convert absolute bar weights into 1..length.
        float weightedSum = weightedWindowSum - (bar_index + 1.0 - length) * windowSum
 
        float denominator = length * (length + 1.0) / 2.0
        float wmaValue = weightedSum / denominator
 
        compositeSum += wmaValue
        validCount += 1
 
float compositeWMA = validCount == maCount ? compositeSum / maCount : na
 
// ─────────────────────────────────────────────────────
// ATR FILTER
// ─────────────────────────────────────────────────────
float atr = ta.atr(atrLength)
float buffer = useFilter ? atr * atrMultiplier : 0.0
 
float upperBand = compositeWMA + buffer
float lowerBand = compositeWMA - buffer
 
// ─────────────────────────────────────────────────────
// CONFIRMED TREND STATE
// ─────────────────────────────────────────────────────
// 1 = LONG
// -1 = CASH
// 0 = Uninitialised
 
var int trendState = 0
 
bool longSignal = false
bool cashSignal = false
 
if barstate.isconfirmed and inPeriod and not na(buffer) and not na(compositeWMA)
    int newState = trendState
 
    if close > upperBand
        newState := 1
    else if close < lowerBand
        newState := -1
 
    longSignal := newState == 1 and trendState != 1
    cashSignal := newState == -1 and trendState == 1
 
    trendState := newState
 
// ─────────────────────────────────────────────────────
// RSI REVERSION MODULE
// ─────────────────────────────────────────────────────
// RSI does not influence WMA consensus signals.
 
float rsiValue = ta.rsi(close, rsiLength)
 
// Trigger only when RSI first crosses below the threshold.
bool rsiCrossBelow = ta.crossunder(rsiValue, rsiLower)
bool reversionSignal = rsiCrossBelow and barstate.isconfirmed and inPeriod
 
// ─────────────────────────────────────────────────────
// TREND COLOUR
// ─────────────────────────────────────────────────────
color trendColour = trendState == 1 ? col_up1 : trendState == -1 ? col_dn1 : color.gray
 
// ─────────────────────────────────────────────────────
// COMPOSITE WMA
// ─────────────────────────────────────────────────────
plot(inPeriod and showAverage ? compositeWMA : na, title="Composite WMA", color=trendColour, linewidth=3, style=plot.style_linebr)
 
// ─────────────────────────────────────────────────────
// CUSTOM COLOURED CANDLES
// ─────────────────────────────────────────────────────
plotcandle(
     colourBars and inPeriod ? open : na,
     colourBars and inPeriod ? high : na,
     colourBars and inPeriod ? low : na,
     colourBars and inPeriod ? close : na,
     title="MACE Trend Candles",
     color=trendColour,
     wickcolor=trendColour,
     bordercolor=trendColour)
 
barcolor(colourBars and inPeriod ? trendColour : na)
 
// ─────────────────────────────────────────────────────
// BACKGROUND
// ─────────────────────────────────────────────────────
bgcolor(colourBackground and inPeriod ? color.new(trendColour, 85) : na)
 
// ─────────────────────────────────────────────────────
// ATR BANDS
// ─────────────────────────────────────────────────────
plot(inPeriod and useFilter and showATRBands ? upperBand : na, title="Upper ATR Band", color=col_up2, linewidth=1, style=plot.style_linebr)
 
plot(inPeriod and useFilter and showATRBands ? lowerBand : na, title="Lower ATR Band", color=col_dn2, linewidth=1, style=plot.style_linebr)
 
// ─────────────────────────────────────────────────────
// PROBABLE REVERSION LABEL
// ─────────────────────────────────────────────────────
plotshape(
     showReversion and reversionSignal,
     title="Probable Reversion",
     style=shape.labelup,
     location=location.belowbar,
     color=color_reversion,
     text="PROBABLE REVERSION",
     textcolor=color.white,
     size=size.small)
 
// ─────────────────────────────────────────────────────
// DATA WINDOW
// ─────────────────────────────────────────────────────
plot(rsiValue, title="RSI Value", display=display.data_window)
 
// ─────────────────────────────────────────────────────
// ALERTS
// ─────────────────────────────────────────────────────
alertcondition(longSignal, title="Valence MACE LONG", message="Valence MACE: LONG confirmed on {{ticker}} at {{close}}")
 
alertcondition(cashSignal, title="Valence MACE CASH", message="Valence MACE: CASH confirmed on {{ticker}} at {{close}}")
 
alertcondition(reversionSignal, title="Valence MACE Probable Reversion", message="Valence MACE: RSI entered an oversold condition. Potential mean-reversion opportunity on {{ticker}} at {{close}}")

← 返回列表