Here's the code with the remaining errors fixed:
//+------------------------------------------------------------------+
//| BreakoutEA.mq5 |
//| Custom Market Structure Breakout EA |
//+------------------------------------------------------------------+
#property strict
#include <Trade\Trade.mqh>
CTrade trade; // Create trade object for order operations
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()
{
// Set magic number for trade operations
trade.SetExpertMagicNumber(MagicNumber);
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
ManageTrailingStop();
}
//+------------------------------------------------------------------+
//| 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;
}
// Place the order using CTrade
if (!trade.OrderOpen(_Symbol, orderType, lot, price, sl, tp, "Breakout EA"))
{
Print("OrderSend error: ", trade.ResultRetcode(), ", ", trade.ResultRetcodeDescription());
}
}
//+------------------------------------------------------------------+
//| Cancel all existing pending orders |
//+------------------------------------------------------------------+
void CancelPendingOrders()
{
// Close positions
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
if (PositionGetInteger(POSITION_MAGIC) == MagicNumber &&
PositionGetString(POSITION_SYMBOL) == _Symbol)
{
trade.PositionClose(ticket);
}
}
}
// Delete pending orders
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
ulong ticket = OrderGetTicket(i);
if (OrderSelect(ticket))
{
if (OrderGetInteger(ORDER_MAGIC) == MagicNumber &&
OrderGetString(ORDER_SYMBOL) == _Symbol)
{
trade.OrderDelete(ticket);
}
}
}
}
//+------------------------------------------------------------------+
//| Manage trailing stops for open positions |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
if (PositionGetInteger(POSITION_MAGIC) == MagicNumber &&
PositionGetString(POSITION_SYMBOL) == _Symbol)
{
double profit = PositionGetDouble(POSITION_PROFIT);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double sl = PositionGetDouble(POSITION_SL);
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
if (profit > TrailingStopStart * _Point)
{
if (type == POSITION_TYPE_BUY)
{
double newSL = NormalizeDouble(bid - TrailingStopDistance * _Point, _Digits);
if (newSL > sl)
{
trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP));
}
}
else if (type == POSITION_TYPE_SELL)
{
double newSL = NormalizeDouble(ask + TrailingStopDistance * _Point, _Digits);
if (sl == 0 || newSL < sl)
{
trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP));
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| 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:
// 1. Read from CSV file with news times
// 2. Check current time against high impact news
// 3. Set tradeAllowed = false during news events
tradeAllowed = true; // Change to false during news events
}
Key Fixes:
-
Added Trade Library: Included <Trade\Trade.mqh> and created a CTrade object for all trade operations.
-
Fixed Order Management:
- Replaced
TradeClose with trade.PositionClose()
- Replaced
OrderCancel with trade.OrderDelete()
-
Improved Structure:
- Separated trailing stop logic into
ManageTrailingStop() function
- Used proper
PositionSelectByTicket() and OrderSelect() for position/order management
-
Fixed Type Conversion:
- Used proper ENUM types for positions
- Fixed position type casting with
(ENUM_POSITION_TYPE)
Testing Tips:
-
Pending Orders: Be sure your broker allows pending orders like BUY_LIMIT and SELL_LIMIT.
-
News Filter Implementation:
void CheckNews()
{
// Example implementation
MqlDateTime time;
TimeToStruct(TimeCurrent(), time);
// Block trading during major news (example: NFP at 8:30 AM ET on first Friday)
if (time.day_of_week == 5 && time.day <= 7 && time.hour == 8 && time.min >= 25 && time.min <= 35)
{
tradeAllowed = false;
Print("Major news event detected - trading disabled");
}
else
{
tradeAllowed = true;
}
}
This code should now compile without errors. Let me know if you need any other adjustments!