本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// © CryptoSwing-Systems
// Valence MACE | EMA Consensus Engine
indicator("Valence MACE | EMA Consensus Engine", overlay=true)
// ─────────────────────────────────────────────────────
// INPUTS
// ─────────────────────────────────────────────────────
groupMA = "EMA Consensus Engine"
maCount = input.int(50, "Number of EMAs", minval=2, maxval=100, group=groupMA)
minLength = input.int(10, "Shortest EMA Length", minval=1, group=groupMA)
maxLength = input.int(300, "Longest EMA Length", minval=2, 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 EMA", 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)
// ─────────────────────────────────────────────────────
// EMA CONSENSUS ENGINE
// ─────────────────────────────────────────────────────
var array<float> emaValues = array.new_float()
if barstate.isfirst
for i = 0 to maCount - 1
array.push(emaValues, na)
float compositeSum = 0.0
for i = 0 to maCount - 1
float fraction = float(i) / float(maCount - 1)
float length = minLength * math.pow(float(maxLength) / float(minLength), fraction)
float alpha = 2.0 / (length + 1.0)
float previousEMA = array.get(emaValues, i)
float currentEMA = na(previousEMA) ? close : alpha * close + (1.0 - alpha) * previousEMA
array.set(emaValues, i, currentEMA)
compositeSum += currentEMA
float compositeEMA = compositeSum / maCount
// ─────────────────────────────────────────────────────
// ATR FILTER
// ─────────────────────────────────────────────────────
float atr = ta.atr(atrLength)
float buffer = useFilter ? atr * atrMultiplier : 0.0
float upperBand = compositeEMA + buffer
float lowerBand = compositeEMA - 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)
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 EMA consensus signals.
float rsiValue = ta.rsi(close, rsiLength)
// Trigger only when RSI first crosses below the threshold.
bool reversionSignal = ta.crossunder(rsiValue, rsiLower) and barstate.isconfirmed and inPeriod
// ─────────────────────────────────────────────────────
// TREND COLOUR
// ─────────────────────────────────────────────────────
color trendColour = trendState == 1 ? col_up1 : trendState == -1 ? col_dn1 : color.gray
// ─────────────────────────────────────────────────────
// COMPOSITE EMA
// ─────────────────────────────────────────────────────
plot(inPeriod and showAverage ? compositeEMA : na, title="Composite EMA", 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}}")