RSI Relative Strength Index Strategy

Author: ChaoZhang, Date: 2024-04-18 16:41:27
Tags: RSI



This strategy is based on the Relative Strength Index (RSI) indicator. It generates trading signals on XAUUSD by analyzing the RSI value against predefined overbought and oversold thresholds. When the RSI value crosses below the oversold threshold, a long position is opened, and when the RSI value crosses above the overbought threshold, a short position is opened. The strategy also employs a trailing stop loss and position sizing based on a percentage of the account equity to manage risk.

Strategy Logic

  1. Calculate the RSI value for a given period.
  2. Compare the RSI value with the predefined overbought and oversold thresholds:
    • If the RSI value crosses below the oversold threshold, open a long position.
    • If the RSI value crosses above the overbought threshold, open a short position.
  3. Calculate the position size for each trade based on a certain percentage of the account equity and the predefined stop loss points.
  4. Set a downward trailing stop loss for long positions and an upward trailing stop loss for short positions.
  5. Close the position when the price reaches the trailing stop or the fixed stop loss point.


  1. The RSI indicator can effectively capture overbought and oversold market conditions, providing good entry opportunities for trades.
  2. The trailing stop loss mechanism automatically adjusts the stop loss level as the price moves in an unfavorable direction, maximizing profit protection.
  3. Position sizing based on a percentage of the account equity allows for proper allocation of funds according to the current account size, controlling the risk exposure of each trade.
  4. The strategy logic is clear and easy to understand, making it suitable for beginners to learn and apply.

Risk Analysis

  1. The RSI indicator may generate frequent and invalid trading signals in a choppy market, leading to overtrading and commission losses.
  2. Fixed RSI overbought and oversold thresholds may not adapt to different market conditions, requiring optimization and adjustment based on market characteristics.
  3. The trailing stop loss may be triggered prematurely during short-term market fluctuations, causing potentially profitable trades to be closed too early.
  4. The position sizing only considers the account equity and fixed stop loss points, without taking into account other risk factors such as price volatility, which may introduce additional risks in highly volatile markets.

Optimization Directions

  1. Combine other technical indicators or market condition judgments to confirm the RSI signals, filtering out invalid signals and improving trade quality.
  2. Implement adaptive optimization for the RSI overbought and oversold thresholds, dynamically adjusting the thresholds based on recent market volatility characteristics to adapt to different market conditions.
  3. Optimize the triggering conditions and magnitude of the trailing stop loss, such as setting a dynamic stop loss based on the ATR indicator or employing more flexible stop loss strategies like time-based or trend-based stop losses.
  4. Introduce more risk control factors into the position sizing, such as considering price volatility and trading frequency, dynamically adjusting the risk exposure of each trade to achieve more comprehensive risk management.


This strategy, based on the RSI indicator, generates trading signals on XAUUSD by capturing overbought and oversold conditions. Although the strategy logic is simple and straightforward, practical application still requires consideration of optimizing trading signals, dynamically adjusting parameters, refining the stop loss mechanism, and improving risk management to enhance the strategy’s robustness and profitability. With continuous optimization and improvement, this strategy can serve as a valuable reference and learning resource for quantitative trading strategies.

start: 2024-03-18 00:00:00
end: 2024-04-17 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("Ds_investimento", overlay=true)

// Parâmetros do RSI
rsi_length = input(7, title="Período do RSI")
rsi_overbought = input(70, title="Overbought (RSI)")
rsi_oversold = input(30, title="Oversold (RSI)")

// Parâmetros do Trailing Stop
trail_offset = input(0.005, title="Trailing Stop Offset")
stop_loss_points = input(10, title="Pontos do Stop Loss")

// Porcentagem da banca a ser arriscada por entrada
risk_percent = input(1, title="Porcentagem de Risco (%)")

// Calcula o tamanho da posição com base na porcentagem de risco, tamanho da banca e pontos de stop loss
equity = strategy.equity
risk_amount = (equity * risk_percent) / 100
lot_size = risk_amount / stop_loss_points

// Calcula o RSI
rsi_value = rsi(close, rsi_length)

// Condições de entrada e saída
long_condition = crossunder(rsi_value, rsi_oversold)
short_condition = crossover(rsi_value, rsi_overbought)

if (long_condition)
    strategy.entry("Long", strategy.long, 1)

if (short_condition)
    strategy.entry("Short", strategy.short, 1)

// Calcula o Trailing Stop para saída
trail_price_long = close * (1 - trail_offset)
trail_price_short = close * (1 + trail_offset)

// Saída Long/Trailing
strategy.exit("Exit Long/Trailing", from_entry="Long", trail_offset=trail_offset, trail_price=trail_price_long, stop=stop_loss_points)

// Saída Short/Trailing
strategy.exit("Exit Short/Trailing", from_entry="Short", trail_offset=trail_offset, trail_price=trail_price_short, stop=stop_loss_points)