0bps

Strategy vault / Trend following

SMA Moving Average Crossover Strategy 427593

This is a simple trend following crossover strategy based on SMA moving averages, suitable for higher timeframes for trading BTCUSD and other crypto pairs.

Author
ChaoZhang
Complexity
Starter · 46 lines
Parameters
5
Backtest bar
6h · swing
Tested on
BTC/USDT · futures

Overview

This is a simple trend following crossover strategy based on SMA moving averages, suitable for higher timeframes for trading BTCUSD and other crypto pairs.

Strategy Logic

The strategy is based on two SMA moving averages with different periods. One is a 10-period SMA, the other is a 100-period SMA. The strategy keeps monitoring the values of the two SMAs. When the shorter 10-period SMA crosses above the longer 100-period SMA, it signals an uptrend, and the strategy goes long. When the 10-period SMA crosses below the 100-period SMA, it signals a downtrend, and the strategy goes short.

Specifically, the strategy determines the crossover by comparing the values of the 10-period SMA and 100-period SMA. If the 10-period SMA crosses above the 100-period SMA, the longCondition is set to true. The strategy then goes long through the strategy.entry function. Conversely, if the 10-period SMA crosses below the 100-period SMA, the shortCondition is set to true. The strategy then goes short through strategy.entry.

Through this simple SMA crossover system, the strategy can capture trend reversal points and get in and out of the market in a timely manner. It goes long when the shorter SMA crosses above the longer SMA, and goes short when the shorter SMA crosses below the longer SMA.

Advantages

  1. The logic is simple and clear, easy to understand and implement, suitable for beginners.

  2. SMA crossover can effectively capture trend reversal points and enter the market in a timely manner.

  3. Moving averages can filter out market noise and identify trend directions.

  4. The SMA periods can be adjusted for different market environments. For example, shorter periods for bull market and longer periods for bear market.

  5. The strategy has been validated for a long time and works well in crypto markets.

Risks

  1. SMA crossover may lag and cause late entry and stop loss risks.

  2. Shorter SMA may generate false breakouts and cause unnecessary whipsaws.

  3. Need to set stop loss when holding positions for long term.

  4. May lead to frequent losing trades in ranging markets. Need to combine with other indicators.

  5. Inappropriate parameter settings may affect strategy performance. SMA periods need to be adjusted per market conditions.

Enhancements

  1. Combine SMA with other indicators like RSI, Bollinger Bands to improve accuracy.

  2. Add stop loss mechanisms, like SMA breakout stop loss.

  3. Dynamically adjust SMA parameters based on market conditions, shorter periods for bull market and longer periods for bear market.

  4. Use different position sizing based on crossover strength of short and long SMAs.

  5. Add re-entry rules, like re-enter when price reverts to SMA.

  6. Evaluate parameters and strategy through backtesting and paper trading.

Summary

The SMA crossover strategy has simple and clear logic, easy to understand and implement. It captures trend reversal points through crossover of two SMAs with different periods. It is a classical trend following strategy. The advantages are direct logic and clear trading signals, able to track trends effectively. The disadvantages are possible lagging entry and false breakouts. We can optimize it by introducing other indicators and stop loss mechanisms to control risks and improve practical results. With continuous optimization and verification, this strategy can become a very useful trend following strategy for crypto trading.

Parameters

NameDefaultWhat it controls
v_input_1_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_2101st MA Length
v_input_301st MA Type: SMA | EMA
v_input_41002nd MA Length
v_input_502nd MA Type: SMA | EMA

Source Pine Script

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

//@version=3
//study(title="MA Crossover Strategy", overlay = true)
// Simple MA crossover strategy with a 10/100 MA crossover)

strategy("MA Crossover Strategy", overlay=true)
src = input(close, title="Source")

price = security(syminfo.tickerid, timeframe.period, src)
ma1 = input(10, title="1st MA Length")
type1 = input("SMA", "1st MA Type", options=["SMA", "EMA"])

ma2 = input(100, title="2nd MA Length")
type2 = input("SMA", "2nd MA Type", options=["SMA", "EMA"])

price1 = if (type1 == "SMA")
    sma(price, ma1)
else
    ema(price, ma1)
    
price2 = if (type2 == "SMA")
    sma(price, ma2)
else
    ema(price, ma2)


//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=line,  title="1st MA", color=blue, linewidth=2, transp=0)
plot(series=price2, style=line, title="2nd MA", color=green, linewidth=2, transp=0)


longCondition = crossover(price1, price2)
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = crossunder(price1, price2)
if (shortCondition)
    strategy.entry("Short", strategy.short)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-22. 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/SMA-Moving-Average-Crossover-Strategy-427593.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