Strategy vault / Trend following
Trend Tracking Strategy Based on Moving Average and Average True Range
This strategy uses moving average and average true range to determine the market trend direction for trend tracking trading.

Overview
This strategy uses moving average and average true range to determine the market trend direction for trend tracking trading.
Principles
This strategy uses the moving average ma of len periods and 2 times the average true range atr of len periods to determine the market trend. The specific rules are:
When the low is greater than the moving average plus the average true range (low > ma + atr), it is judged as an upward trend. When the high is less than the moving average minus the average true range (high < ma - atr), it is judged as a downward trend.
In other cases, the previous judgment is maintained.
When an upward trend is determined, go long at a certain percentage when allowed to go long. When a downward trend is determined, go short at a certain percentage when allowed to go short.
The closing condition is to reach the specified trading end date.
Advantage Analysis
The advantages of this strategy are:
- Use moving average to determine general trend direction and avoid being misled by short-term market fluctuations.
- Use average true range to set dynamic stop loss, which is conducive to risk control.
- Can capture trend opportunities in a timely manner following the trend, with high profit potential.
- Simple and easy to operate rules.
Risk Analysis
The main risks faced by this strategy are:
- It is prone to multiple losses in a sharply fluctuating market.
- Unable to effectively determine trend reversal points, there is a risk of chasing highs and killing lows.
- Improper parameter settings of average true range may result in exit points that are too loose or too strict.
Solutions:
- Adjust moving average parameters appropriately to use more stable parameters.
- Confirm signals with other indicators to avoid chasing highs and killing lows.
- Optimize and test the average true range parameters to set appropriate parameters.
Optimization Directions
The strategy can be optimized from the following aspects:
- Test different moving average systems to find more stable parameter combinations.
- Add other auxiliary indicators to judge the reliability of signals.
- Test the average true range parameters to find the optimal parameters.
- Optimize capital utilization through leverage to increase return on capital.
- Combine machine learning and other methods to achieve dynamic parameter optimization.
Summary
The overall idea of this strategy is clear and easy to understand. It uses moving averages to determine trend direction and uses average true range to set stops. It can effectively track trends. But there are certain risks, and further optimization of parameter settings and adding other judgment indicators are needed. In general, this strategy provides a viable approach for trend tracking trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | true | Long |
| v_input_2 | true | Short |
| v_input_3 | 100 | Capital, % |
| v_input_4 | 30 | MA Length |
| v_input_5_ohlc4 | 0 | MA Source: ohlc4 | high | low | open | hl2 | hlc3 | hlcc4 | close |
| v_input_6 | false | limitmode |
| v_input_7 | 1900 | From Year |
| v_input_8 | 2100 | To Year |
| v_input_9 | true | From Month |
| v_input_10 | 12 | To Month |
| v_input_11 | true | From day |
| v_input_12 | 31 | To day |
Source Pine Script
/*backtest
start: 2024-01-04 00:00:00
end: 2024-01-11 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//2019
//Noro
//@version=4
strategy(title = "Noro's MA+ATR Strategy", shorttitle = "MA+ATR str", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 0)
//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
capital = input(100, defval = 100, minval = 1, maxval = 10000, title = "Capital, %")
len = input(30, minval = 2, title = "MA Length")
src = input(ohlc4, title = "MA Source")
limitmode = input(false)
fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")
//MA + BG
atr = sma(tr, len) * 2
ma = sma(src, len)
plot(ma, color = color.blue, linewidth = 4)
trend = 0
trend := low > ma + atr ? 1 : high < ma - atr ? -1 : trend[1]
col = trend == 1 ? color.lime : color.red
bgcolor(col, transp = 70)
//Trading
lot = 0.0
lot := strategy.position_size != strategy.position_size[1] ? strategy.equity / close * capital / 100 : lot[1]
if trend == 1 and limitmode == false
strategy.entry("Long", strategy.long, needlong == false ? 0 : lot)
if trend == -1 and limitmode == false
strategy.entry("Short", strategy.short, needshort == false ? 0 : lot)
if trend == 1 and limitmode
strategy.entry("Long", strategy.long, needlong == false ? 0 : lot)
if trend == -1 and limitmode
strategy.entry("Short", strategy.short, needshort == false ? 0 : lot)
// if time > timestamp(toyear, tomonth, today, 23, 59)
// strategy.close_all()Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-12. 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/Trend-Tracking-Strategy-Based-on-Moving-Average-and-Average-True-Range.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 Moving Average Crossover Hybrid StrategyPine 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
- 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