This article introduces a quantitative trading strategy for going long based on the Relative Strength Index (RSI) and trailing stop. The strategy uses the RSI indicator to determine overbought and oversold market conditions, entering long positions when the market is oversold and closing positions when it is overbought. At the same time, the strategy employs a percentage-based trailing stop to control risk. This is a classic trend-following strategy designed to capture uptrends in strong markets.
The core of this strategy is the Relative Strength Index (RSI). RSI is a momentum oscillator used to measure the magnitude of price changes over a period of time. Its calculation formula is:
RS = Average gain over N days / Average loss over N days
RSI = 100 - 100 / (1 + RS)
where N is the time period for calculating RSI, usually set to 14.
The strategy logic is as follows:
The strategy attempts to enter positions at the beginning of a market transition from bearish to bullish, and exit at the end of a bull market, in order to capture the main uptrend.
This article presented a quantitative trading strategy for going long based on RSI and trailing stops. The strategy uses RSI overbought and oversold signals to enter and exit positions, while using percentage-based trailing stops to control risk. This is a simple and practical trend-following strategy suitable for beginners to learn. However, it also has some limitations, such as poor performance in range-bound markets and lack of flexibility in stop loss and position management. To address these shortcomings, we can optimize the strategy in aspects such as trend filtering, dynamic stop loss, position management, and long-short hedging, in order to obtain more robust returns. The development of quantitative trading strategies is a process of continuous optimization and iteration, requiring investors to constantly summarize experiences and refine the strategy in practice.
/*backtest start: 2023-03-02 00:00:00 end: 2024-03-07 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("RSI Strategy (Long)", overlay=true, initial_capital=1000, default_qty_type=strategy.percent_of_equity, default_qty_value=100) length = input( 14 ) overSold = input( 30 ) overBought = input( 70 ) price = close vrsi = ta.rsi(price, length) co = ta.crossover(vrsi, overSold) cu = ta.crossunder(vrsi, overBought) // *** Signals *** enter_long = ta.crossover(vrsi, overSold) enter_short = ta.crossunder(vrsi, overBought) close_long = ta.crossunder(vrsi, overBought) close_short = ta.crossunder(vrsi, overBought) // *** Risk management *** entry_price = close percent_diff = input(5) stop_loss_price_long = (1 - percent_diff / 100.) * entry_price stop_loss_price_short = (1 + percent_diff / 100.) * entry_price // *** Positions *** if enter_long and strategy.position_size == 0 strategy.entry("Long", strategy.long) strategy.exit("SL Long", "Long", stop = stop_loss_price_long) if enter_short and strategy.position_size == 0 strategy.entry("Short", strategy.short, qty=.001) strategy.exit("SL short", "Short", stop = stop_loss_price_short) if close_long strategy.close("Long", "Exit Long") if close_short strategy.close("Short", "Exit Short")