Strategy vault / Mean reversion
RSI Moving Average Trailing Stop Strategy
This strategy combines RSI and moving averages for trend bias and adds trailing stops for risk management. It aims to follow trends with adaptive exits. Strategy Logic: 1. Calculate RSI to judge overbought/oversold…
This strategy combines RSI and moving averages for trend bias and adds trailing stops for risk management. It aims to follow trends with adaptive exits.
Strategy Logic:
Calculate RSI to judge overbought/oversold levels. RSI above 50 signals bullishness.
Compute fast and slow moving averages, golden cross signals bull trend.
Consistent RSI rise also signals long entry.
After entry, set trailing stop loss and take profit lines.
Stop loss trail below price, take profit trail above.
Exit when price hits stop or take profit.
Advantages:
RSI avoids chasing tops and bottoms.
Moving averages identify trend direction. Combination improves accuracy.
Trailing stops/profits adjust dynamically to price.
Risks:
RSI and MAs prone to false signals in ranging markets.
Trailing stop width requires prudent calibration, too wide or too narrow problematic.
Unable to limit loss size, risks large losing trades.
In summary, this strategy combines RSI and MAs then uses trailing stops for risk management. With robust optimization and risk controls, it can achieve good results.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | true | Show Date Range |
| v_input_2 | 14 | length |
| v_input_float_1 | 2 | Trail Long Loss (%) |
| v_input_float_2 | true | Trail Short Loss (%) |
Source Pine Script
/*backtest
start: 2022-09-06 00:00:00
end: 2023-09-12 00:00:00
period: 4d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("RSI and MA Strategy with Trailing Stop Loss and Take Profit",
overlay=true,
initial_capital=1000,
process_orders_on_close=true,
default_qty_type=strategy.percent_of_equity,
default_qty_value=100,
commission_type=strategy.commission.percent,
commission_value=0.1)
showDate = input(defval=true, title='Show Date Range')
timePeriod = time >= timestamp(syminfo.timezone, 2022, 1, 1, 0, 0)
notInTrade = strategy.position_size <= 0
//==================================Buy Conditions============================================
//RSI
length = input(14)
rsi = ta.rsi(close, length)
buyCondition1 = rsi > 50
//MA
SMA9 = ta.sma(close, 9)
SMA50 = ta.sma(close, 50)
SMA100 = ta.sma(close, 100)
plot(SMA9, color = color.green)
plot(SMA50, color = color.orange)
plot(SMA100, color = color.blue)
buyCondition2 = SMA9 > SMA50//ta.crossover(SMA9, SMA100)
//RSI Increase
increase = 5
buyCondition3 = (rsi > rsi[1] + increase)
if (buyCondition1 and buyCondition2 and buyCondition3 and timePeriod) //and buyCondition
strategy.entry("Long", strategy.long)
//==================================Sell Conditions============================================
//Trailing Stop Loss and Take Profit
longTrailPerc = input.float(title='Trail Long Loss (%)', minval=0.0, step=0.1, defval=2) * 0.01
shortTrailPerc = input.float(title='Trail Short Loss (%)', minval=0.0, step=0.1, defval=1) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - longTrailPerc)
math.max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + shortTrailPerc)
math.min(stopValue, shortStopPrice[1])
else
999999
strategy.exit(id="Exit", stop = longStopPrice, limit = shortStopPrice)Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-13. 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-Moving-Average-Trailing-Stop-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
- Adaptive Mean Reversion Trading Strategy Based on Chande Momentum OscillatorPine Script · Mean reversion · 1d
- Advanced 15 Minute Chart Trading Signal StrategyPine Script · Mean reversion · 3h
- Advanced Momentum EMA Trend Following with RSI Overbought Oversold Trading StrategyPine Script · Mean reversion · 1d
- Advanced Quantitative Trading Strategy Combining RSI Divergence and Moving AveragesPine Script · Mean reversion · 1h
- Advanced Strategy with Volume and Price Pullback Multiple Take ProfitPine Script · Mean reversion · 1h
- BONK Multi Factor Trading StrategyPine Script · Mean reversion · 1d