Converting the TradingView Pine Script into an MT5 Expert Advisor (EA) involves translating the logic into MQL5, MetaTrader 5's programming language. Below is the MQL5 version of the provided Pine Script. This EA will implement the same logic, including position sizing, risk management, and the unicorn pattern detection.
//+------------------------------------------------------------------+
//| RoboQuant - ICT Unicorn Strategy |
//| Based on the provided Pine Script |
//+------------------------------------------------------------------+
#property strict
// 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
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialization code (if needed)
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Deinitialization code (if needed)
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Variables
int n = Bars - 1;
double atr = iATR(Symbol(), 0, 14, 1);
double ph = iZigZag(Symbol(), 0, len, len, 0, MODE_HIGH, 0);
double pl = iZigZag(Symbol(), 0, len, len, 0, MODE_LOW, 0);
double phTsL = iZigZag(Symbol(), 0, lenS, lenS, 0, MODE_HIGH, 0);
double plTsL = iZigZag(Symbol(), 0, lenS, lenS, 0, MODE_LOW, 0);
double highest = iHighest(Symbol(), 0, 5, 0);
double lowest = iLowest(Symbol(), 0, 5, 0);
// Unicorn Pattern Detection
if (ph > 0 && iBear)
{
// Bear Pattern Logic
double Cy = ph;
double Ay = ph[len];
double By = ph[len * 2];
double Ax = ph[len * 3];
if (Close[len] < By && Cy > Ay && n - len > (len > 1 ? 1 : 0) && len - len > (len > 1 ? 1 : 0) && len * 2 - Ax > (len > 1 ? 1 : 0))
{
// Bear Pattern Found
double y = High[n - len];
double z = Low[n - len];
// FVG Detection
for (int i = 0; i < n - len; i++)
{
double fvgT = Low[i + 2];
double fvgB = High[i];
if (fvgB < fvgT && ((fvgT < y && fvgT > z) || (fvgB < y && fvgB > z)) && fvgT - fvgB > atr * 0.05)
{
// Draw FVG Box
ObjectCreate("FVG_Bear", OBJ_RECTANGLE, 0, n - i - 2, Low[i + 2], n - 1, High[i]);
ObjectSetInteger(0, OBJPROP_COLOR, colBr);
ObjectSetInteger(0, OBJPROP_BACK, true);
ObjectSetInteger(0, OBJPROP_BACK_COLOR, colBr);
ObjectSetInteger(0, OBJPROP_TRANSPARENCY, 75);
// Calculate Target and Stop Loss
double btm = z;
double swing = Cy;
double TaR = btm - (swing - btm) * rr;
double TsL = swing;
// Enter Short Position
double entryPrice = Close[0];
double stopLoss = swing;
double positionSize = CalculatePositionSize(entryPrice, stopLoss);
if (positionSize > 0)
{
if (OrdersTotal() == 0 || combine)
{
ticket = OrderSend(Symbol(), OP_SELL, positionSize, Ask, 3, stopLoss, TaR, "Bear Unicorn", 0, 0, clrRed);
}
}
break;
}
}
}
}
if (pl > 0 && iBull)
{
// Bull Pattern Logic
double Cy = pl;
double Ay = pl[len];
double By = pl[len * 2];
double Ax = pl[len * 3];
if (Close[len] > By && Cy < Ay && n - len > (len > 1 ? 1 : 0) && len - len > (len > 1 ? 1 : 0) && len * 2 - Ax > (len > 1 ? 1 : 0))
{
// Bull Pattern Found
double y = High[n - len];
double z = Low[n - len];
// FVG Detection
for (int i = 0; i < n - len; i++)
{
double fvgB = High[i + 2];
double fvgT = Low[i];
if (fvgB < fvgT && ((fvgT < y && fvgT > z) || (fvgB < y && fvgB > z)) && fvgT - fvgB > atr * 0.05)
{
// Draw FVG Box
ObjectCreate("FVG_Bull", OBJ_RECTANGLE, 0, n - i - 2, Low[i], n - 1, High[i + 2]);
ObjectSetInteger(0, OBJPROP_COLOR, colBl);
ObjectSetInteger(0, OBJPROP_BACK, true);
ObjectSetInteger(0, OBJPROP_BACK_COLOR, colBl);
ObjectSetInteger(0, OBJPROP_TRANSPARENCY, 75);
// Calculate Target and Stop Loss
double top = y;
double swing = Cy;
double TaR = top + (top - swing) * rr;
double TsL = swing;
// Enter Long Position
double entryPrice = Close[0];
double stopLoss = swing;
double positionSize = CalculatePositionSize(entryPrice, stopLoss);
if (positionSize > 0)
{
if (OrdersTotal() == 0 || combine)
{
ticket = OrderSend(Symbol(), OP_BUY, positionSize, Bid, 3, stopLoss, TaR, "Bull Unicorn", 0, 0, clrGreen);
}
}
break;
}
}
}
}
}
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
//| Custom Indicator Functions (if needed) |
//+------------------------------------------------------------------+
double iZigZag(string symbol, int timeframe, int len, int ext, int mode, int type, int shift)
{
// Custom ZigZag implementation (simplified for example)
// In a real scenario, use a custom indicator or built-in ZigZag
return 0;
}
//+------------------------------------------------------------------+
Key Points:
- Pattern Detection: The EA detects bull and bear patterns based on ZigZag indicators and FVG (Fair Value Gap) conditions.
- Position Sizing: The
CalculatePositionSize function ensures the position size respects the risk amount and maximum position size.
- Order Management: The EA sends market orders with stop-loss and take-profit levels based on the detected patterns.
- Visualization: FVG boxes are drawn on the chart for visual confirmation of the patterns.
Notes:
- ZigZag Indicator: The
iZigZag function is a placeholder. You may need to implement a custom ZigZag indicator or use a built-in one in MT5.
- Error Handling: Add error handling for
OrderSend and other critical functions to ensure robustness.
- Optimization: Consider optimizing the EA for performance, especially if running on multiple symbols or timeframes.
Let me know if you need further assistance or enhancements! 🚀