0bps

Strategy vault / Trend following

Trend Following Strategy Based on Dynamic Moving Average

This strategy is based on the dynamic moving average indicator to track the price trend in real time and generate trading signals when the moving average is broken through. The advantage of this strategy lies in its…

Author
ChaoZhang
Complexity
Intermediate · 72 lines
Parameters
8
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy is based on the dynamic moving average indicator to track the price trend in real time and generate trading signals when the moving average is broken through. The advantage of this strategy lies in its simple parameter settings, clear signal rules, and suitability for medium-to-long-term holding.

Strategy Logic

This strategy utilizes dynamic moving average indicators including ALMA, EMA, SMA and more. The principle is to go long when the price breaks above the moving average and go short when it breaks below. That is, the moving average serves as a barometer for the price trend, and signals can be generated when a trend reversal occurs.

Specifically, the strategy uses moving averages formed by high and low prices. The low price MA serves as the signal line for long signals, while the high price MA serves as the line for shorts. When the closing price rises above the low price MA, go long. When the close drops below the high price MA, go short.

By judging the price trend with MA and combining with the breakout principle to generate signals, a simple and practical trend following strategy is formed.

Advantages

  • Simple parameter settings with MA indicator, easy to operate
  • Clear signal rules without false signals
  • Flexible MA types to adapt to market changes
  • Adjustable MA periods suit different trend cycles
  • Multi-timeframe signal validation improves reliability

Risks and Solutions

  • MA lag may miss some opportunities
    • Shorten MA period or use EMA
  • Large swing risks in short term
    • Widen stop loss room for flexibility
  • Long holding risks, unable to lock profit in time
    • Combine other indicators, avoid chasing highs and killing lows

Optimization Directions

  • Adjust MA type and parameters based on symbol characteristics
  • Add auxiliary indicators to improve strategy
  • Add stop loss and take profit mechanisms
  • Evaluate signal reliability across timeframes
  • Utilize machine learning to find better parameters

Conclusion

This strategy judges the trend direction with MA and generates signals based on the breakout principles. It is simple to use and suitable for medium-to-long-term holding. The parameters can also be adjusted to adapt to market changes. Risks from short-term fluctuations and long holding need to be managed with stop loss/profit taking. There is room for improvement by incorporating more indicators and finding optimal parameters through machine learning.

Parameters

NameDefaultWhat it controls
v_input_10MA Type: ALMA | EMA | WMA | HMA | VWMA | RMA | SMA
v_input_255MA Length
v_input_3_ohlc40Closing Source: ohlc4 | high | low | open | hl2 | hlc3 | hlcc4 | close
v_input_40.85Offset (alma only)
v_input_510Sigma (alma only)
v_input_6trueUse Current Resolution
v_input_71440Timeframe
v_input_8falseShow Signals ?

Source Pine Script

/*backtest
start: 2023-12-02 00:00:00
end: 2024-01-01 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy(title="Baseline Strategy - evo", shorttitle="Baseline", overlay=true)

//INPUTS
mat =               input("ALMA", "MA Type", options=["SMA", "EMA", "WMA", "HMA", "VWMA", "RMA", "ALMA"])
baseline =          input(55, title="MA Length")
src =               input(ohlc4, title="Closing Source")

offset =            input(0.85, step=0.05, title="Offset (alma only)")
sigma =             input(10, title="Sigma (alma only)")

useCurrentRes =     input(true, title="Use Current Resolution")
resCustom =         input("1440", title="Timeframe")

showsignals =       input(false, title="Show Signals ?")

//BASELINE
baselinehigh = 

 mat=="SMA" ? sma(high,baseline) : 
 mat=="EMA" ? ema(high,baseline) : 
 mat=="WMA" ? wma(high,baseline) : 
 mat=="HMA" ? wma(2*wma(high, baseline/2)-wma(high, baseline), round(sqrt(baseline))) : 
 mat=="VWMA" ? vwma(high,baseline) : 
 mat=="RMA" ? rma(high,baseline) :
 mat=="ALMA" ? alma(high, baseline, offset, sigma) : na

baselinelow = 

 mat=="SMA" ? sma(low,baseline) : 
 mat=="EMA" ? ema(low,baseline) : 
 mat=="WMA" ? wma(low,baseline) : 
 mat=="HMA" ? wma(2*wma(low, baseline/2)-wma(low, baseline), round(sqrt(baseline))) : 
 mat=="VWMA" ? vwma(low,baseline) : 
 mat=="RMA" ? rma(low,baseline) : 
 mat=="ALMA" ? alma(low, baseline, offset, sigma) : na

//RESOLUTION
res =               useCurrentRes ? timeframe.period : resCustom

mtfhigh =           security(syminfo.tickerid, res, baselinehigh)
mtflow =            security(syminfo.tickerid, res, baselinelow)

//PLOTS
plot(mtfhigh, color=color.navy, linewidth=2, transp=0, title="Baseline High")
plot(mtflow, color=color.navy, linewidth=2, transp=0, title="Baseline Low")

long =              src > mtfhigh
short =             src < mtflow

barcolor(long ? #ffe0b2 : short ? #2a2e39 : not long and not short ? #b09e82 : na, title="BaseLine BarColor")

signal = 0
signal := long ? 1 : short ? 2 : nz(signal[1])

plotshape(showsignals ? (signal != signal[1] and long ? mtflow : na) : na, title="Long", location=location.absolute, size=size.small, style=shape.labelup, text="Long", textcolor=color.black, transp=40, color=#00ff00)
plotshape(showsignals ? (signal != signal[1] and short ? mtfhigh : na) : na, title="Short", location=location.absolute, size=size.small, style=shape.labeldown, text="Short", textcolor=color.white, transp=40, color=#ff0000)

alertcondition(signal != signal[1], title="Trend Change !", message="Trend Change !")

if (long)
    strategy.entry("Long", strategy.long)
if (short)
    strategy.entry("Short", strategy.short)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-02. 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/Trend-Following-Strategy-Based-on-Dynamic-Moving-Average.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