0bps

Strategy vault / Trend following

ATR Trend Following Strategy

This strategy uses Average True Range (ATR) to capture price trends and sets stops based on ATR for trend following.

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

Overview

This strategy uses Average True Range (ATR) to capture price trends and sets stops based on ATR for trend following.

How it Works

  1. Calculate ATR value.

  2. Determine stop loss level based on ATR.

  3. Enter long/short when price breaks stop level.

  4. Lock in profits by adjusting stops dynamically.

Advantages

  • ATR automatically adjusts stops, no manual intervention needed
  • Simple and intuitive logic, easy to implement
  • Helps avoid being trapped, timely stop loss
  • Profits from riding trends
  • Trade frequency controlled via ATR parameters

Risks

  • Poor ATR parameters can cause stops to be too Loose or Tight
  • Unable to effectively identify trend end
  • Some time lag exists
  • Reversals may cut profits

Optimization Directions

  • Optimize ATR period parameter
  • Test different ATR multiples for stop distance
  • Add filters to detect trend reversal
  • Explore machine learning for parameter optimization
  • Consider additional profit taking mechanisms

Conclusion

The strategy effectively catches trends using ATR and locks in profits with dynamic stops. Fine tuning parameters can improve performance. But ATR lag cannot be completely eliminated. Overall a simple and practical trend following solution.

Parameters

NameDefaultWhat it controls
v_input_15nATRPeriod
v_input_23.5nATRMultip

Source Pine Script

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

strategy(title="ATR Strategy", overlay = true,  commission_type=strategy.commission.percent,commission_value=0.075)
//credits to HPotter for the orginal code
nATRPeriod = input(5)
nATRMultip = input(3.5)
xATR = ta.atr(nATRPeriod)
nLoss = nATRMultip * xATR
xATRTrailingStop = iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), math.max(nz(xATRTrailingStop[1]), close - nLoss),
                    iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), math.min(nz(xATRTrailingStop[1]), close + nLoss), 
                        iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
pos =	iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
	    iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) 
color = pos == -1 ? color.red: pos == 1 ? color.green : color.blue 
plot(xATRTrailingStop, color=color, title="ATR Trailing Stop")

barbuy = close > xATRTrailingStop 
barsell = close < xATRTrailingStop 

strategy.entry("Long", strategy.long, when = barbuy) 
strategy.entry("Short", strategy.short, when = barsell) 

barcolor(barbuy? color.green:color.red)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-21. 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/ATR-Trend-Following-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