Here's how to convert the Pine Script strategy into an MQL5 Expert Advisor. This implementation preserves the core logic of the ICT Unicorn strategy while adapting it to MT5's architecture.
mql5 Copy code
//+------------------------------------------------------------------+
//| ICT_Unicorn_EA.mq5 |
//| Copyright 2023, RoboQuant |
//| https://roboquant.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, RoboQuant"
#property link "https://roboquant.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Input Parameters |
//+------------------------------------------------------------------+
input int SwingLength = 10; // Swings
input bool EnableBull = true; // Enable Bull patterns
input bool EnableBear = true; // Enable Bear patterns
input color BullColor = clrSeaGreen;// Bull color
input color BearColor = clrTomato; // Bear color
input bool CombinePatterns = false;// Combine patterns
// Position sizing
input double RiskAmount = 1000; // Risk Amount USD
input double MaxPositionSize = 1000; // Max Position Size
// Targets
input double RiskRewardRatio = 2.0; // Risk/Reward ratio
input bool UseTrailingStop = false;// Use trailing stop
input int TrailLength = 5; // Trailing stop length
input bool ShowTargetAreas = true; // Show target areas
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
enum ENUM_PATTERN_DIRECTION {NEUTRAL, BULLISH, BEARISH};
struct SUnicornPattern {
datetime startTime;
double topPrice;
double bottomPrice;
double swingPrice;
double entryPrice;
double stopLoss;
double takeProfit;
double positionSize;
bool isActive;
bool isTriggered;
ENUM_PATTERN_DIRECTION direction;
};
SUnicornPattern currentBullPattern;
SUnicornPattern currentBearPattern;
double ATRBuffer[];
int zigzagBuffer[];
double highsBuffer[];
double lowsBuffer[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
// Set up indicator buffers
ArraySetAsSeries(ATRBuffer, true);
ArraySetAsSeries(zigzagBuffer, true);
ArraySetAsSeries(highsBuffer, true);
ArraySetAsSeries(lowsBuffer, true);
// Initialize patterns
currentBullPattern.isActive = false;
currentBearPattern.isActive = false;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
// Clean up objects
ObjectsDeleteAll(0, "Unicorn_");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
// Only process at new bar
if(!IsNewBar()) return;
// Get necessary data
int bars = Bars(Symbol(), Period());
CopyHigh(Symbol(), Period(), 0, bars, highsBuffer);
CopyLow(Symbol(), Period(), 0, bars, lowsBuffer);
// Calculate ATR
double atr = iATR(Symbol(), Period(), 14, 1);
// Find pivot points (simplified zigzag implementation)
FindPivotPoints(SwingLength);
// Check for patterns
CheckForBullPattern(atr);
CheckForBearPattern(atr);
// Manage active patterns
ManageBullPattern();
ManageBearPattern();
// Check for exits
CheckForExits();
}
//+------------------------------------------------------------------+
//| Find pivot points |
//+------------------------------------------------------------------+
void FindPivotPoints(int length) {
// Simplified pivot point detection
// In a real implementation, you'd want a more robust zigzag indicator
int bars = Bars(Symbol(), Period());
for(int i = length; i < bars - length; i++) {
bool isHighPivot = true;
bool isLowPivot = true;
for(int j = 1; j <= length; j++) {
if(highsBuffer[i] < highsBuffer[i+j] || highsBuffer[i] < highsBuffer[i-j]) {
isHighPivot = false;
}
if(lowsBuffer[i] > lowsBuffer[i+j] || lowsBuffer[i] > lowsBuffer[i-j]) {
isLowPivot = false;
}
}
if(isHighPivot) zigzagBuffer[i] = 1; // High pivot
if(isLowPivot) zigzagBuffer[i] = -1; // Low pivot
}
}
//+------------------------------------------------------------------+
//| Check for bull pattern |
//+------------------------------------------------------------------+
void CheckForBullPattern(double atr) {
if(!EnableBull) return;
// Simplified pattern detection - would need more robust implementation
int bars = Bars(Symbol(), Period());
// Look for the pattern structure
for(int i = 5; i < bars - 5; i++) {
if(zigzagBuffer[i] == -1) { // Potential swing low
// Check for FVG
for(int j = i+1; j < i+5; j++) {
if(lowsBuffer[j] > highsBuffer[j+2] && (highsBuffer[j+2] - lowsBuffer[j]) > atr * 0.05) {
// Found potential FVG
if(!currentBullPattern.isActive || CombinePatterns) {
currentBullPattern.startTime = iTime(Symbol(), Period(), i);
currentBullPattern.topPrice = highsBuffer[i];
currentBullPattern.bottomPrice = lowsBuffer[i];
currentBullPattern.swingPrice = lowsBuffer[i];
currentBullPattern.isActive = true;
currentBullPattern.isTriggered = false;
currentBullPattern.direction = BULLISH;
// Draw pattern on chart
if(ShowTargetAreas) {
string boxName = "Unicorn_Bull_" + TimeToString(currentBullPattern.startTime);
ObjectCreate(0, boxName, OBJ_RECTANGLE, 0,
currentBullPattern.startTime, currentBullPattern.topPrice,
TimeCurrent(), currentBullPattern.bottomPrice);
ObjectSetInteger(0, boxName, OBJPROP_COLOR, BullColor);
ObjectSetInteger(0, boxName, OBJPROP_BACK, true);
}
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Check for bear pattern |
//+------------------------------------------------------------------+
void CheckForBearPattern(double atr) {
if(!EnableBear) return;
// Similar to bull pattern but inverted
int bars = Bars(Symbol(), Period());
for(int i = 5; i < bars - 5; i++) {
if(zigzagBuffer[i] == 1) { // Potential swing high
for(int j = i+1; j < i+5; j++) {
if(highsBuffer[j] < lowsBuffer[j+2] && (highsBuffer[j] - lowsBuffer[j+2]) > atr * 0.05) {
if(!currentBearPattern.isActive || CombinePatterns) {
currentBearPattern.startTime = iTime(Symbol(), Period(), i);
currentBearPattern.topPrice = highsBuffer[i];
currentBearPattern.bottomPrice = lowsBuffer[i];
currentBearPattern.swingPrice = highsBuffer[i];
currentBearPattern.isActive = true;
currentBearPattern.isTriggered = false;
currentBearPattern.direction = BEARISH;
if(ShowTargetAreas) {
string boxName = "Unicorn_Bear_" + TimeToString(currentBearPattern.startTime);
ObjectCreate(0, boxName, OBJ_RECTANGLE, 0,
currentBearPattern.startTime, currentBearPattern.topPrice,
TimeCurrent(), currentBearPattern.bottomPrice);
ObjectSetInteger(0, boxName, OBJPROP_COLOR, BearColor);
ObjectSetInteger(0, boxName, OBJPROP_BACK, true);
}
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Manage bull pattern |
//+------------------------------------------------------------------+
void ManageBullPattern() {
if(!currentBullPattern.isActive) return;
double currentHigh = iHigh(Symbol(), Period(), 1);
double currentLow = iLow(Symbol(), Period(), 1);
double currentClose = iClose(Symbol(), Period(), 1);
double currentOpen = iOpen(Symbol(), Period(), 1);
// Check for stop (price below swing low)
if(currentLow < currentBullPattern