Strategy vault / Mean reversion
RSI based Stop Loss and Take Profit Strategy
This strategy designs an automated stop loss and take profit trading strategy based on the Relative Strength Index (RSI) indicator. When the RSI indicator crosses above the overbought line or crosses below the…

Overview
This strategy designs an automated stop loss and take profit trading strategy based on the Relative Strength Index (RSI) indicator. When the RSI indicator crosses above the overbought line or crosses below the oversold line, the strategy will open long or short positions respectively. At the same time, the strategy will automatically set the stop loss price and take profit price based on the opening price and the preset stop loss percentage and take profit percentage.
Strategy Logic
This strategy uses the RSI indicator to determine overbought and oversold conditions in the market. When the RSI drops below the lower point (default 30), the market is considered oversold and a long position is opened. When the RSI rises above the upper point (default 70), the market is considered overbought and a short position is opened.
After opening long or short, the strategy automatically sets the stop loss price and take profit price based on the stop loss percentage (default 5%) and take profit percentage (default 10%). For example, after opening long, the stop loss price is (1 - stop loss percentage) * entry price, and take profit price is (1 + take profit percentage) * entry price.
Advantage Analysis
The biggest advantage of this strategy is that it can automatically set stop loss and take profit to mitigate trading risks. Stop loss helps limit losses and take profit allows locking in profits. At the same time, RSI is a mature technical indicator that can effectively identify overbought and oversold conditions.
Risk Analysis
There are also some risks with this strategy. RSI signals may be wrong sometimes, leading to unnecessary losses. In addition, triggered stop loss or take profit could also result in losing some profits. The stop loss and take profit percentages need to be set carefully - too loose may fail to control risks effectively while too tight may result in unnecessary stop loss.
These risks could be reduced by optimizing RSI parameters or adjusting stop loss/take profit percentages. Also, incorporating other indicators to confirm signals can improve accuracy of trading decisions.
Strategy Optimization
The strategy can be optimized in the following aspects:
Optimize RSI parameters to find the best combination
Test different stop loss and take profit percentage settings
Add other indicators to filter trading signals
Incorporate trend determination rules to avoid false signals in ranging markets
Optimize entry timing, set up a trailing stop to lock in profits
Conclusion
This strategy designs a simple and practical stop loss and take profit strategy based on the RSI indicator. The logic is clear and easy to implement, with automated stop loss and take profit to control risks. Attention is needed on parameters and rules optimization to prevent risks associated with incorrect RSI signals. Overall, it provides a good idea for quantitative trading and is worth further research and optimization.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 14 | length |
| v_input_2 | 35 | overSold |
| v_input_3 | 65 | overBought |
| v_input_4 | 5 | Stop Loss (%) |
| v_input_5 | 10 | Take Profit (%) |
Source Pine Script
/*backtest
start: 2023-12-29 00:00:00
end: 2024-01-28 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("twelve12 first RSI remix", overlay=true)
length = input(14)
overSold = input(35)
overBought = input(65)
stopLossPercent = input(5, title="Stop Loss (%)") / 100
takeProfitPercent = input(10, title="Take Profit (%)") / 100
price = close
vrsi = ta.rsi(price, length)
co = ta.crossover(vrsi, overSold)
cu = ta.crossunder(vrsi, overBought)
if (not na(vrsi))
if (co)
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
if (cu)
strategy.entry("RsiSE", strategy.short, comment="RsiSE")
// Calculate stop loss and take profit levels for long and short positions
longStopLoss = strategy.position_avg_price * (1 - stopLossPercent)
longTakeProfit = strategy.position_avg_price * (1 + takeProfitPercent)
shortStopLoss = strategy.position_avg_price * (1 + stopLossPercent)
shortTakeProfit = strategy.position_avg_price * (1 - takeProfitPercent)
// Set stop loss and take profit for long position
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-29. 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.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/RSI-based-Stop-Loss-and-Take-Profit-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- ATR and EMA based Dynamic Take Profit and Stop Loss Adaptive StrategyPine Script · Mean reversion · 1h
- Adaptive Mean Reversion Trading Strategy Based on Chande Momentum OscillatorPine Script · Mean reversion · 1d
- Adaptive RSI Oscillator Dynamic Trading Strategy with Threshold OptimizationPine Script · Mean reversion · 1d
- Advanced 15 Minute Chart Trading Signal StrategyPine Script · Mean reversion · 3h
- Advanced Fibonacci Retracement Trend Following and Reversal Trading StrategyPine Script · Mean reversion · 3h
- Advanced Momentum EMA Trend Following with RSI Overbought Oversold Trading StrategyPine Script · Mean reversion · 1d