0bps

Strategy vault / Trend following

SuperTrend Dual Moving Average Crossover Strategy

This is a dual moving average crossover strategy based on the SuperTrend indicator. SuperTrend consists of two moving averages, their crossover acts as buy and sell signals. The strategy belongs to the trend…

Author
ChaoZhang
Complexity
Starter · 24 lines
Parameters
0
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

Overview

This is a dual moving average crossover strategy based on the SuperTrend indicator. SuperTrend consists of two moving averages, their crossover acts as buy and sell signals. The strategy belongs to the trend following category.

Strategy Logic

  1. Calculate the fast line demaFast, formula: 2*ema5 - ema(ema5,5)

  2. Calculate the slow line demaSlow, formula: 2*ema2 - ema(ema2,2)

  3. The fast line consists of 5-day EMA, more responsive to price changes; the slow line consists of 2-day EMA, lagging in response.

  4. When fast line crosses above slow line from below, generate buy signal; when crossing below from above, generate sell signal.

  5. Using crossover of two lines with different response speeds to determine trend change is a typical trend following strategy.

  6. Execute trades based on buy and sell signals.

The core logic is simple and clear. By adjusting the MA parameters it can adapt to different cycle markets, a common trend following strategy.

Advantage Analysis

  1. Using dual MA crossover to determine trend change is a simple and practical technique.

  2. Fast and slow line parameters are adjustable for optimizing different periods.

  3. Clear signals and simple execution.

  4. Complete backtest functionality to verify strategy.

  5. Intuitive visual interface showing crossover.

  6. Easy to understand logic, suitable for beginners.

Risk Analysis

  1. Dual MA crossover may have lagging signals or false signals. Can improve by adjusting parameters or adding filters.

  2. Ineffective in range-bound or choppy markets, prone to stop loss. Can add trend mechanism.

  3. Limited optimization space in backtest, real trading effect untested.

  4. Need to watch transaction cost impact on profitability.

Optimization Directions

  1. Test different MA length combinations to find optimal match.

  2. Add other indicators for signal filtering, e.g. KDJ.

  3. Add stop loss mechanism to control single trade loss amount.

  4. Add position sizing to use different percentages for different market conditions.

  5. Optimize money management, set risk metrics like profit ratio.

  6. Consider machine learning algorithms for parameter optimization or signal forecasting.

Summary

This SuperTrend dual MA strategy is a simple trend following system adaptable to different cycles. Combining with other technical indicators and risk control can further enhance stability. Easy to learn with great potential for expansions, it is a highly practical quant trading strategy.

Source Pine Script

/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-18 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

strategy(title = "SuperTrend", shorttitle = "BTC")
ema5=ta.ema(close, 5)
ema2=ta.ema(close, 2)
 
demaFast =  request.security(syminfo.tickerid, "30", 2 * ema5 - ta.ema(ema5, 5)  )

plotchar((2 * ema5 - ta.ema(ema5, 5)), "d", "", location = location.top)
plotchar(demaFast, "fast", "", location = location.top)

demaSlow  = request.security(syminfo.tickerid,"30", 2 * ema2 - ta.ema(ema2, 2)  )
plotchar(demaSlow, "slow", "", location = location.top)

buy = ta.crossover(demaSlow, demaFast)
sell = ta.crossunder(demaSlow, demaFast)
strategy.entry("BUY", strategy.long, 1, when = buy)
strategy.entry("SELL", strategy.short, 1, when = sell )

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-19. 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/SuperTrend-Dual-Moving-Average-Crossover-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