Strategy vault / Trend following
ATR and RSI Based Trend Following Trading Strategy
This strategy designs a trading system with trend following function based on Average True Range (ATR) and Relative Strength Index (RSI). It can automatically identify the trend direction and has stop loss and take…
Overview
This strategy designs a trading system with trend following function based on Average True Range (ATR) and Relative Strength Index (RSI). It can automatically identify the trend direction and has stop loss and take profit features.
Strategy Logic
Calculate ATR and RSI. ATR reflects the average price volatility over a period. RSI reflects the power comparison between bulls and bears.
When ATR is greater than its moving average, it is considered a high volatility period suitable for trading.
When RSI is above the overbought line, go long. When RSI is below the oversold area, go short.
After long, use high point multiplied by a fixed ratio as trailing stop loss price. After short, use low point multiplied by a fixed ratio as trailing stop loss price.
Take profit by profit ratio.
Advantage Analysis
Trailing stop loss can maximize stop loss orders to reduce losses.
RSI can effectively judge the power of bulls and bears to avoid repeatedly opening positions in range-bound markets.
As a volatility indicator, ATR can filter out range-bound markets and only trade in trending markets.
Take profit by profit ratio can lock in some profits.
Risk Analysis
Both ATR and RSI are lagging indicators, which may lead to late entry timing. Parameters can be optimized to make the system more sensitive.
Fixed profit and loss ratio for stop loss and take profit is prone to over optimization, should be set prudently based on backtest results.
In large cycle range-bound markets, ATR may be greater than moving average for a long time, leading to over trading. Other filters can be added.
Optimization Directions
Optimize parameters of ATR and RSI to make system more sensitive.
Add MA and other indicators to determine trend direction, avoid wrongly entering range-bound markets.
Try dynamic stop loss and take profit ratios, instead of fixed settings.
Consider adding trading size control measures.
Summary
This strategy integrates the advantages of ATR and RSI indicators and designs a simple and practical trend following trading system. Further improving system stability by parameter optimization and adding filters. Overall, this strategy has strong practical value for live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 26 | atr_length |
| v_input_int_2 | 45 | atr_ma_length |
| v_input_int_3 | 15 | rsi_length |
| v_input_int_4 | 10 | rsi_entry |
| v_input_float_1 | 0.3 | atr_ma_norm_min |
| v_input_float_2 | 0.7 | atr_ma_norm_max |
| v_input_float_3 | 1.5 | trailing_percent |
Source Pine Script
/*backtest
start: 2023-09-08 00:00:00
end: 2023-10-08 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// © liwei666
//@version=5
// # ========================================================================= #
// # | Strategy |
// # ========================================================================= #
strategy(
title = "ATR_RSI_Strategy v2[liwei666]",
shorttitle = "ATR_RSI_Strategy",
overlay = true,
max_lines_count = 500,
max_labels_count = 500,
max_boxes_count = 500,
max_bars_back = 5000,
initial_capital = 10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=50, commission_type=strategy.commission.percent, pyramiding=1,
commission_value=0.05
)
// # ========================================================================= #
// # | Strategy |
// # ========================================================================= #
atr_length = input.int(26, "atr_length", minval = 6, maxval = 100, step=1)
atr_ma_length = input.int(45, "atr_ma_length", minval = 6, maxval = 100, step=1)
rsi_length = input.int(15, "rsi_length", minval = 6, maxval = 100, step=1)
rsi_entry = input.int(10, "rsi_entry", minval = 6, maxval = 100, step=1)
atr_ma_norm_min = input.float(0.3, "atr_ma_norm_min", minval = 0.1, maxval = 0.5, step=0.1)
atr_ma_norm_max = input.float(0.7, "atr_ma_norm_max", minval = 0.5, maxval = 1, step=0.1)
trailing_percent= input.float(1.5, "trailing_percent", minval = 0.1, maxval = 2, step=0.1)
var rsi_buy = 50 + rsi_entry
var rsi_sell = 50 - rsi_entry
sma_norm_h_45() =>
source = high
n = 45
sma = ta.sma(source, n)
sma_norm = (sma - ta.lowest(sma, n)) / (ta.highest(sma,n) - ta.lowest(sma, n))
sma_norm
atr_value = ta.atr(atr_length)
atr_ma = ta.sma(atr_value, atr_ma_length)
rsi_value = ta.rsi(close, length = rsi_length)
atr_ma_norm = atr_ma / close * 100
sma_norm = sma_norm_h_45()
var intra_trade_high = 0.0
var intra_trade_low = 0.0
if strategy.position_size == 0
intra_trade_high := high
intra_trade_low := low
if atr_ma_norm >= atr_ma_norm_min and atr_ma_norm <= atr_ma_norm_max
if atr_value > atr_ma
if rsi_value > rsi_buy
strategy.entry("B1", strategy.long, limit = close + 5 )
else if rsi_value < rsi_sell
strategy.entry("S1", strategy.short, limit = close - 5 )
else if strategy.position_size > 0
intra_trade_high := math.max(intra_trade_high, high)
intra_trade_low := low
long_tp = intra_trade_high * (1 - trailing_percent / 100)
strategy.exit("Exit B1", from_entry="B1", stop = long_tp, limit = strategy.position_avg_price * 1.03)
else if strategy.position_size < 0
intra_trade_high := high
intra_trade_low := math.min(intra_trade_low, low)
short_tp = intra_trade_low * (1 + trailing_percent / 100)
strategy.exit("Exit S1", from_entry="S1", stop = short_tp, limit = strategy.position_avg_price * 0.94)Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-09. 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/ATR-and-RSI-Based-Trend-Following-Trading-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
- BB Percentage Index Trend Fading StrategyPine Script · Trend following · 1h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual Mode Adaptive Trend Trading Strategy EMA Crossover with ATR Volatility Based Risk Management SystemPine Script · Trend following · 1d
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- Dynamic Signal Line Trend Following and Volatility Filtering StrategyPine Script · Trend following · 1h