Crazy MA by KingDonut

KingDonut · study · 180 行 · 点赞 33 · TradingView 原页

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

Pine Script

//@version=6
indicator("Crazy MA by KingDonut", shorttitle="Crazy MA by KD", overlay=true)
 
// ─────────────────────────────────────────────
// Inputs
// ─────────────────────────────────────────────
useCombined = input.bool(true, "Combined MA", tooltip="ON = blend all the moving averages into one line (see Combined MA Settings). OFF = use the single MA Type selected below.")
maLength = input.int(55, "MA Length", minval=1)
maType = input.string("SMA", "MA Type (when Combined is off)", options=["SMA", "EMA", "HMA", "VWMA", "ALMA", "SMMA", "WMA", "DEMA", "TEMA", "ZLEMA", "T3", "LSMA", "McGinley"])
colorUp = input.color(color.green, "Up Color")
colorFlat = input.color(color.white, "Flat Color")
colorDown = input.color(color.red, "Down Color")
 
// ALMA specific inputs
almaOffset = input.float(0.85, "ALMA Offset", minval=0, maxval=1, group="ALMA Settings")
almaSigma = input.float(6, "ALMA Sigma", minval=0, group="ALMA Settings")
 
// T3 specific input
t3VolumeFactor = input.float(0.7, "T3 Volume Factor", minval=0, maxval=1, group="T3 Settings")
 
// Combined MA settings
avgMethod = input.string("Weighted Mean", "Averaging Method", options=["Weighted Mean", "Mean", "Median", "Highest", "Lowest", "Mid-Range", "Trimmed Mean", "Geometric Mean"], group="Combined MA Settings", tooltip="How the moving averages are blended together.\n\nWeighted Mean: uses the weights below.\nMean: simple average.\nMedian: middle value.\nHighest / Lowest: the top or bottom MA.\nMid-Range: (highest + lowest) / 2.\nTrimmed Mean: mean after dropping the top and bottom MAs (see Trim %).\nGeometric Mean: multiplicative average.\n\nA weight of 0 removes that MA from every method. Weights only change the result in Weighted Mean mode.")
trimPct = input.float(20, "Trim % (Trimmed Mean)", minval=0, maxval=49, step=5, group="Combined MA Settings", tooltip="Percentage of MAs dropped from each end before averaging.")
flatTolerance = input.float(0.01, "Flat Tolerance", step=0.001, group="Combined MA Settings")
 
weightSMA = input.float(1.0, "SMA Weight", step=0.1, group="Combined MA Weights")
weightEMA = input.float(1.0, "EMA Weight", step=0.1, group="Combined MA Weights")
weightHMA = input.float(1.0, "HMA Weight", step=0.1, group="Combined MA Weights")
weightVWMA = input.float(1.0, "VWMA Weight", step=0.1, group="Combined MA Weights")
weightALMA = input.float(1.0, "ALMA Weight", step=0.1, group="Combined MA Weights")
weightSMMA = input.float(1.0, "SMMA Weight", step=0.1, group="Combined MA Weights")
weightWMA = input.float(1.0, "WMA Weight", step=0.1, group="Combined MA Weights")
weightDEMA = input.float(1.0, "DEMA Weight", step=0.1, group="Combined MA Weights")
weightTEMA = input.float(1.0, "TEMA Weight", step=0.1, group="Combined MA Weights")
weightZLEMA = input.float(1.0, "ZLEMA Weight", step=0.1, group="Combined MA Weights")
weightT3 = input.float(1.0, "T3 Weight", step=0.1, group="Combined MA Weights")
weightLSMA = input.float(1.0, "LSMA Weight", step=0.1, group="Combined MA Weights")
weightMcGinley = input.float(1.0, "McGinley Weight", step=0.1, group="Combined MA Weights")
 
// ─────────────────────────────────────────────
// MA helper calculations
// ─────────────────────────────────────────────
getDEMA(source, length) =>
    ema1 = ta.ema(source, length)
    ema2 = ta.ema(ema1, length)
    2 * ema1 - ema2
 
getTEMA(source, length) =>
    ema1 = ta.ema(source, length)
    ema2 = ta.ema(ema1, length)
    ema3 = ta.ema(ema2, length)
    3 * ema1 - 3 * ema2 + ema3
 
getZLEMA(source, length) =>
    lag = math.floor((length - 1) / 2)
    emaData = source + (source - source[lag])
    ta.ema(emaData, length)
 
