0bps

Strategy vault / Breakout

Multi Period Dynamic Moving Average Strategy

This strategy dynamically selects different types of moving averages across multiple timeframes to generate trading signals. The strategy allows selecting from SMA, EMA, TEMA, WMA and HMA moving averages, with…

Author
ChaoZhang
Complexity
Intermediate · 83 lines
Parameters
12
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

This strategy dynamically selects different types of moving averages across multiple timeframes to generate trading signals.

Strategy Logic

The strategy allows selecting from SMA, EMA, TEMA, WMA and HMA moving averages, with customizable period lengths. Different types of moving averages will be plotted dynamically based on selection. It goes long when the closing price breaks above the moving average, and goes short when the closing price breaks below.

Specifically, the strategy first defines the backtest period based on input parameters. It then calculates five types of moving averages:

  • SMA Simple Moving Average
  • EMA Exponential Moving Average
  • TEMA Triple Exponential Moving Average
  • WMA Weighted Moving Average
  • HMA Hull Moving Average

The corresponding moving average is plotted based on selection. It goes long when the closing price is above the moving average, and goes short when below.

By combining different types of moving averages, the strategy can smooth price data and filter out market noise to generate more reliable trading signals. Customizable period lengths allow trading different trends across timeframes.

Advantages

  • Combines multiple moving averages for greater reliability
  • Customizable periods suit different trading timeframes
  • Dynamic switching of averages allows flexible optimization
  • Simple and intuitive trend following suitable for beginners

Risks

  • Lagging of moving averages may miss trend turning points
  • Overfitting with fixed parameters, underperformance in live trading likely
  • Aggressive long/short signals impact capital usage efficiency

Risks can be reduced by:

  • Adding other indicators to determine entries more precisely
  • Real-trade optimization of parameters for different market regimes
  • Optimizing position sizing based on account size and risk management

Enhancement Opportunities

The strategy can be improved in several aspects:

  1. Add other filters for more stable signals

    e.g. volume indicators to avoid false breakouts without volume confirmation.

  2. Optimize entry and exit logic

    Set price channels and stop losses to reduce unnecessary losses.

  3. Dynamic moving average periods

    Use longer periods in strong trends and shorter periods during consolidations.

  4. Improve money management

    Adjust position sizes based on drawdowns and profit taking.

Conclusion

The strategy combines various moving averages across timeframes to generate relatively stable trend following effects. With ample room for optimizations in entries, exits, parameters and money management, it can be improved for better real-world performance.

Parameters

NameDefaultWhat it controls
v_input_1100000000Buy quantity
v_input_22017Backtest Start Year
v_input_3trueBacktest Start Month
v_input_4trueBacktest Start Day
v_input_5falseBacktest Start Hour
v_input_6falseBacktest Start Minute
v_input_72099Backtest Stop Year
v_input_8trueBacktest Stop Month
v_input_930Backtest Stop Day
v_input_10trueColor Background?
v_input_110Select MA: SMA | EMA | TEMA | WMA | HMA
v_input_127Period

Source Pine Script

/*backtest
start: 2022-10-20 00:00:00
end: 2023-10-26 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
strategy("MA_strategy ", shorttitle="MA_strategy", overlay=true, initial_capital=100000)

qty = input(100000000, "Buy quantity")

testStartYear = input(2017, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testStartHour = input(0, "Backtest Start Hour")
testStartMin = input(0, "Backtest Start Minute")

testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,testStartHour,testStartMin)

testStopYear = input(2099, "Backtest Stop Year")
testStopMonth = input(1, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)


testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false


ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])


len1 = input(7, minval=1, title="Period")

s=sma(close,len1)

e=ema(close,len1)


xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3


f_hma(_src, _length)=>
    _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

h = f_hma(close, len1)

w = wma(close, len1)

ma = ma1 == "SMA"?s:ma1=="EMA"?e:ma1=="WMA"?w:ma1=="HMA"?h:ma1=="TEMA"?t:na

buy= close>ma
sell= close<ma

alertcondition(buy, title='buy', message='buy')
alertcondition(sell, title='sell', message='sell')

ordersize=floor(strategy.equity/close)

if testPeriod()
    strategy.entry("long",strategy.long,ordersize,when=buy)
    strategy.close("long", when = sell )


    
  







Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-27. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Multi-Period-Dynamic-Moving-Average-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open