Below is the minimal change needed to fix the “wrong parameters count” error for iATR in MQL5. In MQL5, iATR returns an indicator handle, not the ATR value directly. You must then call CopyBuffer() to retrieve the ATR values.
I’ll show you how to integrate this into your PlacePendingOrders() function. Afterwards I’ll post the full updated EA for clarity.
- Add at the top (global scope) so we can reuse the handle if desired:
int atr_handle = INVALID_HANDLE;
- In
OnInit(), create the ATR handle if you’ve enabled ATR-based SL:
int OnInit()
{
if(UseATRStopLoss)
{
atr_handle = iATR(_Symbol, PERIOD_D1, ATRPeriod);
if(atr_handle == INVALID_HANDLE)
Print("Failed to get ATR handle");
}
// … the rest of your init …
return(INIT_SUCCEEDED);
}
- In
OnDeinit(), release the handle:
void OnDeinit(const int reason)
{
if(atr_handle != INVALID_HANDLE)
IndicatorRelease(atr_handle);
// … delete lines …
}
- Finally, modify
PlacePendingOrders() so it pulls the latest ATR value via CopyBuffer():
void PlacePendingOrders()
{
double slPoints = StopLossPoints;
double tpPoints = TakeProfitPoints;
//--- ATR-based Stop Loss?
if(UseATRStopLoss && atr_handle != INVALID_HANDLE)
{
double atr_buf[];
// copy 1 bar, shift=1 (the completed D1 bar)
if(CopyBuffer(atr_handle, 0, 1, 1, atr_buf) > 0)
slPoints = atr_buf[0] * ATRMultiplier / _Point;
else
Print("CopyBuffer failed for ATR");
}
//--- USD-based overrides?
if(StopLossUSD > 0.0) slPoints = USDToPoints(StopLossUSD);
if(TakeProfitUSD > 0.0) tpPoints = USDToPoints(TakeProfitUSD);
// … then place your BuyStop/SellStop exactly as before …
}
— — — — — — —
Full EA with ATR Handle Integration
//+------------------------------------------------------------------+
//| GoldenBreakout.mq5
//+------------------------------------------------------------------+
#property copyright "You"
#property link "https://…"
#property version "1.00"
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
input double LotSize = 0.1;
input int StopLossPoints = 50;
input int TakeProfitPoints = 100;
input double StopLossUSD = 0.0;
input double TakeProfitUSD = 0.0;
input bool UseATRStopLoss = false;
input int ATRPeriod = 14;
input double ATRMultiplier = 2.0;
input bool UseTrailingStop = false;
input int TrailingStopPoints = 30;
input int MagicNumber = 12345;
//+------------------------------------------------------------------+
//| Globals |
//+------------------------------------------------------------------+
double prevDayHigh=0, prevDayLow=0;
datetime prevDay=0;
CTrade trade;
CSymbolInfo symInfo;
int atr_handle = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Expert init |
//+------------------------------------------------------------------+
int OnInit()
{
symInfo.Name(_Symbol);
symInfo.Refresh();
if(UseATRStopLoss)
{
atr_handle = iATR(_Symbol, PERIOD_D1, ATRPeriod);
if(atr_handle == INVALID_HANDLE)
Print("Failed to create ATR handle");
}
UpdateDailyLevels();
PlacePendingOrders();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinit |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(atr_handle != INVALID_HANDLE)
IndicatorRelease(atr_handle);
ObjectDelete("PrevDayHigh");
ObjectDelete("PrevDayLow");
}
//+------------------------------------------------------------------+
//| OnTick |
//+------------------------------------------------------------------+
void OnTick()
{
datetime day0 = iTime(_Symbol, PERIOD_D1, 0);
if(day0 != prevDay)
{
UpdateDailyLevels();
DeletePendingOrders();
PlacePendingOrders();
prevDay = day0;
}
if(UseTrailingStop) ApplyTrailingStop();
}
//+------------------------------------------------------------------+
//| Update Daily Levels |
//+------------------------------------------------------------------+
void UpdateDailyLevels()
{
prevDayHigh = iHigh(_Symbol, PERIOD_D1, 1);
prevDayLow = iLow(_Symbol, PERIOD_D1, 1);
// draw lines
ObjectDelete("PrevDayHigh");
ObjectCreate("PrevDayHigh", OBJ_HLINE, 0, 0, prevDayHigh);
ObjectSetInteger(0, "PrevDayHigh", OBJPROP_COLOR, clrGreen);
ObjectDelete("PrevDayLow");
ObjectCreate("PrevDayLow", OBJ_HLINE, 0, 0, prevDayLow);
ObjectSetInteger(0, "PrevDayLow", OBJPROP_COLOR, clrRed);
}
//+------------------------------------------------------------------+
//| Delete old pending orders |
//+------------------------------------------------------------------+
void DeletePendingOrders()
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
ulong ticket=OrderGetTicket(i);
if(OrderSelect(ticket)
&& OrderGetInteger(ORDER_MAGIC)==MagicNumber
&& OrderGetString(ORDER_SYMBOL)==_Symbol)
trade.OrderDelete(ticket);
}
}
//+------------------------------------------------------------------+
//| Place Pending Orders |
//+------------------------------------------------------------------+
void PlacePendingOrders()
{
double slP = StopLossPoints;
double tpP = TakeProfitPoints;
// ATR-based SL
if(UseATRStopLoss && atr_handle!=INVALID_HANDLE)
{
double atr_buf[];
if(CopyBuffer(atr_handle,0,1,1,atr_buf)>0)
slP = atr_buf[0]*ATRMultiplier/_Point;
else
Print("ATR CopyBuffer failed");
}
// USD overrides
if(StopLossUSD>0) slP = USDToPoints(StopLossUSD);
if(TakeProfitUSD>0) tpP = USDToPoints(TakeProfitUSD);
// Buy Stop
{
double price=prevDayHigh;
double sl = slP>0 ? price - slP*_Point : 0;
double tp = tpP>0 ? price + tpP*_Point : 0;
trade.BuyStop(LotSize, price, _Symbol, sl, tp, ORDER_TIME_DAY,0,"BuyBreakout");
trade.SetExpertMagicNumber(MagicNumber);
}
// Sell Stop
{
double price=prevDayLow;
double sl = slP>0 ? price + slP*_Point : 0;
double tp = tpP>0 ? price - tpP*_Point : 0;
trade.SellStop(LotSize, price, _Symbol, sl, tp, ORDER_TIME_DAY,0,"SellBreakout");
trade.SetExpertMagicNumber(MagicNumber);
}
}
//+------------------------------------------------------------------+
//| USD → Points |
//+------------------------------------------------------------------+
double USDToPoints(double usd)
{
symInfo.Refresh();
double val = symInfo.TickValue();
double sz = symInfo.TickSize();
return usd/(val*LotSize)*sz/_Point;
}
//+------------------------------------------------------------------+
//| Trailing Stop |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
ulong ticket=PositionGetTicket(i);
if(!PositionSelectByTicket(ticket)) continue;
if(PositionGetInteger(POSITION_MAGIC)!=MagicNumber ||
PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
double current = (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
? SymbolInfoDouble(_Symbol,SYMBOL_BID)
: SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double openp = PositionGetDouble(POSITION_PRICE_OPEN);
double sl = PositionGetDouble(POSITION_SL);
double tp = PositionGetDouble(POSITION_TP);
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
double newsl = current - TrailingStopPoints*_Point;
if(newsl>sl && newsl>openp)
trade.PositionModify(ticket,newsl,tp);
}
else
{
double newsl = current + TrailingStopPoints*_Point;
if((newsl<sl || sl==0) && newsl<openp)
trade.PositionModify(ticket,newsl,tp);
}
}
}
Key points:
• In MQL5 you must use iATR() → handle → CopyBuffer() to get ATR values.
• Release the handle with IndicatorRelease() in OnDeinit().
Compile this, attach to your chart, and the ATR-based stop loss will now work without the parameter‐count error. 🚀