It looks like there were multiple syntax and logical errors in the provided code. Let's correct them step by step to ensure the EA compiles and functions correctly.
Here is the revised and corrected version of the EA:
//+------------------------------------------------------------------+
//| BreakoutEA.mq5 |
//| Custom Market Structure Breakout EA |
//+------------------------------------------------------------------+
#property strict
// Input parameters
input int MagicNumber = 123456;
input double LotSize = 0.01;
input double RiskPercent = 1.0; // 1% risk per trade
input int TakeProfitPoints = 10;
input int StopLossPoints = 10;
input int TrailingStopStart = 10;
input int TrailingStopDistance = 10;
// News filter (example logic)
bool tradeAllowed = true;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("Breakout EA started.");
EventSetTimer(60); // Set timer for trailing stop
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer(); // Kill timer on deinit
Print("Breakout EA stopped.");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for news filter
CheckNews();
if (!tradeAllowed)
return;
// Get recent price data
double hh = iHigh(_Symbol, PERIOD_M5, 2); // Higher High
double lh = iHigh(_Symbol, PERIOD_M5, 1); // Lower High
double ll = iLow(_Symbol, PERIOD_M5, 2); // Lower Low
double hl = iLow(_Symbol, PERIOD_M5, 1); // Higher Low
// Cancel existing pending orders before placing new ones
CancelPendingOrders();
// Place Buy Pending Orders
if (SymbolInfoDouble(_Symbol,SYMBOL_BID) < hh)
PlacePendingOrder(ORDER_TYPE_BUY_LIMIT, hh);
if (SymbolInfoDouble(_Symbol,SYMBOL_BID) < lh)
PlacePendingOrder(ORDER_TYPE_BUY_LIMIT, lh);
// Place Sell Pending Orders
if (SymbolInfoDouble(_Symbol,SYMBOL_BID) > ll)
PlacePendingOrder(ORDER_TYPE_SELL_LIMIT, ll);
if (SymbolInfoDouble(_Symbol,SYMBOL_BID) > hl)
PlacePendingOrder(ORDER_TYPE_SELL_LIMIT, hl);
}
//+------------------------------------------------------------------+
//| Timer function for trailing stop |
//+------------------------------------------------------------------+
void OnTimer()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
double profit = PositionGetDouble(POSITION_PROFIT);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = PositionGetDouble(POSITION_SL);
int type = PositionGetInteger(POSITION_TYPE);
if (profit >= TrailingStopStart * _Point)
{
if (type == POSITION_TYPE_BUY)
{
if (bid - sl >= TrailingStopDistance * _Point)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_SLTP;
request.position = ticket;
request.sl = bid - TrailingStopDistance * _Point;
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request, result);
}
}
else if (type == POSITION_TYPE_SELL)
{
if (sl - ask >= TrailingStopDistance * _Point)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_SLTP;
request.position = ticket;
request.sl = ask + TrailingStopDistance * _Point;
request.tp = PositionGetDouble(POSITION_TP);
OrderSend(request, result);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Place a pending order |
//+------------------------------------------------------------------+
void PlacePendingOrder(int orderType, double price)
{
double sl = 0, tp = 0;
double lot = CalculateLotSize();
// Set SL and TP based on order type
if (orderType == ORDER_TYPE_BUY_LIMIT || orderType == ORDER_TYPE_BUY_STOP)
{
sl = price - StopLossPoints * _Point;
tp = price + TakeProfitPoints * _Point;
}
else if (orderType == ORDER_TYPE_SELL_LIMIT || orderType == ORDER_TYPE_SELL_STOP)
{
sl = price + StopLossPoints * _Point;
tp = price - TakeProfitPoints * _Point;
}
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_PENDING;
request.symbol = _Symbol;
request.volume = lot;
request.type = orderType;
request.price = price;
request.sl = sl;
request.tp = tp;
request.deviation = 10;
request.magic = MagicNumber;
request.comment = "Breakout EA";
if(!OrderSend(request, result))
{
Print("OrderSend failed with error #", result.retcode);
}
}
//+------------------------------------------------------------------+
//| Cancel all existing pending orders |
//+------------------------------------------------------------------+
void CancelPendingOrders()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if (OrderGetString(ORDER_SYMBOL) == _Symbol && OrderGetInteger(ORDER_MAGIC) == MagicNumber)
{
OrderDelete(ticket);
}
}
}
//+------------------------------------------------------------------+
//| Calculate lot size based on 1% risk |
//+------------------------------------------------------------------+
double CalculateLotSize()
{
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskAmount = accountBalance * (RiskPercent / 100);
double slPips = StopLossPoints / 10.0; // 10 points = 1 pip
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double lot = riskAmount / (slPips * tickValue);
lot = NormalizeDouble(lot, 2);
return MathMax(lot, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN));
}
//+------------------------------------------------------------------+
//| News Filter Placeholder |
//+------------------------------------------------------------------+
void CheckNews()
{
// TODO: Implement your news filter logic here
// Example: Check current time against known news times
// or read from a CSV file or API
tradeAllowed = true; // Change this to false during news events
}