Strategies KB.ZH
Strategies

多指数移动平均与方向性趋势过滤交易系统-Multi-Exponential-Moving-Average-with-Directional-Trend-Filter-Trading-System

ianzeng123 · PineScript · Trend Following · 2025-04-08 10:13:36

All strategies →

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

Read the Chinese version →

Source code (57 lines)
/*backtest
start: 2025-01-01 00:00:00
end: 2025-04-07 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/

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

//@version=5
strategy("[SM-042] EMA 5-8-13 with ADX Filter", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, currency=currency.USD, commission_type=strategy.commission.percent)

// === INPUTS ===
enableLong     = input.bool(true,  title="Enable Long Trades")
enableShort    = input.bool(true,  title="Enable Short Trades")
useAdxFilter   = input.bool(false,  title="Use ADX Filter")
adxThreshold   = input.int(20,     title="ADX Threshold")

// === EMA CALCULATIONS ===
ema5  = ta.ema(close, 5)
ema8  = ta.ema(close, 8)
ema13 = ta.ema(close, 13)

// === ADX FILTER ===
[plusDI, minusDI, adxValue] = ta.dmi(14, 14)
adxCondition = adxValue > adxThreshold

// === ENTRY CONDITIONS ===
longCondition  = ta.crossover(ema5, ema8)  and enableLong  and (not useAdxFilter or adxCondition)
shortCondition = ta.crossunder(ema5, ema8) and enableShort and (not useAdxFilter or adxCondition)

// === EXIT CONDITIONS ===
longExit  = close < ema13
shortExit = close > ema13

// === STRATEGY EXECUTION ===
if (longCondition)
    strategy.entry("Long", strategy.long)

if (shortCondition)
    strategy.entry("Short", strategy.short)

if (strategy.position_size > 0 and longExit)
    strategy.close("Long")

if (strategy.position_size < 0 and shortExit)
    strategy.close("Short")

// === PLOTTING ===
plot(ema5,  title="EMA 5",  color=color.blue)
plot(ema8,  title="EMA 8",  color=color.yellow)
plot(ema13, title="EMA 13", color=color.purple)

hline(adxThreshold, "ADX Threshold", color=color.gray, linestyle=hline.style_dotted)
plot(adxValue, title="ADX", color=color.orange)