Institutional ATR Trailing Stop and Breakeven Manager - expert for MetaTrader 4
详情
The default MetaTrader 4 trailing stop relies on static pip distances. In a dynamic market environment, static stops are fundamentally flawed. During low volatility, your stop might be too loose, exposing unnecessary risk; during high volatility, it might be too tight, causing premature stop-outs before the real trend begins.
默认的 MetaTrader 4 trailing stop 策略依赖于固定的点数间隔。在动态的市场环境中,这种静态的止损策略存在根本缺陷。在波动性较低的情况下,止损点可能会设置得过于宽松,从而带来不必要的风险;而在波动性较高的时期,止损点则可能设置得太紧,导致在趋势真正开始之前就不得不执行止损操作。
Institutional algorithms solve this by trailing stops based on market volatility, most commonly measured by the Average True Range (ATR). The Institutional ATR Trailing Manager is a plug-and-play utility Expert Advisor designed to take over trade management for manual or EA-opened positions.
机构型算法通过基于市场波动性的跟踪止损机制来解决这个问题,其中最常用的衡量指标就是平均真实范围(ATR)。机构型 ATR 跟踪管理器是一种即插即用的智能顾问,旨在负责手动操作或由自动交易系统开启的持仓的管理工作。
Core Protective Features 核心保护功能
Dynamic ATR Trailing: Continuously calculates the current market volatility. As the price moves in your favor, it trails your Stop Loss at a mathematical distance (ATR Value × Multiplier), giving the trade "room to breathe" while locking in maximum profit.
动态 ATR 跟踪机制:持续计算当前市场的波动性。当价格对你有利时,系统会以一定的数学距离(ATR 值×乘数)来设定止损点,从而给交易带来更多调整空间,同时锁定最大利润。Auto-Breakeven System: Emotional trading often leads to letting winning trades turn into losers. This EA automatically moves your Stop Loss to your entry price (plus a small profit to cover commissions) the moment the price reaches your defined safety threshold.
自动止损系统:情绪化的交易往往会导致获胜的交易变成亏损。当价格达到你设定的安全阈值时,这个自动交易系统会自动将止损点移动到你的入场价格位置(同时会保留一部分利润以支付交易佣金)。Universal Management: By default, it manages all manual trades on the chart's symbol. You can also assign a specific Magic Number to manage positions opened by other Expert Advisors.
通用管理:默认情况下,该系统会管理图表中所有手动交易的订单。您也可以为其他专家顾问所开的头寸分配特定的编号来进行管理。
Input Parameters 输入参数
ATR_Period: The lookback period for the volatility calculation (Default: 14).
ATR_Period:用于计算波动性的回测周期(默认值为 14)。ATR_Multiplier: How wide you want the trailing stop. 2.0 or 3.0 are standard institutional settings to avoid noise.
ATR_Multiplier:希望设置多大的跟踪止损范围。2.0 或 3.0 是避免市场波动干扰的标准设置。Use_Breakeven: Enable or disable the auto-breakeven feature.
使用断断平衡功能:可以启用或禁用自动断断平衡功能。Breakeven_Activation_Pips: How many pips in profit before moving the stop to entry.
盈亏平衡点所需的 pip 数:在将止损点移动到进入点之前,需要获得多少 pip 的盈利。Magic_Number: Set to 0 to manage manual trades, or specify a number to manage EA trades.
魔法数字:设置为 0 可用于管理手动交易,或者指定一个数字来管控自动交易。
源代码
MQL4//+------------------------------------------------------------------+
//| Prop_ATR_Trail_Manager.mq4 |
//| Copyright 2026, Amanda V |
//+------------------------------------------------------------------+
#property copyright "Amanda V"
#property version "1.00"
#property strict
//--- Inputs
input group "=== Volatility Trailing Settings ==="
input int InpATRPeriod = 14; // ATR Period
input double InpATRMultiplier = 2.0; // ATR Multiplier
input group "=== Breakeven Settings ==="
input bool InpUseBreakeven = true; // Use Auto-Breakeven
input int InpBEActivation = 20; // Pips in profit to activate BE
input int InpBELockProfit = 2; // Pips to lock in profit (covers fees)
input group "=== Global Settings ==="
input int InpMagicNumber = 0; // Magic Number (0 = Manual Trades)
double pt;
double pip_mult;
//+------------------------------------------------------------------+
int OnInit()
{
pt = Point;
pip_mult = (Digits == 3 || Digits == 5) ? 10.0 : 1.0;
Print("Institutional ATR Manager Initialized.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTick()
{
double atr_value = iATR(Symbol(), 0, InpATRPeriod, 1);
double trail_distance = atr_value * InpATRMultiplier;
double be_activation_pts = InpBEActivation * pip_mult * pt;
double be_lock_pts = InpBELockProfit * pip_mult * pt;
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && (InpMagicNumber == 0 || OrderMagicNumber() == InpMagicNumber))
{
double open_price = OrderOpenPrice();
double current_sl = OrderStopLoss();
int type = OrderType();
//--- BUY ORDERS
if(type == OP_BUY)
{
double bid = Bid;
// 1. Breakeven Logic
if(InpUseBreakeven && bid >= open_price + be_activation_pts)
{
double new_be_sl = open_price + be_lock_pts;
if(current_sl < new_be_sl && new_be_sl < bid)
{
bool res = OrderModify(OrderTicket(), open_price, new_be_sl, OrderTakeProfit(), 0, clrGreen);
if(res) current_sl = new_be_sl; // Update for trailing check
}
}
// 2. ATR Trailing Logic
double new_trail_sl = bid - trail_distance;
if(new_trail_sl > current_sl && new_trail_sl < bid)
{
// Only modify if difference is meaningful to prevent broker spam
if(current_sl == 0 || new_trail_sl - current_sl > (2 * pip_mult * pt))
{
OrderModify(OrderTicket(), open_price, new_trail_sl, OrderTakeProfit(), 0, clrBlue);
}
}
}
//--- SELL ORDERS
else if(type == OP_SELL)
{
double ask = Ask;
// 1. Breakeven Logic
if(InpUseBreakeven && ask <= open_price - be_activation_pts)
{
double new_be_sl = open_price - be_lock_pts;
if(current_sl > new_be_sl || current_sl == 0)
{
if(new_be_sl > ask)
{
bool res = OrderModify(OrderTicket(), open_price, new_be_sl, OrderTakeProfit(), 0, clrGreen);
if(res) current_sl = new_be_sl;
}
}
}
// 2. ATR Trailing Logic
double new_trail_sl = ask + trail_distance;
if(current_sl == 0 || (new_trail_sl < current_sl && new_trail_sl > ask))
{
if(current_sl == 0 || current_sl - new_trail_sl > (2 * pip_mult * pt))
{
OrderModify(OrderTicket(), open_price, new_trail_sl, OrderTakeProfit(), 0, clrRed);
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
