0bps

Strategy vault / Trend following

MA and RSI based Trend Following Swing Trading Strategy

The MA and RSI-based Trend Following Swing Trading Strategy is a quantitative trading strategy that combines moving averages and the Relative Strength Index (RSI) indicator. The strategy aims to capture medium to…

Author
ChaoZhang
Complexity
Intermediate · 55 lines
Parameters
7
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Strategy Overview

The MA and RSI-based Trend Following Swing Trading Strategy is a quantitative trading strategy that combines moving averages and the Relative Strength Index (RSI) indicator. The strategy aims to capture medium to long-term market trends while using the RSI indicator to determine overbought and oversold market conditions, optimizing entry and exit points.

Strategy Principles

The core principles of the strategy are as follows:

  1. Calculate two moving averages (MA) with different periods, namely the fast MA and the slow MA. When the fast MA crosses above the slow MA, it indicates an upward trend in the market; when the fast MA crosses below the slow MA, it indicates a downward trend.

  2. Calculate the RSI indicator to determine overbought and oversold market conditions. When the RSI is above the overbought threshold, the market is considered overbought; when the RSI is below the oversold threshold, the market is considered oversold.

  3. Combine the signals from MA and RSI. When the market is in an uptrend and the RSI is not overbought, open a long position; when the market is in a downtrend and the RSI is not oversold, open a short position.

  4. Set stop loss and take profit levels to control risk and lock in profits. The stop loss level is calculated based on the latest closing price and the stop loss percentage, while the take profit level is calculated based on the latest closing price, stop loss percentage, and risk-reward ratio.

  5. Close the position when the price reaches the stop loss or take profit level.

Strategy Advantages

  1. Trend Following: The strategy uses MA crossovers to identify market trends, effectively capturing medium to long-term price trends.

  2. Overbought and Oversold Detection: By incorporating the RSI indicator, the strategy further optimizes entry timing based on trend identification, avoiding entering positions in overbought or oversold regions.

  3. Risk Control: The strategy sets explicit stop loss and take profit levels, strictly controlling the risk exposure of each trade.

  4. Parameter Flexibility: The key parameters of the strategy, such as MA periods, RSI period, overbought and oversold thresholds, stop loss percentage, and risk-reward ratio, are provided as input parameters, allowing users to adjust them according to their needs.

Strategy Risks

  1. Parameter Risk: The performance of the strategy is sensitive to parameter selection. Different parameter settings may lead to significant differences in strategy performance. Therefore, in practical application, thorough backtesting and optimization of parameters are required.

  2. Trend Identification Risk: The strategy primarily relies on MA crossovers to identify trends. However, in certain market conditions (such as ranging markets or trend turning points), MA crossovers may produce false signals or lag behind.

  3. Black Swan Events: The strategy is mainly built based on historical data and may not be able to respond promptly to sudden and extreme market events (such as major political events or natural disasters).

Optimization Directions

  1. Introduce additional technical indicators, such as Bollinger Bands and MACD, to improve the accuracy and robustness of trend identification.

  2. Consider incorporating market sentiment analysis, such as using big data analysis of market sentiment to assist in trend judgment and position adjustment.

  3. Perform more comprehensive and detailed parameter optimization. Intelligent optimization methods, such as genetic algorithms, can be used to find the optimal parameter combination.

  4. Add position management and money management modules to the strategy. Dynamically adjust positions based on market volatility and account profit and loss to further control risk.

Summary

The MA and RSI-based Trend Following Swing Trading Strategy is a classic quantitative trading strategy that uses MA crossovers to identify market trends and the RSI indicator to optimize entry and exit points. The strategy has a clear logic, is easy to implement and optimize, and can effectively capture medium to long-term market trends while controlling a certain level of risk. However, the strategy is sensitive to parameter selection and requires thorough backtesting and optimization in practical application. Moreover, the strategy is mainly built on technical indicators and may not be sufficient to respond to extreme market events. In the future, consideration can be given to introducing more technical indicators and market sentiment analysis, as well as adding position management and money management modules to further enhance the robustness and profitability of the strategy. Overall, the strategy provides a basic quantitative trading framework that can serve as a foundation for further development and optimization.

Parameters

NameDefaultWhat it controls
v_input_15050-Day MA
v_input_2200200-Day MA
v_input_314RSI Length
v_input_470RSI Overbought
v_input_530RSI Oversold
v_input_62Risk/Reward Ratio
v_input_72Stop Loss (%)

Source Pine Script

/*backtest
start: 2024-02-20 00:00:00
end: 2024-03-21 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Swing Trading Strategy", overlay=true)

// Inputs
ma_fast_length = input(50, "50-Day MA")
ma_slow_length = input(200, "200-Day MA")
rsi_length = input(14, "RSI Length")
rsi_overbought = input(70, "RSI Overbought")
rsi_oversold = input(30, "RSI Oversold")
risk_reward_ratio = input(2.0, "Risk/Reward Ratio")
stop_loss_percent = input(2.0, "Stop Loss (%)")

// Moving Averages
ma_fast = ta.sma(close, ma_fast_length)
ma_slow = ta.sma(close, ma_slow_length)

// RSI
rsi = ta.rsi(close, rsi_length)

// Trend Identification
bullish_trend = ta.crossover(ma_fast, ma_slow)
bearish_trend = ta.crossunder(ma_fast, ma_slow)

// Entry Conditions
long_entry = bullish_trend and close > ma_fast and rsi < rsi_overbought
short_entry = bearish_trend and close < ma_fast and rsi > rsi_oversold

// Stop Loss and Take Profit Calculations
long_sl = close * (1 - stop_loss_percent / 100)
short_sl = close * (1 + stop_loss_percent / 100)
long_tp = close * (1 + (stop_loss_percent / 100) * risk_reward_ratio)
short_tp = close * (1 - (stop_loss_percent / 100) * risk_reward_ratio)

// Strategy Execution
if (long_entry)
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit Long", "Long", stop=long_sl, limit=long_tp)

if (short_entry)
    strategy.entry("Short", strategy.short)
    strategy.exit("Exit Short", "Short", stop=short_sl, limit=short_tp)

// Plotting
plot(ma_fast, "50-Day MA", color=color.blue)
plot(ma_slow, "200-Day MA", color=color.red)
hline(rsi_overbought, "Overbought", color=color.red)
hline(rsi_oversold, "Oversold", color=color.green)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-03-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/MA-and-RSI-based-Trend-Following-Swing-Trading-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