0bps

Strategy vault / Mean reversion

RSI Trading Strategy

This is an automated cryptocurrency trading strategy based on the Relative Strength Index (RSI) indicator. It calculates the RSI metric of BTC/USDT to set overbought and oversold thresholds for generating buy and…

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

IMG

Overview

This is an automated cryptocurrency trading strategy based on the Relative Strength Index (RSI) indicator. It calculates the RSI metric of BTC/USDT to set overbought and oversold thresholds for generating buy and sell signals, enabling automated long and short positions.

Strategy Principle

The core principle of this strategy is to use the RSI indicator to judge overbought and oversold market conditions. The RSI reflects the speed and magnitude of price changes with a range of 0-100. When RSI>70 the market is overbought and selling should be chosen; when RSI<30 the market is oversold and buying should be chosen.

Specifically, the strategy calculates 14-period RSI values and sets oversold line at 30 and overbought line at 70. When RSI crosses over the oversold line 30 upwards a buy signal is generated; when RSI crosses down the overbought line 70 a sell signal is generated. These two signals form long and short decisions.

In addition, protective stop losses are built in when RSI crosses back over the overbought and oversold lines for closing positions. This allows locking in profits and reducing losses.

Advantage Analysis

The biggest advantage of this strategy is using the RSI indicator to judge overbought/oversold market conditions, which is a proven and reliable trading principle. RSI can capture price reversal opportunities and provide informative signals for our decisions.

Also, the adjustable parameters provide flexibility. We can optimize the RSI period and threshold values based on changing market dynamics to improve performance. This gives us sufficient adaptivity.

Lastly, the protective stop loss mechanism effectively controls risks, also a major highlight of the strategy.

Risk Analysis

The biggest risk is that RSI signals may provide incorrect trading guidance. When there are abnormal price penetrations, RSI cannot perfectly determine overbought/oversold levels, which can lead to trading losses.

Additionally, the preset overbought/oversold thresholds may not suit all market conditions. We need to incorporate more indicators to confirm RSI signals and avoid false signals.

Finally, stop loss positioning also introduces some risks. We have to dynamically adjust stop levels based on different markets, otherwise stops may trigger prematurely or have too large loss size. This requires continuous testing and tuning.

Optimization Directions

The strategy can be improved in the following aspects:

  1. Optimize RSI parameters like period length and threshold values to find best combination

  2. Incorporate more indicators like candlestick patterns and MACD to form more reliable trade signals

  3. Refine capital management like adaptive stop loss levels and dynamic position sizing

  4. Backtest for performance under various markets and continuously improve logic

  5. Add machine learning models to aid in predicting signals

These optimizations can improve win rate, profitability, and reduce erroneous trades.

Conclusion

Overall, this RSI trading strategy utilizes the RSI indicator to determine overbought and oversold market conditions and generate trade signals accordingly. Its core principle, adjustable parameters, protective stop loss, and potential optimization directions make it a viable algorithmic trading system. However, we need to be aware of the risks like false signals and constantly test and iterate the strategy to achieve best performance. With further refinements, this RSI-based approach can become a robust tool for crypto currency trading.

Parameters

NameDefaultWhat it controls
v_input_114Longitud RSI
v_input_230Nivel de sobreventa
v_input_370Nivel de sobrecompra
v_input_420Capital inicial (USDT)

Source Pine Script

/*backtest
start: 2022-12-13 00:00:00
end: 2023-12-19 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Estrategia RSI para BTC/USDT", overlay=true)

// Parámetros de la estrategia
length = input(14, title="Longitud RSI")
oversold_level = input(30, title="Nivel de sobreventa")
overbought_level = input(70, title="Nivel de sobrecompra")
initial_capital = input(20, title="Capital inicial (USDT)")

// Cálculo del RSI
rsi_value = rsi(close, length)

// Variable para el capital actual
var float capital = na

// Inicializar el capital con el capital inicial
if barstate.isfirst
    capital := initial_capital

// Condiciones de entrada
long_signal = crossover(rsi_value, oversold_level)
short_signal = crossunder(rsi_value, overbought_level)

// Condiciones de salida
exit_long_signal = crossunder(rsi_value, overbought_level)
exit_short_signal = crossover(rsi_value, oversold_level)

// Operaciones de compra y venta
if long_signal
    strategy.entry("Compra", strategy.long)
    strategy.close("Venta", strategy.short)
    capital := strategy.equity
if short_signal
    strategy.entry("Venta", strategy.short)
    strategy.close("Compra", strategy.long)
    capital := strategy.equity

// Estilo de visualización
plot(rsi_value, title="RSI", color=color.blue)
hline(oversold_level, "Sobreventa", color=color.green)
hline(overbought_level, "Sobrecompra", color=color.red)

// Mostrar el capital actual en el gráfico
plot(capital, title="Capital", color=color.orange, linewidth=2, style=plot.style_linebr)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-20. 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-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