0bps

Strategy vault / Trend following

The DEMA Trend Following Strategy

The DEMA trend following strategy is designed based on the DEMA indicator. It generates buy signals when the price breaks through the lower band of the DEMA and sell signals when the price breaks through the upper…

Author
ChaoZhang
Complexity
Intermediate · 69 lines
Parameters
11
Backtest bar
30m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The DEMA trend following strategy is designed based on the DEMA indicator. It generates buy signals when the price breaks through the lower band of the DEMA and sell signals when the price breaks through the upper band. This strategy belongs to the trend following system.

Strategy Logic

This strategy uses the DEMA indicator to determine the price trend. DEMA is the Double Exponential Moving Average, which is calculated with two EMA lines and can capture price changes faster. The strategy calculates the percentage difference between the price and DEMA, and then generates trading signals.

When the percentage difference crosses above the buyper parameter, a buy signal is generated. When the percentage difference crosses below the sellper parameter, a sell signal is generated. The buyper and sellper parameters represent the strength to generate signals, which can be adjusted based on market conditions.

In addition, the strategy also sets date ranges as filter conditions. Trading signals are only generated within the specified date range.

Advantage Analysis

  • Using DEMA can capture price changes more sensitively and identify trend reversals in a timely manner.
  • Compared with SMA, DEMA has lower lagging.
  • Setting buy/sell strength parameters can control the trading frequency.
  • Adding date filters can optimize for seasonal patterns.
  • Overall, the parameter settings are reasonable and can be optimized for different market environments.

Risk Analysis

  • DEMA itself has some lagging effect and may miss short-term trend reversals.
  • There is certain lag in signal generation, entry timing is not precise.
  • The strategy relies solely on DEMA without other indicators to verify the signal reliability.
  • No stop loss is set, which can lead to large losses.

Risks can be mitigated by combining other indicators for signal verification, optimizing parameters, and adding stop loss.

Optimization Directions

  • Consider adding MA indicators for signal filtering, utilizing the trending quality of MA.
  • Test the impact of different parameters on return to find the optimal parameter combination.
  • Add stop loss strategies with reasonable stop loss percentage to control per trade loss.
  • Test the strategy on different stocks to optimize the stock pool.
  • Try various exit strategies like trend reversal, breakout etc.

Conclusion

The DEMA trend following strategy is reasonably designed with stable profitability. It successfully uses the DEMA indicator to determine trend direction and works well on various stocks and medium-to-long-term timeframes. Further improvements on parameters, additional indicators, stop loss can enhance return and risk control. This strategy has practical value for live trading but needs continuous testing and optimization for long-term stability.

Parameters

NameDefaultWhat it controls
v_input_1-1buyper
v_input_2truesellper
v_input_350Dema Length
v_input_4trueBand for OverBought
v_input_5-1Band for OverSold
v_input_62018yearfrom
v_input_72019yearuntil
v_input_86monthfrom
v_input_912monthuntil
v_input_10truedayfrom
v_input_1131dayuntil

Source Pine Script

/*backtest
start: 2023-09-16 00:00:00
end: 2023-10-16 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version= 2
strategy("DEMA PRICE DİFFERENCE Strategy ",shorttitle="DPD% STR " ,overlay=false)

buyper =input(-1)
sellper=input(1)

demalen = input(50,title="Dema Length")

e1= ema(close,demalen)
e2=ema(e1,demalen)
demaprice  =   2 * e1 - e2

price=close

demadifper =  ((price-demaprice)/price)*100



plot(demadifper, color=red)
OverDemaPer = input(1, title="Band for OverBought")
UnderDemaPer= input(-1,title="Band for OverSold")




band1 = hline(OverDemaPer)
band0 = hline(UnderDemaPer)
zeroline=0
fill(band1, band0, color=green, transp=90)








yearfrom = input(2018)
yearuntil =input(2019)
monthfrom =input(6)
monthuntil =input(12)
dayfrom=input(1)
dayuntil=input(31)



if (  crossover(demadifper,buyper)) 
    strategy.entry("BUY", strategy.long, stop=close, oca_name="TREND",  comment="BUY")
    
else
    strategy.cancel(id="BUY")


if ( crossunder(demadifper,sellper)  ) 

    strategy.entry("SELL", strategy.short,stop=close, oca_name="TREND",  comment="SELL")
else
    strategy.cancel(id="SELL")
    
    
    

Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-17. 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/The-DEMA-Trend-Following-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