mql5 Copy code
//+------------------------------------------------------------------+
//| BreakoutEA.mq5 |
//| Custom Market Structure Breakout EA |
//+------------------------------------------------------------------+
#property strict
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;
CTrade trade; // Declare CTrade object for order/position management
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("Breakout EA started.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("Breakout EA stopped.");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for news filter
CheckNews();
if (!tradeAllowed)
return;
// Get recent price data (MQL5 compatible)
double hh = iHigh(_Symbol, PERIOD_M5, 1); // Higher High (previous bar)
double lh = iHigh(_Symbol, PERIOD_M5, 2); // Lower High (bar before previous)
double ll = iLow(_Symbol, PERIOD_M5, 1); // Lower Low (previous bar)
double hl = iLow(_Symbol, PERIOD_M5, 2); // Higher Low (bar before previous)
// 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);
// Trailing Stop Logic
for (int i = (int)(PositionsTotal() - 1); i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (ticket > 0 && PositionSelectByTicket(ticket))
{
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);
if (!OrderSend(request, result))
{
Print("Trailing Stop error: ", GetLastError());
}
}
}
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);
if (!OrderSend(request, result))
{
Print("Trailing Stop error: ", GetLastError());
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Place a pending order |
//+------------------------------------------------------------------+
void PlacePendingOrder(ENUM_ORDER_TYPE 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 error: ", GetLastError());
}
}
//+------------------------------------------------------------------+
//| Cancel all existing pending orders |
//+------------------------------------------------------------------+
void CancelPendingOrders()
{
// Close positions
for (int i = (int)(PositionsTotal() - 1); i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (ticket > 0 && PositionSelectByTicket(ticket))
{
if ((long)PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
trade.PositionClose(ticket);
}
}
}
// Delete pending orders
for (int i = (int)(OrdersTotal() - 1); i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if (ticket > 0 && OrderSelectByTicket(ticket))
{
if (OrderGetString(ORDER_SYMBOL) == _Symbol && (long)OrderGetInteger(ORDER_MAGIC) == MagicNumber)
{
trade.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
tradeAllowed = true; // Change to false during news events
}