//+------------------------------------------------------------------+
//| ICT_Unicorn_EA.mq5 |
//| Converted from Pine Script by RoboQuant |
//| Updated for MT5 Platform |
//+------------------------------------------------------------------+
#property copyright "RoboQuant"
#property version "1.00"
#property strict
#include <Trade\Trade.mqh> // Required for trading operations
// Input parameters
input int len = 10; // Swings length
input bool iBull = true; // Enable Bull pattern
input color colBl = clrLimeGreen; // Bull color
input bool iBear = true; // Enable Bear pattern
input color colBr = clrRed; // Bear color
input bool combine = false; // Combine patterns
input double riskAmount = 1000; // Risk Amount USD
input double maxPositionSize = 1000;// Max Position Size
input double risk = 1.0; // Risk/Reward ratio
input double reward = 2.0; // Reward multiple
input bool trail = false; // Trailing stop
input int lenS = 5; // Trailing stop length
// Global variables
double atr;
double entryPrice, stopLoss, targetPrice;
bool bullActive = false, bearActive = false;
double rr = reward / risk;
CTrade trade; // Trading object
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(Bars(_Symbol, PERIOD_CURRENT) < 100) return;
MqlRates rates[];
ArraySetAsSeries(rates, true);
CopyRates(_Symbol, PERIOD_CURRENT, 0, 100, rates);
double currentATR = iATR(_Symbol, PERIOD_CURRENT, 14, 0);
atr = currentATR;
// Get pivot points
double ph = FindPivotHigh(rates, len);
double pl = FindPivotLow(rates, len);
// --- Bullish Pattern Check ---
if(iBull && !bullActive && ph != EMPTY_VALUE)
{
double fvgTop = CheckFVG(rates, true);
if(fvgTop != EMPTY_VALUE)
{
double diff = (fvgTop - ph) * rr;
targetPrice = ph + diff;
stopLoss = ph;
// Calculate position size
double lotSize = CalculatePositionSize(SymbolInfoDouble(_Symbol, SYMBOL_ASK), stopLoss);
if(lotSize > 0)
{
// Normalize lot size to broker's minimum step
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathFloor(lotSize / step) * step;
// Close existing position if needed
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
trade.PositionClose(_Symbol);
}
// Open new position
trade.Buy(lotSize, _Symbol, 0, stopLoss, targetPrice, "Bull Unicorn");
bullActive = true;
}
}
}
// --- Bearish Pattern Check ---
if(iBear && !bearActive && pl != EMPTY_VALUE)
{
double fvgBottom = CheckFVG(rates, false);
if(fvgBottom != EMPTY_VALUE)
{
double diff = (pl - fvgBottom) * rr;
targetPrice = pl - diff;
stopLoss = pl;
// Calculate position size
double lotSize = CalculatePositionSize(SymbolInfoDouble(_Symbol, SYMBOL_BID), stopLoss);
if(lotSize > 0)
{
// Normalize lot size
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
lotSize = MathFloor(lotSize / step) * step;
// Close existing position
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
trade.PositionClose(_Symbol);
}
// Open new position
trade.Sell(lotSize, _Symbol, 0, stopLoss, targetPrice, "Bear Unicorn");
bearActive = true;
}
}
}
// --- Trailing Stop Logic ---
if(trail)
{
for(int i = PositionsTotal()-1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket <= 0) continue;
string comment = PositionGetString(POSITION_COMMENT);
if(comment == "Bull Unicorn" || comment == "Bear Unicorn")
{
double currentSL = PositionGetDouble(POSITION_SL);
double newSL = EMPTY_VALUE;
if(comment == "Bull Unicorn")
newSL = CalculateTrailingStop(rates, true);
else
newSL = CalculateTrailingStop(rates, false);
if(newSL != EMPTY_VALUE)
{
if((comment == "Bull Unicorn" && newSL > currentSL) ||
(comment == "Bear Unicorn" && newSL < currentSL))
{
trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP));
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Position size calculation |
//+------------------------------------------------------------------+
double CalculatePositionSize(double entry, double sl)
{
double slDistance = MathAbs(entry - sl);
if(slDistance <= 0) return 0;
// Get account currency info
double tickValue;
if(!SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE, tickValue))
return 0;
double riskPerPip = riskAmount / slDistance;
double lotSize = riskPerPip / (tickValue * 10); // Assuming 10 points per pip
return MathMin(lotSize, maxPositionSize);
}
//+------------------------------------------------------------------+
//| FVG Check |
//+------------------------------------------------------------------+
double CheckFVG(const MqlRates &rates[], bool isBull)
{
for(int i = 0; i < 5; i++)
{
if(isBull)
{
if(rates[i+2].low < rates[i].high &&
((rates[i+2].low < rates[i].high && rates[i+2].low > rates[i].low) ||
(rates[i].high < rates[i].high && rates[i].high > rates[i].low)))
{
return rates[i].high;
}
}
else
{
if(rates[i+2].high > rates[i].low &&
((rates[i+2].high < rates[i].high && rates[i+2].high > rates[i].low) ||
(rates[i].low < rates[i].high && rates[i].low > rates[i].low)))
{
return rates[i].low;
}
}
}
return EMPTY_VALUE;
}
//+------------------------------------------------------------------+
//| Trailing Stop Calculation |
//+------------------------------------------------------------------+
double CalculateTrailingStop(const MqlRates &rates[], bool isBull)
{
double pivot = isBull ? FindPivotLow(rates, lenS) : FindPivotHigh(rates, lenS);
return pivot;
}
//+------------------------------------------------------------------+
//| Pivot High Calculation |
//+------------------------------------------------------------------+
double FindPivotHigh(const MqlRates &rates[], int left)
{
double max = rates[1].high;
for(int i = 2; i <= left; i++)
if(rates[i].high > max) max = rates[i].high;
return max;
}
//+------------------------------------------------------------------+
//| Pivot Low Calculation |
//+------------------------------------------------------------------+
double FindPivotLow(const MqlRates &rates[], int left)
{
double min = rates[1].low;
for(int i = 2; i <= left; i++)
if(rates[i].low < min) min = rates[i].low;
return min;
}
//+------------------------------------------------------------------+
//| ICT_Unicorn_EA.mq5 |
//| Converted from Pine Script by RoboQuant |
//| Updated for MT5 Platform |
//+------------------------------------------------------------------+
#property copyright "RoboQuant"
#property version "1.00"
#property strict
#include <Trade\Trade.mqh> // Include Trade library for CTrade
// Input parameters
input int len = 10; // Swings length
input bool iBull = true; // Enable Bull pattern
input color colBl = clrLimeGreen; // Bull color
input bool iBear = true; // Enable Bear pattern
input color colBr = clrRed; // Bear color
input bool combine = false; // Combine patterns
input double riskAmount = 1000; // Risk Amount USD
input double maxPositionSize = 1000;// Max Position Size
input double risk = 1.0; // Risk/Reward ratio
input double reward = 2.0; // Reward multiple
input bool trail = false; // Trailing stop
input int lenS = 5; // Trailing stop length
// Global variables
double atr;
double entryPrice, stopLoss, targetPrice;
bool bullActive = false, bearActive = false;
double rr = reward / risk;
CTrade trade; // Added CTrade object
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(Bars(_Symbol, Period()) < 100) return; // Use Period() instead of PERIOD_CURRENT
MqlRates rates[];
ArraySetAsSeries(rates, true);
CopyRates(_Symbol, Period(), 0, 100, rates); // Use Period() for timeframe
atr = iATR(_Symbol, Period(), 14, 0); // Use Period()
double ph = FindPivotHigh(rates, len, 1);
double pl = FindPivotLow(rates, len, 1);
// Bull pattern logic
if(iBull && !bullActive && ph != EMPTY_VALUE)
{
double fvgTop = CheckFVG(rates, true); // Pass rates array
if(fvgTop != EMPTY_VALUE)
{
double diff = (fvgTop - ph) * rr;
targetPrice = ph + diff;
stopLoss = ph;
double lotSize = CalculatePositionSize(SymbolInfoDouble(_Symbol, SYMBOL_ASK), stopLoss);
if(lotSize > 0)
{
double lotSizeInLots = lotSize / SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN); // Use SymbolInfoDouble
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
// Close existing position
ulong ticket = PositionGetTicket(0);
if(ticket > 0) trade.PositionClose(ticket);
}
}
trade.Buy(lotSizeInLots, _Symbol, 0, 0, 0, "Bull Unicorn");
trade.Modify(trade.PositionGetTicket(), 0, stopLoss, targetPrice);
bullActive = true;
}
}
}
// Bear pattern logic (similar corrections)
if(iBear && !bearActive && pl != EMPTY_VALUE)
{
double fvgBottom = CheckFVG(rates, false);
if(fvgBottom != EMPTY_VALUE)
{
double diff = (pl - fvgBottom) * rr;
targetPrice = pl - diff;
stopLoss = pl;
double lotSize = CalculatePositionSize(SymbolInfoDouble(_Symbol, SYMBOL_BID), stopLoss);
if(lotSize > 0)
{
double lotSizeInLots = lotSize / SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(PositionSelect(_Symbol))
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
ulong ticket = PositionGetTicket(0);
if(ticket > 0) trade.PositionClose(ticket);
}
}
trade.Sell(lotSizeInLots, _Symbol, 0, 0, 0, "Bear Unicorn");
trade.Modify(trade.PositionGetTicket(), 0, stopLoss, targetPrice);
bearActive = true;
}
}
}
// Trailing stop logic (simplified)
if(trail)
{
for(int i = PositionsTotal()-1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket > 0 && PositionSelectByTicket(ticket))
{
string comment = PositionGetString(POSITION_COMMENT);
if(comment == "Bull Unicorn" || comment == "Bear Unicorn")
{
double currentSL = PositionGetDouble(POSITION_SL);
double newSL = CalculateTrailingStop(rates, comment == "Bull Unicorn");
if(newSL != EMPTY_VALUE &&
(comment == "Bull Unicorn" ? newSL > currentSL : newSL < currentSL))
{
trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP));
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Custom functions |
//+------------------------------------------------------------------+
double CalculatePositionSize(double entry, double sl)
{
double slDistance = MathAbs(entry - sl);
if(slDistance <= 0) return 0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double riskPerPip = riskAmount / slDistance;
double lotSize = riskPerPip / (tickValue * 10); // Adjusted calculation
return MathMin(lotSize, maxPositionSize / SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN));
}
double CheckFVG(MqlRates &rates[], bool isBull) // Corrected parameter
{
for(int i = 0; i < 5; i++)
{
if(isBull)
{
if(rates[i+2].low < rates[i].high &&
((rates[i+2].low < rates[i].high && rates[i+2].low > rates[i].low) ||
(rates[i].high < rates[i].high && rates[i].high > rates[i].low)))
{
return rates[i].high;
}
}
else
{
if(rates[i+2].high > rates[i].low &&
((rates[i+2].high < rates[i].high && rates[i+2].high > rates[i].low) ||
(rates[i].low < rates[i].high && rates[i].low > rates[i].low)))
{
return rates[i].low;
}
}
}
return EMPTY_VALUE;
}
double CalculateTrailingStop(MqlRates &rates[], bool isBull) // Corrected parameter
{
double pivot = isBull ? FindPivotLow(rates, lenS, lenS) : FindPivotHigh(rates, lenS, lenS);
return (pivot != EMPTY_VALUE) ? pivot : EMPTY_VALUE;
}
double FindPivotHigh(MqlRates &rates[], int left, int right)
{
double max = rates[right].high;
for(int i = right-1; i >= left; i--)
if(rates[i].high > max) max = rates[i].high;
return max;
}
double FindPivotLow(MqlRates &rates[], int left, int right)
{
double min = rates[right].low;
for(int i = right-1; i >= left; i--)
if(rates[i].low < min) min = rates[i].low;
return min;
}