0bps

Strategy vault / Trend following

Dual SMA Trend Following Strategy

This strategy uses only two SMA lines, with the slow SMA for trend direction and fast SMA for entry signals. Combined with candlestick color determination, it generates long and short signals. The strategy follows…

Author
ChaoZhang
Complexity
Starter · 47 lines
Parameters
6
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

Overview

This strategy uses only two SMA lines, with the slow SMA for trend direction and fast SMA for entry signals. Combined with candlestick color determination, it generates long and short signals. The strategy follows medium-term trends, suitable for consolidations at highs or lows.

Strategy Logic

Two SMA lines are computed, one fast and one slow, along with the midline of the price channel. The fast line has a period of 5, while the slow line has a period of 20. Above the price channel midline is considered an uptrend, where opportunities to go long on fast line crossing above slow line are sought. Below the midline is a downtrend, where opportunities to go short on fast line crossing below slow line are sought.

In addition, candlestick body color is incorporated. In an uptrend, at least 2 consecutive red candlesticks are required after seeing the bottom, before going long when the fast line crosses above the slow line. In a downtrend, at least 2 consecutive green candles are required after seeing the top, before going short when the fast line crosses below the slow line.

Advantage Analysis

The dual SMA lines and price channel help determine trend direction, avoiding false breakouts. Candlestick color filters further eliminate false signals. Long and short signals both exist for hedging. The strategy effectively tracks medium-term trends.

Customizable parameters allow configuring long/short conditions flexibly. Backtests show decent returns in consolidations at both highs and lows.

Risk Analysis

Overreliance on SMA lines may generate excessive false signals during rangings. Price factors are considered while volume is ignored.

Tuning SMA periods or incorporating other technical indicators could filter signals. Volume indicators may also provide additional insight. Position sizing could also be optimized based on market conditions.

Optimization Directions

  1. Test different fast and slow SMA combinations to find optimal parameters.

  2. Add volume and other indicators for signal validation.

  3. Incorporate other technical indicators to form an ensemble strategy.

  4. Set dynamic position sizing to optimize capital management.

  5. Apply machine learning to predict price trends and inflection points.

  6. Optimize stop loss strategies to limit losses.

Summary

The dual SMA system for trend determination is logically clear and commonly used. But overreliance on moving averages alone tends to generate false signals, requiring other indicators for enhancement. With more qualitative and quantitative validation, the strategy would become more robust. Overall it provides a simple and reliable trend following template.

Parameters

NameDefaultWhat it controls
v_input_1truelong
v_input_2trueshort
v_input_3trueUse fast SMA
v_input_45fast SMA Period
v_input_520slow SMA Period
v_input_62Bars Q

Source Pine Script

/*backtest
start: 2023-08-20 00:00:00
end: 2023-09-19 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("Noro's Trend SMA Strategy v1.4", shorttitle = "Trend SMA str 1.4", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value=100.0, pyramiding=0)

needlong = input(true, "long")
needshort = input(true, "short")
usefastsma = input(true, "Use fast SMA")
fastlen = input(5, defval = 5, minval = 1, maxval = 50, title = "fast SMA Period")
slowlen = input(20, defval = 20, minval = 2, maxval = 200, title = "slow SMA Period")
bars = input(2, defval = 2, minval = 0, maxval = 3, title = "Bars Q")

fastsma = ema(close, fastlen)
slowsma = ema(close, slowlen)

//PriceChannel
src = ohlc4
lasthigh = highest(src, slowlen)
lastlow = lowest(src, slowlen)
center = (lasthigh + lastlow) / 2

trend = low > center ? 1 : high < center ? -1 : trend[1]

bar = close > open ? 1 : close < open ? -1 : 0
redbars = bars == 0 ? 1 : bars == 1 and bar == -1 ? 1 : bars == 2 and bar == -1 and bar[1] == -1 ? 1 : bars == 3 and bar == -1 and bar[1] == -1 and bar[2] == -1 ? 1 : 0
greenbars = bars == 0 ? 1 : bars == 1 and bar == 1 ? 1 : bars == 2 and bar == 1 and bar[1] == 1 ? 1 : bars == 3 and bar == 1 and bar[1] == 1 and bar[2] == 1 ? 1 : 0

up = trend == 1 and (low < fastsma or usefastsma == false) and redbars == 1 ? 1 : 0
dn = trend == -1 and (high > fastsma or usefastsma == false) and greenbars == 1 ? 1 : 0

colorfastsma = usefastsma == true ? red : na
plot(fastsma, color = colorfastsma, title = "Fast SMA")
plot(center, color = blue, title = "Price Channel")

longCondition = up == 1
if (longCondition)
    strategy.entry("Long", strategy.long, needlong == false ? 0 : na)

shortCondition = dn == 1
if (shortCondition)
    strategy.entry("Short", strategy.short, needshort == false ? 0 : na)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-20. 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/Dual-SMA-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