Strategies KB.ZH
Strategies

多信号组合自适应交易策略-Multi-Signal-Adaptive-Trading-Strategy

ianzeng123 · PineScript · Trend Following · 2025-04-22 17:17:09

All strategies →

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

Read the Chinese version →

Source code (113 lines)
/*backtest
start: 2024-04-22 00:00:00
end: 2025-04-21 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"TRX_USD"}]
*/

//@version=5
strategy("Full‑Featured Multi‑Signal Strategy By Andi Tan", overlay=true)

// === POSITION SIZE ===
posPct = input.float(10, "Position Size (% Equity)", minval=0.1, step=0.1)

// === INPUTS SIGNALS ===
useEMA     = input.bool(true, "Enable EMA Crossover")
emaFastLen = input.int(9,     "EMA Fast Length", minval=1)
emaSlowLen = input.int(21,    "EMA Slow Length", minval=1)

useRSI     = input.bool(true, "Enable RSI Signal")
rsiLen     = input.int(14,    "RSI Length", minval=1)
rsiOB      = input.int(70,    "RSI Overbought", minval=50, maxval=100)
rsiOS      = input.int(30,    "RSI Oversold", minval=0,  maxval=50)

useMACD    = input.bool(true, "Enable MACD Signal")
macdFast   = input.int(12,    "MACD Fast Length",   minval=1)
macdSlow   = input.int(26,    "MACD Slow Length",   minval=1)
macdSig    = input.int(9,     "MACD Signal Length", minval=1)

mode       = input.string("Any", "Signal Combination", options=["Any","All"])
showArrows = input.bool(true, "Show Buy/Sell Arrows")

// === RISK MANAGEMENT ===
slPct     = input.float(1.0, "Stop‑Loss (%)", minval=0) / 100
tpPct     = input.float(2.0, "Take‑Profit (%)", minval=0) / 100

useTrail  = input.bool(true, "Enable ATR Trailing Stop")
atrLen    = input.int(14,    "ATR Length", minval=1)
trailMul  = input.float(1.5, "ATR Multiplier", minval=0.1)

// === FILTERS ===
useVolFilt  = input.bool(true, "Enable Volume Filter")
volLen      = input.int(20,   "Volume MA Length", minval=1)

useHigherTF = input.bool(true, "Enable Higher‑TF Confirmation")
higherTF    = input.string("60", "Higher‑TF Timeframe", options=["5","15","60","240","D","W"])

// === CALCULATIONS ===
// EMA crossover
emaFast = ta.ema(close, emaFastLen)
emaSlow = ta.ema(close, emaSlowLen)
emaUp   = ta.crossover(emaFast, emaSlow)
emaDown = ta.crossunder(emaFast, emaSlow)

// RSI
rsiVal  = ta.rsi(close, rsiLen)
rsiBuy  = rsiVal < rsiOS
rsiSell = rsiVal > rsiOB

// MACD
[macdLine, macdSignal, _] = ta.macd(close, macdFast, macdSlow, macdSig)
macdBuy  = ta.crossover(macdLine, macdSignal)
macdSell = ta.crossunder(macdLine, macdSignal)

// Combine base signals with if…else (bukan ternary terpecah)
var bool buyBase  = false
var bool sellBase = false
if mode == "Any"
    buyBase  := (useEMA and emaUp)   or (useRSI and rsiBuy)   or (useMACD and macdBuy)
    sellBase := (useEMA and emaDown) or (useRSI and rsiSell)  or (useMACD and macdSell)
else
    buyBase  := ((not useEMA) or emaUp)   and ((not useRSI) or rsiBuy)   and ((not useMACD) or macdBuy)
    sellBase := ((not useEMA) or emaDown) and ((not useRSI) or rsiSell)  and ((not useMACD) or macdSell)

// Volume filter
volMA = ta.sma(volume, volLen)
buyF  = buyBase  and (not useVolFilt or volume > volMA)
sellF = sellBase and (not useVolFilt or volume > volMA)

// ——— HIGHER‑TF EMA (dipanggil di top‑scope) ———
htEMA = request.security(syminfo.tickerid, higherTF, ta.ema(close, emaSlowLen))

// Final buy/sell signals
buySignal  = buyF  and (not useHigherTF or close > htEMA)
sellSignal = sellF and (not useHigherTF or close < htEMA)

// ATR untuk trailing
atrVal = ta.atr(atrLen)

// === ORDERS ===
if buySignal
    float qty = (strategy.equity * posPct/100) / close
    strategy.entry("Long", strategy.long, qty=qty)
if sellSignal
    float qty = (strategy.equity * posPct/100) / close
    strategy.entry("Short", strategy.short, qty=qty)

strategy.exit("Exit Long",  from_entry="Long",
     loss=slPct * close, profit=tpPct * close,
     trail_points = useTrail ? atrVal * trailMul : na)

strategy.exit("Exit Short", from_entry="Short",
     loss=slPct * close, profit=tpPct * close,
     trail_points = useTrail ? atrVal * trailMul : na)

// === PLOTS ===
plot(useEMA ? emaFast : na, title="EMA Fast", color=color.orange)
plot(useEMA ? emaSlow : na, title="EMA Slow", color=color.blue)

plotshape(showArrows and buySignal,  title="Buy",  location=location.belowbar,
     style=shape.arrowup,   text="BUY")
plotshape(showArrows and sellSignal, title="Sell", location=location.abovebar,
     style=shape.arrowdown, text="SELL")