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…

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:
- Using a dual moving average crossover system allows flexible tuning of entry and exit criteria to adapt to market changes
- The SMA lines themselves can filter out some noise and generate more reliable trading signals
- Customizable SMA parameter combinations allow parameter optimization for different products
Risk Analysis
There are also some risks associated with this strategy:
- SMA crossover signals may lag and fail to generate timely signals around turning points
- Improper selection of SMA parameters can lead to too many false signals
- 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:
- Test more SMA parameter combinations to find the optimal parameters
- Add volume confirmation to avoid wrong signals during violent price fluctuations
- Combine other indicators (e.g. MACD, RSI) to filter SMA crossover signals
- 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
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 377 | smaB1 |
| v_input_2 | 200 | smaB2 |
| v_input_3 | 377 | smaS1 |
| v_input_4 | 200 | smaS2 |
| v_input_5 | true | From Month |
| v_input_6 | true | From Day |
| v_input_7 | 2020 | From Year |
| v_input_8 | true | To Month |
| v_input_9 | true | To Day |
| v_input_10 | 9999 | To 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.
/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- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- Fixed Range Volume Profile And Anchored VWAP Trend Identification Strategy With Dynamic Stop LossPine Script · Trend following · 1h
- Five Minute Triple Confirmation Trend Trading Strategy with Risk Management SystemPine Script · Trend following · 1m
- Moving Average Crossover Strategy 427164Pine Script · Trend following · 10m
- Multi Indicator Fusion Volatility Capture Adaptive Trend Following StrategyPine Script · Trend following · 1h
- Multi Indicator Cross Trend Tracking and Volume Price Combined Adaptive Trading StrategyPine Script · Trend following · 1d
- Multi Timeframe Volume and Trend Confluence Trading StrategyPine Script · Trend following · 1d