//+------------------------------------------------------------------+
//| Advanced Breakout EA (MT5) |
//| Full Version with SL/TP Modes, Trailing, Risk, etc. |
//+------------------------------------------------------------------+
#property strict
#include <Trade/Trade.mqh>
CTrade trade;
int PositionsTotalByMagic()
{
int count = 0;
for (int i = 0; i < PositionsTotal(); i++)
{
if (PositionGetTicket(i))
{
if ((ulong)PositionGetInteger(POSITION_MAGIC) == MagicNumber)
count++;
}
}
return count;
}
void CloseAllPositions()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionGetTicket(i))
{
if ((ulong)PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
string symbol = PositionGetString(POSITION_SYMBOL);
trade.PositionClose(symbol);
}
}
}
}
Your EA is well structured with many advanced features! However, I noticed some potential issues and improvements that might help the code work more reliably in MQL5:
You use PositionGetTicket(i) to check for a position's ticket, but PositionGetTicketdoes not take an index as input, it's a function that returns the ticket of the currently selected position.
You need to use PositionSelectByIndex(i) to select the position at index i before querying its properties.
Similarly, to get the magic number and symbol of that position, you must call PositionGetInteger() and PositionGetString()after selecting the position.
In OpenTrade() you get the entry price by SymbolInfoDouble(symbol, SYMBOL_ASK) and SYMBOL_BID but better to get the current price for more accurate SL/TP calculation:
For buy: entry = SymbolInfoDouble(symbol, SYMBOL_ASK)
For sell: entry = SymbolInfoDouble(symbol, SYMBOL_BID)
The CTrade class's SetStopLoss() and SetTakeProfit() are deprecated or unofficial. The best practice is to pass the SL/TP values directly in the Buy() or Sell() functions parameters or include them in trade.PositionOpen().
In MQL5, typically, you open a position like this:
Good, but be careful that stopLossPoints might become 0 in some edge cases (e.g. SL = entry price). This will cause division by zero.
Better to add a minimal check:
mql5
double stopLossPoints = MathAbs(entry - slPrice)/_Point;
if(stopLossPoints < 1.0) // or any minimum
return 0; // or minimal lot, or reject trade
6. Trailing Stop management
Your EA includes trailing stop parameters, but you do not have an implementation for trailing stops inside OnTick(). You may want to add a function that manages trailing stops on existing positions.
7. Miscellaneous suggestions
Use ENUM_ORDER_TYPE constants instead of Buy() & Sell() overloads (okay to continue using them).
Add verbose error handling after every trade operation.
When normalizing prices, consider using SymbolInfoInteger(symbol, SYMBOL_DIGITS) rather than _Digits for accuracy.
Summary with corrections in key functions:
mql5
int PositionsTotalByMagic()
{
int count = 0;
for(int i=0; i<PositionsTotal(); i++)
{
if(PositionSelectByIndex(i))
{
if ((ulong)PositionGetInteger(POSITION_MAGIC) == MagicNumber)
count++;
}
}
return count;
}
void CloseAllPositions()
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
if(PositionSelectByIndex(i))
{
if ((ulong)PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
string symbol = PositionGetString(POSITION_SYMBOL);
if(!trade.PositionClose(symbol))
Print("Failed to close position on ", symbol, ", error: ", GetLastError());
}
}
}
}
void OpenTrade(bool isBuy)
{
string symbol = _Symbol;
double entry = isBuy ? SymbolInfoDouble(symbol, SYMBOL_ASK) : SymbolInfoDouble(symbol, SYMBOL_BID);
if(!PassRSIFilter(symbol, isBuy)) return;
double slPrice = GetStopLossPrice(isBuy, entry, symbol);
double tpPrice = (TP_Mode == 1) ? GetTakeProfitPrice(entry, slPrice, isBuy) : 0;
double stopLossPoints = MathAbs(entry - slPrice)/_Point;
if(stopLossPoints < 1.0)
{
Print("Stop loss distance too small, trade skipped.");
return;
}
double lotSize = CalculateLotSize(symbol, stopLossPoints);
if(lotSize <= 0)
{
Print("Calculated lot size invalid, trade skipped.");
return;
}
bool result = false;
if(isBuy)
result = trade.Buy(lotSize, symbol, entry, slPrice, tpPrice, "Breakout Buy");
else
result = trade.Sell(lotSize, symbol, entry, slPrice, tpPrice, "Breakout Sell");
if (!result)
Print("Trade failed: ", trade.ResultRetcodeDescription());
}
If you want me to help with adding trailing stop logic or any other feature, just ask!
TimeCurrent()exists but is sometimes ambiguous if you miss datetime declaration or miss parentheses.
The equivalent of TimeDay in MQL5 is TimeDay() or TimeDayOfMonth()? Actually in MQL5, there is no single TimeDay() function; instead, you use TimeDay() for datetime values only if you include the Datetime.mqh library, or you extract day using:
mql5
// you get a datetime variable, e.g. current_time = TimeCurrent();
// then use TimeDay(current_time) if TimeDay() is known
But it's better to use built-in TimeDay() function from Time namespace or extract day manually with TimeToStruct() or TimeLocal().
In MQL5 base, you can get the day of a datetime variable dt by:
mql5
MqlDateTime dtStruct;
TimeToStruct(dtVar, dtStruct);
int day = dtStruct.day;