The errors you're encountering are due to several issues in the MQL5 code:
- Incorrect Function Calls: Functions like
iATR, iHighest, iLowest, and iZigZag are used incorrectly.
- Undefined Variables: Arrays like
Close, High, Low, and variables like Ask, Bid, ticket are not declared.
- Wrong
OrderSend Syntax: The OrderSend function in MQL5 has changed; it now requires MqlTradeRequest and MqlTradeResult structures.
- Missing ENUM Values: Constants like
OBJPROP_BACK_COLOR and OBJPROP_TRANSPARENCY should be used with their proper ENUM prefixes.
- Array Access: You're trying to access
Close[], High[], Low[] directly, but these are not predefined arrays in MQL5.
Here's the corrected and optimized version of your EA:
//+------------------------------------------------------------------+
//| RoboQuant - ICT Unicorn Strategy |
//+------------------------------------------------------------------+
#property strict
#property version "1.00"
#property indicator_chart_window
// Input Parameters
input int len = 10; // Swings
input bool iBull = true; // Bull
input color colBl = clrGreen; // Bull Color
input bool iBear = true; // Bear
input color colBr = clrRed; // Bear Color
input bool combine = false; // Combine Bull and Bear
// Position Sizing
input double riskAmount = 1000; // Risk Amount USD
input double maxPositionSize = 1000; // Max Position Size
// Targets
input double risk = 1.0; // Risk/Reward
input double reward = 2.0; // Reward
double rr = reward / risk;
input bool trail = false; // Trailing Stop
input int lenS = 5; // Trailing Stop Length
input bool showTargets = true; // Show Target Areas
input color colRisk = clrGray; // Risk Color
input color colReward = clrBlue; // Reward Color
// Global Variables
double swingHigh[], swingLow[];
int swingHighCount = 0, swingLowCount = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
ArraySetAsSeries(swingHigh, true);
ArraySetAsSeries(swingLow, true);
ArrayResize(swingHigh, len);
ArrayResize(swingLow, len);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, "FVG_");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int n = Bars - 1;
double atr = iATR(_Symbol, _Period, 14);
// Detect Swings (Simplified ZigZag Logic)
DetectSwings();
// Bull and Bear Logic
if (iBear)
{
for (int i = swingHighCount - 1; i >= 2; i--)
{
double Cy = swingHigh[i];
double Ay = swingHigh[i - 1];
double By = swingHigh[i - 2];
if (Close(1) < By && Cy > Ay)
{
double y = High(n - i);
double z = Low(n - i);
// FVG Detection
for (int j = 0; j < n - i; j++)
{
double fvgT = Low(n - j - 2);
double fvgB = High(n - j);
if (fvgB < fvgT && fvgT - fvgB > atr * 0.05)
{
// Draw FVG Box
string name = "FVG_Bear_" + IntegerToString(n - j);
DrawFVGBox(name, n - j - 2, fvgT, n - j, fvgB, colBr);
// Calculate Targets
double entryPrice = Close(0);
double stopLoss = Cy;
double targetPrice = z - (Cy - z) * rr;
double positionSize = CalculatePositionSize(entryPrice, stopLoss);
if (positionSize > 0)
OpenTrade(OP_SELL, positionSize, entryPrice, stopLoss, targetPrice, "Bear Unicorn");
break;
}
}
}
}
}
if (iBull)
{
for (int i = swingLowCount - 1; i >= 2; i--)
{
double Cy = swingLow[i];
double Ay = swingLow[i - 1];
double By = swingLow[i - 2];
if (Close(1) > By && Cy < Ay)
{
double y = High(n - i);
double z = Low(n - i);
// FVG Detection
for (int j = 0; j < n - i; j++)
{
double fvgB = High(n - j - 2);
double fvgT = Low(n - j);
if (fvgB < fvgT && fvgT - fvgB > atr * 0.05)
{
// Draw FVG Box
string name = "FVG_Bull_" + IntegerToString(n - j);
DrawFVGBox(name, n - j - 2, fvgT, n - j, fvgB, colBl);
// Calculate Targets
double entryPrice = Close(0);
double stopLoss = Cy;
double targetPrice = y + (y - Cy) * rr;
double positionSize = CalculatePositionSize(entryPrice, stopLoss);
if (positionSize > 0)
OpenTrade(OP_BUY, positionSize, entryPrice, stopLoss, targetPrice, "Bull Unicorn");
break;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Detect Swings (Simplified ZigZag) |
//+------------------------------------------------------------------+
void DetectSwings()
{
swingHighCount = 0;
swingLowCount = 0;
for (int i = len; i < Bars; i++)
{
bool isHigh = true, isLow = true;
for (int j = 1; j <= len; j++)
{
if (High(i) < High(i - j)) isHigh = false;
if (High(i) < High(i + j)) isHigh = false;
if (Low(i) > Low(i - j)) isLow = false;
if (Low(i) > Low(i + j)) isLow = false;
}
if (isHigh)
{
swingHigh[swingHighCount] = High(i);
swingHighCount++;
}
if (isLow)
{
swingLow[swingLowCount] = Low(i);
swingLowCount++;
}
}
}
//+------------------------------------------------------------------+
//| Draw FVG Box |
//+------------------------------------------------------------------+
void DrawFVGBox(string name, datetime t1, double p1, datetime t2, double p2, color clr)
{
ObjectCreate(0, name, OBJ_RECTANGLE, 0, t1, p1, t2, p2);
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_BACK, true);
ObjectSetInteger(0, name, OBJPROP_FILL_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clr);
ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
}
//+------------------------------------------------------------------+
//| Calculate Position Size |
//+------------------------------------------------------------------+
double CalculatePositionSize(double entryPrice, double stopLoss)
{
double slDistance = MathAbs(entryPrice - stopLoss);
double posSize = slDistance > 0 ? riskAmount / slDistance : 0;
return MathMin(posSize, maxPositionSize);
}
//+------------------------------------------------------------------+
//| Open Trade |
//+------------------------------------------------------------------+
void OpenTrade(int type, double volume, double entryPrice, double stopLoss, double takeProfit, string comment)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = NormalizeDouble(volume, 2);
request.type = type;
request.price = type == OP_BUY ? Ask() : Bid();
request.sl = stopLoss;
request.tp = takeProfit;
request.deviation = 5;
request.magic = 0;
request.comment = comment;
OrderSend(request, result);
if (result.retcode != TRADE_RETCODE_DONE)
{
Print("Trade failed. Error: ", result.retcode);
}
}
//+------------------------------------------------------------------+
//| Helper Functions |
//+------------------------------------------------------------------+
double Close(int shift) { return iClose(_Symbol, _Period, shift); }
double High(int shift) { return iHigh(_Symbol, _Period, shift); }
double Low(int shift) { return iLow(_Symbol, _Period, shift); }
double Ask() { return SymbolInfoDouble(_Symbol, SYMBOL_ASK); }
double Bid() { return SymbolInfoDouble(_Symbol, SYMBOL_BID); }
double iATR(string symbol, ENUM_TIMEFRAMES timeframe, int period)
{
return iATR(symbol, timeframe, period, 0);
}