getT3(source, length, volumeFactor) =>
    xe1 = ta.ema(source, length)
    xe2 = ta.ema(xe1, length)
    xe3 = ta.ema(xe2, length)
    xe4 = ta.ema(xe3, length)
    xe5 = ta.ema(xe4, length)
    xe6 = ta.ema(xe5, length)
 
    b = volumeFactor
    c1 = -b * b * b
    c2 = 3 * b * b + 3 * b * b * b
    c3 = -6 * b * b - 3 * b - 3 * b * b * b
    c4 = 1 + 3 * b + b * b * b + 3 * b * b
 
    c1 * xe6 + c2 * xe5 + c3 * xe4 + c4 * xe3
 
// ─────────────────────────────────────────────
// Calculate every MA once per bar (global scope)
// ─────────────────────────────────────────────
sma_v = ta.sma(close, maLength)
ema_v = ta.ema(close, maLength)
hma_v = ta.hma(close, maLength)
vwma_v = ta.vwma(close, maLength)
alma_v = ta.alma(close, maLength, almaOffset, almaSigma)
smma_v = ta.rma(close, maLength)
wma_v = ta.wma(close, maLength)
dema_v = getDEMA(close, maLength)
tema_v = getTEMA(close, maLength)
zlema_v = getZLEMA(close, maLength)
t3_v = getT3(close, maLength, t3VolumeFactor)
lsma_v = ta.linreg(close, maLength, 0)
 
// McGinley Dynamic
var float mcg = na
if na(mcg)
    mcg := ta.ema(close, maLength)
else
    mcg := mcg + (close - mcg) / (maLength * math.pow(close / mcg, 4))
 
// ─────────────────────────────────────────────
// Combined MA
// ─────────────────────────────────────────────
getCombinedMA(vals, wts, method, trim) =>
    float result = na
    float wSum = 0.0
    float wTotal = 0.0
    arr = array.new_float()
 
    // Collect valid MAs (weight > 0 and not na)
    for i = 0 to array.size(vals) - 1
        v = array.get(vals, i)
        w = array.get(wts, i)
        if not na(v) and w > 0
            wSum += v * w
            wTotal += w
            array.push(arr, v)
 
    n = array.size(arr)
    if n > 0
        result := switch method
            "Weighted Mean" => wSum / wTotal
            "Mean" => array.avg(arr)
            "Median" => array.median(arr)
            "Highest" => array.max(arr)
            "Lowest" => array.min(arr)
            "Mid-Range" => (array.max(arr) + array.min(arr)) / 2
            "Trimmed Mean" =>
                sortedArr = array.copy(arr)
                array.sort(sortedArr)
                k = math.min(math.floor(n * trim / 100), math.floor((n - 1) / 2))
                array.avg(array.slice(sortedArr, k, n - k))
            "Geometric Mean" =>
                logSum = 0.0
                cnt = 0
                for j = 0 to n - 1
                    x = array.get(arr, j)
                    if x > 0
                        logSum += math.log(x)
                        cnt += 1
                cnt > 0 ? math.exp(logSum / cnt) : na
            => wSum / wTotal
    result
 
maValues = array.from(sma_v, ema_v, hma_v, vwma_v, alma_v, smma_v, wma_v, dema_v, tema_v, zlema_v, t3_v, lsma_v, mcg)
maWeights = array.from(weightSMA, weightEMA, weightHMA, weightVWMA, weightALMA, weightSMMA, weightWMA, weightDEMA, weightTEMA, weightZLEMA, weightT3, weightLSMA, weightMcGinley)
 
combinedMA = getCombinedMA(maValues, maWeights, avgMethod, trimPct)
 
// ─────────────────────────────────────────────
// Single MA (when Combined is off)
// ─────────────────────────────────────────────
singleMA = switch maType
    "SMA" => sma_v
    "EMA" => ema_v
    "HMA" => hma_v
    "VWMA" => vwma_v
    "ALMA" => alma_v
    "SMMA" => smma_v
    "WMA" => wma_v
    "DEMA" => dema_v
    "TEMA" => tema_v
    "ZLEMA" => zlema_v
    "T3" => t3_v
    "LSMA" => lsma_v
    "McGinley" => mcg
    => sma_v
 
ma = useCombined ? combinedMA : singleMA
 
// ─────────────────────────────────────────────
// Slope and colour
// ─────────────────────────────────────────────
maAngle = ma - ma[1]
 
// Combined uses angle in degrees with a flat tolerance
maColor = if useCombined
    angleDeg = math.atan(maAngle) * 180 / math.pi
    angleDeg > flatTolerance ? colorUp : angleDeg < -flatTolerance ? colorDown : colorFlat
else
    maAngle > 0 ? colorUp : maAngle < 0 ? colorDown : colorFlat
 
plot(ma, title="Crazy MA", color=maColor, linewidth=2)

← 返回列表