0bps

Strategy vault / Trend following

RSI Trend Following Bull Strategy

This strategy is a trend following bull strategy that uses RSI indicator to determine trend and MACD indicator to enter the market. It also incorporates EMA line as a trend filter and emergency stop loss to control…

Author
ChaoZhang
Complexity
Starter · 52 lines
Parameters
6
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy is a trend following bull strategy that uses RSI indicator to determine trend and MACD indicator to enter the market. It also incorporates EMA line as a trend filter and emergency stop loss to control risks.

Strategy Logic

The strategy mainly relies on RSI indicator to determine trend direction. When RSI crosses above the set long line (default 21), it is considered that the market may reverse to an uptrend. At this time if MACD is already in a downtrend, it can be judged that it is at a reversal point, which is a good opportunity to go long.

In addition, the strategy also introduces EMA line (default 200 periods) as a trend filter. Only when price is above EMA line will long trade be considered. This can effectively filter fake reversals when trend is unclear or declining.

On the stop loss side, the strategy also sets regular stop loss line and emergency stop loss line. When RSI crosses below regular stop loss line (default 86), close position; if price drops sharply and RSI crosses below emergency stop loss line (default 73), close position unconditionally to control maximum loss.

Advantage Analysis

  • Use RSI to identify reversal points, with MACD to filter false entries.
  • Introduce EMA line to determine major trend.
  • Use both regular stop loss and emergency stop loss to control risks.

Risk Analysis

  • RSI reversal signals may have misjudgments.
  • EMA line cannot respond timely to major trend changes.
  • Single stop loss indicator may stop profitable trades prematurely.

Optimization Directions

  • Volume indicators or bull/bear candles ratio can be introduced as auxiliary judgment tools to improve entry accuracy.
  • Moving average system can be adjusted to dynamically track most recent N days trend.
  • Add more advanced stop loss mechanisms like moving stop loss or statistical stop loss to make stop loss more flexible.

Summary

In summary, this strategy is a relatively traditional trend following bull strategy. It identifies reversal points with RSI, filters misjudgments with MACD, determines major trend with EMA and controls risks with stop loss. The strategy is quite simple and intuitive, easy to understand, and has some advantage in judging market reversals, making it a good starting point strategy for algo trading. But there are still large rooms for further improvements by optimizing entry signals, trend judges and stop loss mechanisms.

Parameters

NameDefaultWhat it controls
v_input_1trueUse Emergency Exit?
v_input_221RSI Long Cross
v_input_386RSI Close Long Position
v_input_473RSI Emergency Close Long Position
v_input_5trueUse EMA Trend Filter
v_input_6200EMA Length for Trend Filter

Source Pine Script

/*backtest
start: 2022-12-28 00:00:00
end: 2024-01-03 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dravitch
//@version=4
strategy("RSI - BULL RUN (Improved)", overlay=true)

// Input
UseEmergency = input(true, "Use Emergency Exit?")
RSIlong = input(21, "RSI Long Cross")
RSIcloseLong = input(86, "RSI Close Long Position")

EmergencycloseLong = input(73, "RSI Emergency Close Long Position")
UseEMAFilter = input(true, "Use EMA Trend Filter")
EMAlength = input(200, "EMA Length for Trend Filter")  // Utiliser 200 pour SMMA

// RSI
rsiValue = rsi(close, 14)

// MACD
[macdLine, signalLine, _] = macd(close, 12, 26, 9)

// EMA Trend Filter
emaTrend = sma(close, EMAlength)  // Utiliser sma pour la SMMA (Simple Moving Average)

// Conditions pour les trades longs
trendUp = close > emaTrend
trendDown = close < emaTrend
longCondition = crossover(rsiValue, RSIlong) and trendDown or crossunder(macdLine, signalLine) and crossover(rsiValue, RSIlong)
longCloseCondition = crossunder(rsiValue, RSIcloseLong) and trendUp
emergencyLongCondition = crossunder(rsiValue, EmergencycloseLong) 

// Plots
plot(rsiValue, color=color.white, linewidth=2, title="RSI")

// Strategy
if (longCondition)
    strategy.entry("Long", strategy.long, alert_message='RSI Long Cross: LONG')
if (longCloseCondition)
    strategy.close("Long", alert_message='RSI Close Long Position')
if (emergencyLongCondition and UseEmergency)
    strategy.close("Long", alert_message='RSI Emergency Close Long')

// Plot EMA Trend Filter in a separate pane
plot(emaTrend, color=color.rgb(163, 0, 122), title="EMA Trend Filter", linewidth=2, style=plot.style_line, transp=0)
hline(0, "Zero Line", color=color.gray)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-04. 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/RSI-Trend-Following-Bull-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