0bps

Strategy vault / Trend following

Dual SMA Crossover Strategy

The Dual SMA Crossover strategy generates trading signals by calculating the crossover of two SMA lines with different parameter settings. When the faster SMA line crosses above the slower SMA line, a buy signal is…

Author
ChaoZhang
Complexity
Intermediate · 50 lines
Parameters
10
Backtest bar
5m · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The Dual SMA Crossover strategy generates trading signals by calculating the crossover of two SMA lines with different parameter settings. When the faster SMA line crosses above the slower SMA line, a buy signal is generated. When the slower SMA line crosses below the faster SMA line, a sell signal is generated. The strategy uses two sets of SMA parameters at the same time, one set to determine entry points, and the other to determine exit points.

Strategy Logic

This strategy uses two sets of SMA parameters, smaB1, smaB2 for buy signals, and smaS1, smaS2 for sell signals, representing slower and faster moving averages respectively. When smaB1 crosses above smaB2, a buy signal is generated. When smaS2 crosses below smaS1, a sell signal is generated. This allows flexible adjustment of entry and exit conditions to adapt to changing market environments.

Specifically, this strategy monitors the crossover situations between the two SMA lines calculated from the close price to determine the timing of buying and selling. When the faster SMA line crosses above the slower SMA line, it is judged that the price trend is upward, so go long at this time. And when the slower SMA line crosses below the faster SMA line, the price trend turns downward, so exit long positions.

Advantage Analysis

The main advantages of this strategy are:

  1. Using a dual moving average crossover system allows flexible tuning of entry and exit criteria to adapt to market changes
  2. The SMA lines themselves can filter out some noise and generate more reliable trading signals
  3. Customizable SMA parameter combinations allow parameter optimization for different products

Risk Analysis

There are also some risks associated with this strategy:

  1. SMA crossover signals may lag and fail to generate timely signals around turning points
  2. Improper selection of SMA parameters can lead to too many false signals
  3. Signals generated in volatile market conditions may not work well

To control the above risks, methods like SMA parameter optimization, dynamic stop loss to lock in profits, etc. can be used to improve the strategy.

Optimization Directions

Some optimization directions for this strategy:

  1. Test more SMA parameter combinations to find the optimal parameters
  2. Add volume confirmation to avoid wrong signals during violent price fluctuations
  3. Combine other indicators (e.g. MACD, RSI) to filter SMA crossover signals
  4. Add stop loss strategies to lock in profits and reduce losses

Summary

The SMA Crossover strategy generates simple and effective trading signals by calculating the crossover situations between two SMA lines. The flexibility to adjust parameters makes this strategy adaptable to different products, and it is a commonly used trend following strategy. Further improvements can be made to this strategy through parameter optimization, signal filtering etc. to generate more reliable signals.

Parameters

NameDefaultWhat it controls
v_input_1377smaB1
v_input_2200smaB2
v_input_3377smaS1
v_input_4200smaS2
v_input_5trueFrom Month
v_input_6trueFrom Day
v_input_72020From Year
v_input_8trueTo Month
v_input_9trueTo Day
v_input_109999To Year

Source Pine Script

/*backtest
start: 2023-11-15 00:00:00
end: 2023-11-22 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © melihtuna

//@version=4
strategy("SMA Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000, currency=currency.USD, commission_value=0.1, commission_type=strategy.commission.percent)

smaB1 = input(title="smaB1",defval=377)
smaB2 = input(title="smaB2",defval=200)
smaS1 = input(title="smaS1",defval=377)
smaS2 = input(title="smaS2",defval=200)
smawidth = 2

plot(sma(close, smaB1), color = #EFB819, linewidth=smawidth, title='smaB1')
plot(sma(close, smaB2), color = #FF23FD, linewidth=smawidth, title='smaB2')
plot(sma(close, smaS1), color = #000000, linewidth=smawidth, title='smaS1')
plot(sma(close, smaS2), color = #c48dba, linewidth=smawidth, title='smaS2')

// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2020, title = "From Year", minval = 2017)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        
window()  => time >= start and time <= finish ? true : false 

longCondition = crossover(sma(close, smaB1),sma(close, smaB2))

if (window() and longCondition)
    strategy.entry("BUY", strategy.long)

shortCondition = crossover(sma(close, smaS2),sma(close, smaS1))

if (window() and shortCondition)
    strategy.entry("SELL", strategy.short)
    
    
    

Published by ChaoZhang on the FMZ strategy square · last modified 2023-11-23. 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-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