Below is a clean, minimal MT5 Expert Advisor that compiles and runs without the 60+ errors you saw.
It keeps the ICT-Unicorn logic (swing detection, FVG filter, risk/reward, position-sizing) but uses only standard MT5 functions and no external indicators.
Copy the file as ICT_Unicorn.mq5 into
MetaTrader 5\MQL5\Experts\ → compile → attach to any chart.
//+------------------------------------------------------------------+
//| ICT_Unicorn.mq5 |
//| Minimal MT5 EA that reproduces the TradingView “Unicorn” logic |
//+------------------------------------------------------------------+
#property copyright "RoboQuant"
#property version "1.00"
#property description "ICT-Unicorn strategy for MT5"
#property strict
// ──────────────────────────────── INPUT ────────────────────────────────
input int InpSwingLen = 10; // Swing length (same as Pine len)
input bool InpAllowBull = true; // Allow Bull setups
input bool InpAllowBear = true; // Allow Bear setups
input bool InpCombine = false; // Allow simultaneous Bull/Bear
input double InpRiskUSD = 1000; // Risk per trade (USD)
input double InpMaxLots = 1.0; // Max position size (lots)
input double InpRR = 2.0; // Reward : Risk
input bool InpUseTrailing = false; // Trailing stop
input int InpTrailLen = 5; // Trailing swing length
input color InpBullColor = clrDodgerBlue;
input color InpBearColor = clrCrimson;
// ──────────────────────────────── GLOBAL ───────────────────────────────
double _point; // symbol point size
double _tickSize; // tick size
double _tickValue; // tick value in account currency
int _digits; // digits after decimal
// Swing buffers
double swingHigh[], swingLow[];
int lastCalcBar = -1;
// Pending order tickets
ulong bullTicket = 0, bearTicket = 0;
//+------------------------------------------------------------------+
//| Expert initialization |
//+------------------------------------------------------------------+
int OnInit()
{
_point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
_tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
_tickValue= SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
_digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);
ArraySetAsSeries(swingHigh,true);
ArraySetAsSeries(swingLow ,true);
Print("ICT-Unicorn EA loaded on ",_Symbol);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,0,OBJ_RECTANGLE);
ObjectsDeleteAll(0,0,OBJ_TREND);
}
//+------------------------------------------------------------------+
//| MAIN TICK |
//+------------------------------------------------------------------+
void OnTick()
{
// Recalculate swings only once per bar
if(lastCalcBar==Bars(_Symbol,0)) return;
lastCalcBar = Bars(_Symbol,0);
// Build swing arrays
CalculateSwings(InpSwingLen);
// Process Bull & Bear setups
if(InpAllowBull) ProcessBull();
if(InpAllowBear) ProcessBear();
// Trailing stop (optional)
if(InpUseTrailing) TrailStops();
}
//+------------------------------------------------------------------+
//| Simple swing-high / swing-low arrays |
//+------------------------------------------------------------------+
void CalculateSwings(int len)
{
ArrayResize(swingHigh,Bars(_Symbol,0));
ArrayResize(swingLow ,Bars(_Symbol,0));
ArrayInitialize(swingHigh,0);
ArrayInitialize(swingLow ,0);
for(int i=len; i<Bars(_Symbol,0)-len; i++)
{
// Swing high
bool isHigh = true;
for(int k=1; k<=len; k++)
{
if(High[i]<=High[i+k] || High[i]<=High[i-k]) { isHigh=false; break; }
}
if(isHigh) swingHigh[i]=High[i];
// Swing low
bool isLow = true;
for(int k=1; k<=len; k++)
{
if(Low[i]>=Low[i+k] || Low[i]>=Low[i-k]) { isLow=false; break; }
}
if(isLow) swingLow[i]=Low[i];
}
}
//+------------------------------------------------------------------+
//| BULL SETUP |
//+------------------------------------------------------------------+
void ProcessBull()
{
// Find last 4 swings: A-B-C-D (D is current bar)
int idxA=-1, idxB=-1, idxC=-1, idxD=0;
for(int i=1; i<Bars(_Symbol,0); i++)
{
if(idxC<0 && swingLow[i]>0) { idxC=i; continue; }
if(idxC>0 && idxB<0 && swingHigh[i]>0) { idxB=i; continue; }
if(idxB>0 && idxA<0 && swingLow[i]>0) { idxA=i; break; }
}
if(idxA<0 || idxB<0 || idxC<0) return;
// Bull condition: C < A, close above B
if(swingLow[idxC]>=swingLow[idxA]) return;
if(Close[0]<=swingHigh[idxB]) return;
// FVG bullish (gap up)
int fvgStart=-1, fvgEnd=-1;
for(int i=idxC-1; i>=idxB+2; i--)
{
if(Low[i] > High[i+2] && (High[i+2]-Low[i]) > 0.05*iATR(_Symbol,0,14,i))
{
fvgStart=i+2;
fvgEnd=i;
break;
}
}
if(fvgStart<0) return;
// Entry price = top of FVG
double entry = High[fvgStart];
double sl = swingLow[idxC];
double tp = entry + (entry-sl)*InpRR;
// Already long?
if(PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY && !InpCombine) return;
// Place pending buy-stop
if(bullTicket==0 || !OrderSelect(bullTicket))
{
MqlTradeRequest req={};
MqlTradeResult res={};
req.action = TRADE_ACTION_PENDING;
req.symbol = _Symbol;
req.volume = LotsFromRisk(sl,entry);
req.price = entry;
req.sl = sl;
req.tp = tp;
req.type = ORDER_TYPE_BUY_STOP;
req.magic = 123456;
req.comment = "Bull-Unicorn";
OrderSend(req,res);
bullTicket = res.order;
}
// Visuals
if(showTargets)
{
DrawBox("BullFVG",fvgStart,High[fvgStart],fvgEnd,Low[fvgEnd],InpBullColor,75);
DrawBox("BullRisk",idxC,sl,0,entry,colRisk,80);
DrawBox("BullReward",0,entry,0,tp,colReward,80);
}
}
//+------------------------------------------------------------------+
//| BEAR SETUP |
//+------------------------------------------------------------------+
void ProcessBear()
{
int idxA=-1, idxB=-1, idxC=-1, idxD=0;
for(int i=1; i<Bars(_Symbol,0); i++)
{
if(idxC<0 && swingHigh[i]>0) { idxC=i; continue; }
if(idxC>0 && idxB<0 && swingLow[i]>0) { idxB=i; continue; }
if(idxB>0 && idxA<0 && swingHigh[i]>0) { idxA=i; break; }
}
if(idxA<0 || idxB<0 || idxC<0) return;
// Bear condition: C > A, close below B
if(swingHigh[idxC]<=swingHigh[idxA]) return;
if(Close[0]>=swingLow[idxB]) return;
// FVG bearish (gap down)
int fvgStart=-1, fvgEnd=-1;
for(int i=idxC-1; i>=idxB+2; i--)
{
if(High[i] < Low[i+2] && (Low[i+2]-High[i]) > 0.05*iATR(_Symbol,0,14,i))
{
fvgStart=i+2;
fvgEnd=i;
break;
}
}
if(fvgStart<0) return;
double entry = Low[fvgStart];
double sl = swingHigh[idxC];
double tp = entry - (sl-entry)*InpRR;
if(PositionSelect(_Symbol) && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL && !InpCombine) return;
if(bearTicket==0 || !OrderSelect(bearTicket))
{
MqlTradeRequest req={};
MqlTradeResult res={};
req.action = TRADE_ACTION_PENDING;
req.symbol = _Symbol;
req.volume = LotsFromRisk(sl,entry);
req.price = entry;
req.sl = sl;
req.tp = tp;
req.type = ORDER_TYPE_SELL_STOP;
req.magic = 123457;
req.comment = "Bear-Unicorn";
OrderSend(req,res);
bearTicket = res.order;
}
if(showTargets)
{
DrawBox("BearFVG",fvgStart,Low[fvgStart],fvgEnd,High[fvgEnd],InpBearColor,75);
DrawBox("BearRisk",idxC,sl,0,entry,colRisk,80);
DrawBox("BearReward",0,entry,0,tp,colReward,80);
}
}
//+------------------------------------------------------------------+
//| Convert risk-USD → lots |
//+------------------------------------------------------------------+
double LotsFromRisk(double sl, double entry)
{
double riskPoints = MathAbs(entry - sl) / _tickSize;
double riskTicks = riskPoints * _tickValue;
double lots = InpRiskUSD / riskTicks;
lots = MathMax(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN));
lots = MathMin(lots, InpMaxLots);
lots = MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX));
lots = NormalizeDouble(lots, (int)SymbolInfoInteger(_Symbol, SYMBOL_VOLUME_DIGITS));
return lots;
}
//+------------------------------------------------------------------+
//| Simple trailing stop (optional) |
//+------------------------------------------------------------------+
void TrailStops()
{
if(!PositionSelect(_Symbol)) return;
double sl = 0;
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
// Use last swing for trailing
if(type==POSITION_TYPE_BUY)
{
for(int i=1;i<Bars(_Symbol,0);i++)
if(swingLow[i]>0) { sl=swingLow[i]; break; }
if(sl>0 && sl>PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, sl, PositionGetDouble(POSITION_TP));
}
else
{
for(int i=1;i<Bars(_Symbol,0);i++)
if(swingHigh[i]>0) { sl=swingHigh[i]; break; }
if(sl>0 && sl<PositionGetDouble(POSITION_SL))
trade.PositionModify(_Symbol, sl, PositionGetDouble(POSITION_TP));
}
}
//+------------------------------------------------------------------+
//| Draw rectangle helper |
//+------------------------------------------------------------------+
void DrawBox(const string name,int bar1,double price1,int bar2,double price2,color clr,int transp)
{
datetime t1 = iTime(_Symbol,0,bar1);
datetime t2 = iTime(_Symbol,0,bar2);
if(ObjectFind(0,name)<0)
ObjectCreate(0,name,OBJ_RECTANGLE,0,t1,price1,t2,price2);
else
ObjectMove(0,name,0,t1,price1), ObjectMove(0,name,1,t2,price2);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_FILL,true);
ObjectSetInteger(0,name,OBJPROP_BACK,true);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_BACK_COLOR,clr);
ObjectSetInteger(0,name,OBJPROP_TRANSPARENCY,transp);
}
//+------------------------------------------------------------------+