OPEN-SOURCE SCRIPT
已更新 ATR Trend Strategy with Moving Average | Fixed TP/SL version

📈 ## ATR Trend Strategy with Moving Average
# Overview
This strategy combines a **Moving Average trend filter** with an **ATR-based breakout channel** to identify directional market movements. It is designed for traders who prefer **systematic trend-following strategies with clearly defined risk management**.
The script builds an adaptive channel around a selected Moving Average using **Average True Range (ATR)**. When price moves beyond the ATR band and the move is confirmed for a defined number of bars, a trend state is established. Trade entries can then occur either on the initial breakout or on a pullback to the Moving Average.
The strategy also includes **fixed percentage Take Profit and Stop Loss levels**, allowing users to evaluate performance under consistent risk parameters.
---
⚙️ # Key Features
• **Multiple Moving Average types**
Supports EMA, SMA, WMA, Hull MA, VWMA, RMA, and TEMA.
• **ATR-based dynamic channel**
Uses ATR to create adaptive upper and lower boundaries around the Moving Average.
• **Two entry methods**
Users can choose between breakout entries or Moving Average pullback entries.
• **Trend confirmation filter**
Signals are confirmed only after a configurable number of bars remain beyond the ATR boundary.
• **Built-in risk management**
Includes fixed percentage Take Profit and Stop Loss levels.
• **Trade visualization**
Displays the TP/SL zone directly on the chart for each trade.
• **Performance statistics panel**
Shows key strategy metrics such as:
* Total trades
* Win rate
* Profit factor
* Net profit
* Expectancy
* Average R
* Maximum drawdown
---
🧠 # Strategy Logic
The strategy follows a simple **trend-following structure**:
1️⃣ A Moving Average defines the market's baseline trend.
2️⃣ An ATR multiplier builds a dynamic volatility channel around the Moving Average.
3️⃣ When price breaks above or below this channel and remains there for a specified number of bars, a trend is confirmed.
4️⃣ Entries can occur via:
**Breakout Mode**
* Long when price breaks above the upper ATR band.
* Short when price breaks below the lower ATR band.
**MA Cross Mode**
* After a confirmed trend, entries occur on pullbacks that cross the Moving Average.
5️⃣ Risk is controlled using **fixed percentage Take Profit and Stop Loss levels**.
---
⚙️ # Inputs
Moving Average
* MA Type
* MA Length
* MA Source
ATR Signal
* ATR Type
* ATR Length
* ATR Multiplier
Trend Confirmation
* Number of confirmation bars
* Confirmation price source (Close or High/Low)
Entry & Risk Management
* Entry method (Breakout or MA Cross)
* Take Profit (%)
* Stop Loss (%)
---
📊 ## Usage Notes
This strategy is designed for **trend-following market conditions** and may perform best in environments with sustained directional movement.
Users are encouraged to **experiment with different Moving Average types, ATR multipliers, and confirmation settings** to adapt the strategy to different markets and timeframes.
---
⚠️ ## Disclaimer
This script is provided for **educational and research purposes only**.
Past performance does not guarantee future results.
---
# Overview
This strategy combines a **Moving Average trend filter** with an **ATR-based breakout channel** to identify directional market movements. It is designed for traders who prefer **systematic trend-following strategies with clearly defined risk management**.
The script builds an adaptive channel around a selected Moving Average using **Average True Range (ATR)**. When price moves beyond the ATR band and the move is confirmed for a defined number of bars, a trend state is established. Trade entries can then occur either on the initial breakout or on a pullback to the Moving Average.
The strategy also includes **fixed percentage Take Profit and Stop Loss levels**, allowing users to evaluate performance under consistent risk parameters.
---
⚙️ # Key Features
• **Multiple Moving Average types**
Supports EMA, SMA, WMA, Hull MA, VWMA, RMA, and TEMA.
• **ATR-based dynamic channel**
Uses ATR to create adaptive upper and lower boundaries around the Moving Average.
• **Two entry methods**
Users can choose between breakout entries or Moving Average pullback entries.
• **Trend confirmation filter**
Signals are confirmed only after a configurable number of bars remain beyond the ATR boundary.
• **Built-in risk management**
Includes fixed percentage Take Profit and Stop Loss levels.
• **Trade visualization**
Displays the TP/SL zone directly on the chart for each trade.
• **Performance statistics panel**
Shows key strategy metrics such as:
* Total trades
* Win rate
* Profit factor
* Net profit
* Expectancy
* Average R
* Maximum drawdown
---
🧠 # Strategy Logic
The strategy follows a simple **trend-following structure**:
1️⃣ A Moving Average defines the market's baseline trend.
2️⃣ An ATR multiplier builds a dynamic volatility channel around the Moving Average.
3️⃣ When price breaks above or below this channel and remains there for a specified number of bars, a trend is confirmed.
4️⃣ Entries can occur via:
**Breakout Mode**
* Long when price breaks above the upper ATR band.
* Short when price breaks below the lower ATR band.
**MA Cross Mode**
* After a confirmed trend, entries occur on pullbacks that cross the Moving Average.
5️⃣ Risk is controlled using **fixed percentage Take Profit and Stop Loss levels**.
---
⚙️ # Inputs
Moving Average
* MA Type
* MA Length
* MA Source
ATR Signal
* ATR Type
* ATR Length
* ATR Multiplier
Trend Confirmation
* Number of confirmation bars
* Confirmation price source (Close or High/Low)
Entry & Risk Management
* Entry method (Breakout or MA Cross)
* Take Profit (%)
* Stop Loss (%)
---
📊 ## Usage Notes
This strategy is designed for **trend-following market conditions** and may perform best in environments with sustained directional movement.
Users are encouraged to **experiment with different Moving Average types, ATR multipliers, and confirmation settings** to adapt the strategy to different markets and timeframes.
---
⚠️ ## Disclaimer
This script is provided for **educational and research purposes only**.
Past performance does not guarantee future results.
---
版本注释
## 📈 ATR Trend Strategy with Moving Average# Overview
This strategy combines a **Moving Average trend filter** with an **ATR-based breakout channel** to identify directional market movements. It is designed for traders who prefer **systematic trend-following strategies with clearly defined risk management**.
The script builds an adaptive channel around a selected Moving Average using **Average True Range (ATR)**. When price moves beyond the ATR band and the move is confirmed for a defined number of bars, a trend state is established. Trade entries can then occur either on the initial breakout or on a pullback to the Moving Average.
The strategy also includes **fixed percentage Take Profit and Stop Loss levels**, allowing users to evaluate performance under consistent risk parameters.
---
⚙️ Key Features
• **Multiple Moving Average types**
Supports EMA, SMA, WMA, Hull MA, VWMA, RMA, and TEMA.
• **ATR-based dynamic channel**
Uses ATR to create adaptive upper and lower boundaries around the Moving Average.
• **Two entry methods**
Users can choose between breakout entries or Moving Average pullback entries.
• **Trend confirmation filter**
Signals are confirmed only after a configurable number of bars remain beyond the ATR boundary.
• **Built-in risk management**
Includes fixed percentage Take Profit and Stop Loss levels.
• **Trade visualization**
Displays the TP/SL zone directly on the chart for each trade.
• **Performance statistics panel**
Shows key strategy metrics such as:
* Total trades
* Win rate
* Profit factor
* Net profit
* Expectancy
* Average R
* Maximum drawdown
---
🧠 Strategy Logic
The strategy follows a simple **trend-following structure**:
1️⃣ A Moving Average defines the market's baseline trend.
2️⃣ An ATR multiplier builds a dynamic volatility channel around the Moving Average.
3️⃣ When price breaks above or below this channel and remains there for a specified number of bars, a trend is confirmed.
4️⃣ Entries can occur via:
**Breakout Mode**
* Long when price breaks above the upper ATR band.
* Short when price breaks below the lower ATR band.
**MA Cross Mode**
* After a confirmed trend, entries occur on pullbacks that cross the Moving Average.
5️⃣ Risk is controlled using **fixed percentage Take Profit and Stop Loss levels**.
---
⚙️ Inputs
# Moving Average
* MA Type
* MA Length
* MA Source
# ATR Signal
* ATR Type
* ATR Length
* ATR Multiplier
# Trend Confirmation
* Number of confirmation bars
* Confirmation price source (Close or High/Low)
# Entry & Risk Management
* Entry method (Breakout or MA Cross)
* Take Profit (%)
* Stop Loss (%)
---
📊 Usage Notes
This strategy is designed for **trend-following market conditions** and may perform best in environments with sustained directional movement.
Users are encouraged to **experiment with different Moving Average types, ATR multipliers, and confirmation settings** to adapt the strategy to different markets and timeframes.
---
⚠️ Disclaimer
This script is provided for **educational and research purposes only**.
Past performance does not guarantee future results.
---
版本注释
Version 2.0 – Adaptive TP/SL & Trend Analytics• Introduced Auto TP/SL system:
- Take Profit based on 70th percentile of MFE distribution
- Stop Loss based on 70th percentile of MAE distribution
- Adapts dynamically to asset behavior and timeframe
• Added Trend Distribution Engine:
- Tracks MFE, MAE, Efficiency, and Volatility per trend
- Uses percentile-based analysis (P50, P70, P90)
• Improved trend validation logic:
- Uses ATR-normalized thresholds
- Reduces bias across different assets and price scales
• Fixed critical state machine issue:
- Ensured correct order: Finalize → Reset → Update
- Eliminated incorrect MFE/MAE calculations
• Improved statistical accuracy:
- Excluded first bar noise
- Applied interpolation for percentile calculation
• Enhanced UI:
- Auto TP/SL display in table
- Key metrics available in Data Window
This version transforms the strategy into a data-driven, adaptive framework for trend-following systems.
===============================
Important Note:
All key analytics metrics are available in the TradingView Data Window.
To access them:
• Hover over the chart
• Open the “Data Window” panel (right side of TradingView)
Metrics shown:
• MFE (P50 / P70 / P90)
• MAE (P50 / P70 / P90)
• Efficiency
• Volatility
Use these values to evaluate trend behavior and fine-tune your TP/SL settings.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。