Strategy vault / Trend following
ATR Dynamic Profit Target and Stop Loss Strategy
This strategy uses dynamic profit targets and stop losses that adjust based on current price and volatility. The logic is: 1. Calculate Average True Range (ATR) over a period (e.g. 20 days) 2. In uptrend, profit…
Strategy Logic
This strategy uses dynamic profit targets and stop losses that adjust based on current price and volatility.
The logic is:
Calculate Average True Range (ATR) over a period (e.g. 20 days)
In uptrend, profit target/stop is highest price minus ATR multiple
In downtrend, profit target/stop is lowest price plus ATR multiple
Reverse trade when price exceeds profit target/stop
Trend changes when price breaches profit target/stop
Adjust profit target/stop based on new trend state
The strategy leverages ATR to automatically set dynamic trailing profit targets and stops. This allows timely locking in of profits and preventing excessive losses.
Advantages
ATR automatically calculates profit/stop levels
Dynamic adjustment trails price in real-time
Timely profit taking and stopping controls risk
Risks
ATR parameters require optimization
Stops too close risks being stopped out
Need to monitor real-time ATR changes
Summary
This strategy uses ATR to dynamically set profit/stop levels for automatic trailing. ATR tuning can improve stop performance. But over-tight stops require caution.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | length |
| v_input_2 | true | mult |
Source Pine Script
/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-13 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
strategy("Dhananjay Volatility stop strategy v1.0", overlay=true)
length = input(20)
mult = input(1)
atr_ = atr(length)
max1 = max(nz(max_[1]), close)
min1 = min(nz(min_[1]), close)
is_uptrend_prev = nz(is_uptrend[1], true)
stop = is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
vstop_prev = nz(vstop[1])
vstop1 = is_uptrend_prev ? max(vstop_prev, stop) : min(vstop_prev, stop)
is_uptrend = close - vstop1 >= 0
is_trend_changed = is_uptrend != is_uptrend_prev
max_ = is_trend_changed ? close : max1
min_ = is_trend_changed ? close : min1
vstop = is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1
plot(vstop, color = is_uptrend ? green : red, style=line, linewidth=2)
bearish = close < vstop
bullish = close > vstop
if (bullish)
strategy.entry("Buy", strategy.long, 1)
if (bearish)
strategy.entry("Sell", strategy.short, 1)
Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-14. 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-Dynamic-Profit-Target-and-Stop-Loss-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 Dynamic Trend Following and EMA Crossover Trading StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- ATR and T3 Moving Average StrategyPine Script · Trend following · 5m
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m