Strategies KB.ZH
Strategies

多指标动态趋势捕捉与均值回归策略-Multi-Indicator-Dynamic-Trend-Capture-and-Mean-Reversion-Strategy

ianzeng123 · PineScript · Trend Following · 2025-04-18 09:27:27

All strategies →

No English write-up in the source (no author description).

Read the Chinese version →

Source code (91 lines)
/*backtest
start: 2024-04-18 00:00:00
end: 2024-12-05 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"TRX_USD"}]
*/

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © phoenixtradeteam

//@version=5
strategy("Phoenix Pro Strategy", overlay=true, max_lines_count=500, max_labels_count=500)

// === INPUTS === //
// Moving Averages
emaFastLen = input.int(9, "EMA Fast Length")
emaSlowLen = input.int(21, "EMA Slow Length")
smaShortLen = input.int(20, "SMA Short Length")
smaLongLen = input.int(50, "SMA Long Length")

// RSI
rsiLen = input.int(14, "RSI Period")
rsiOB = input.int(70, "RSI Overbought")
rsiOS = input.int(30, "RSI Oversold")

// Pivot High/Low
pivotLeft = input.int(5, "Pivot Left Bars")
pivotRight = input.int(5, "Pivot Right Bars")

// ZigZag
zigzagDev = input.float(5.0, "ZigZag Deviation %", step=0.1)

// Bollinger Bands
bbLength = input.int(20, "Bollinger Band Length")
bbMult = input.float(2.0, "Bollinger Band Multiplier")

// === CALCULATIONS === //
// MAs
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
smaShort = ta.sma(close, smaShortLen)
smaLong = ta.sma(close, smaLongLen)

// RSI
rsi = ta.rsi(close, rsiLen)

// Bollinger Bands
basis = ta.sma(close, bbLength)
deviation = bbMult * ta.stdev(close, bbLength)
upperBB = basis + deviation
lowerBB = basis - deviation

// Pivots
pivotHigh = ta.pivothigh(high, pivotLeft, pivotRight)
pivotLow = ta.pivotlow(low, pivotLeft, pivotRight)

// ZigZag
var float zigzagTop = na
var float zigzagBot = na
zigzagTop := (high >= high * (1 + zigzagDev / 100)) ? high : zigzagTop
zigzagBot := (low <= low * (1 - zigzagDev / 100)) ? low : zigzagBot

// === SIGNAL CONDITIONS === //
longCond = emaFast > emaSlow and close > smaShort and rsi < 60 and close > basis
shortCond = emaFast < emaSlow and close < smaShort and rsi > 40 and close < basis

// === STRATEGY EXECUTION === //
strategy.entry("Long", strategy.long, when=longCond)
strategy.close("Long", when=shortCond)
strategy.entry("Short", strategy.short, when=shortCond)
strategy.close("Short", when=longCond)

// === PLOTS === //
plot(emaFast, title="EMA Fast", color=color.orange)
plot(emaSlow, title="EMA Slow", color=color.red)
plot(smaShort, title="SMA Short", color=color.blue)
plot(smaLong, title="SMA Long", color=color.teal)

plot(upperBB, title="BB Upper", color=color.gray)
plot(lowerBB, title="BB Lower", color=color.gray)
plot(basis, title="BB Basis", color=color.gray)

plotshape(pivotHigh, title="Resistance", location=location.abovebar, style=shape.cross, color=color.red, size=size.tiny)
plotshape(pivotLow, title="Support", location=location.belowbar, style=shape.cross, color=color.green, size=size.tiny)

plot(zigzagTop, title="ZigZag High", color=color.fuchsia, linewidth=2)
plot(zigzagBot, title="ZigZag Low", color=color.aqua, linewidth=2)

// Background based on trend
bgcolor(emaFast > emaSlow ? color.new(color.green, 85) : emaFast < emaSlow ? color.new(color.red, 85) : na, title="Trend Background")