SMC Scalping EA
SMC Scalping EA
//| SMC_Scalping_EA.mq5 |
//| Copyright 2025, Grok 3 built by xAI |
//| |
//+------------------------------------------------------------------+
#property copyright "Grok 3 built by xAI"
#property link "https://x.ai"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Calculate point value
PointValue = MarketInfo(_Symbol, MODE_POINT);
if(PointValue == 0) PointValue = 0.00001; // Fallback for brokers
// Validate inputs
if(InpRiskPercent <= 0 || InpRRRatio <= 0 || InpMaxTrades <= 0)
{
Print("Invalid input parameters!");
return(INIT_PARAMETERS_INCORRECT);
}
LastTradeTime = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if new bar on lower timeframe
if(!IsNewBar(InpTimeframeLower)) return;
//+------------------------------------------------------------------+
//| Check for new bar |
//+------------------------------------------------------------------+
bool IsNewBar(int timeframe)
{
static datetime lastBar;
datetime currentBar = iTime(_Symbol, timeframe, 0);
if(currentBar != lastBar)
{
lastBar = currentBar;
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| Count open trades |
//+------------------------------------------------------------------+
int CountOpenTrades()
{
int count = 0;
for(int i = 0; i < PositionsTotal(); i++)
{
ulong ticket = PositionGetTicket(i);
if(PositionSelectByTicket(ticket))
if(PositionGetString(POSITION_SYMBOL) == _Symbol)
count++;
}
return count;
}
//+------------------------------------------------------------------+
//| Check high-impact news (placeholder) |
//+------------------------------------------------------------------+
bool IsHighImpactNews()
{
// Implement news filter (e.g., via external calendar API or manual input)
// For simplicity, assume no news for now
return false;
}
//+------------------------------------------------------------------+
//| Check higher timeframe trend |
//+------------------------------------------------------------------+
bool IsHigherTimeframeBullish()
{
double maFast = iMA(_Symbol, InpTimeframeHigher, 20, 0, MODE_EMA, PRICE_CLOSE);
double maSlow = iMA(_Symbol, InpTimeframeHigher, 50, 0, MODE_EMA, PRICE_CLOSE);
double maFastPrev = iMA(_Symbol, InpTimeframeHigher, 20, 1, MODE_EMA,
PRICE_CLOSE);
return (maFast > maSlow && maFastPrev <= maSlow);
}
//+------------------------------------------------------------------+
//| Check SMC setup |
//+------------------------------------------------------------------+
void CheckSMCSetup(bool isBullishTrend)
{
// Get current price data
double close[], high[], low[];
ArraySetAsSeries(close, true);
ArraySetAsSeries(high, true);
ArraySetAsSeries(low, true);
CopyClose(_Symbol, InpTimeframeLower, 0, InpOBLookback + 1, close);
CopyHigh(_Symbol, InpTimeframeLower, 0, InpOBLookback + 1, high);
CopyLow(_Symbol, InpTimeframeLower, 0, InpOBLookback + 1, low);
// Trade Logic
double currentPrice = close[0];
LotSize = CalculateLotSize(InpRiskPercent, InpStopLossPips);
// Bullish Setup
if(isBullishTrend && bullishSweep && bullishMSS)
{
if(currentPrice >= bullishOB && bullishOB > 0)
OpenBuyTrade(bullishOB, "Bullish OB");
else if(currentPrice >= breakerBullish && breakerBullish > 0)
OpenBuyTrade(breakerBullish, "Bullish Breaker");
else if(currentPrice >= mitigationBullish && mitigationBullish > 0)
OpenBuyTrade(mitigationBullish, "Bullish Mitigation");
else if(currentPrice >= extBullishOB && extBullishOB > 0)
OpenBuyTrade(extBullishOB, "External Bullish OB");
}
// Bearish Setup
if(!isBullishTrend && bearishSweep && bearishMSS)
{
if(currentPrice <= bearishOB && bearishOB > 0)
OpenSellTrade(bearishOB, "Bearish OB");
else if(currentPrice <= breakerBearish && breakerBearish > 0)
OpenSellTrade(breakerBearish, "Bearish Breaker");
else if(currentPrice <= mitigationBearish && mitigationBearish > 0)
OpenSellTrade(mitigationBearish, "Bearish Mitigation");
else if(currentPrice <= extBearishOB && extBearishOB > 0)
OpenSellTrade(extBearishOB, "External Bearish OB");
}
}
//+------------------------------------------------------------------+
//| Calculate lot size based on risk |
//+------------------------------------------------------------------+
double CalculateLotSize(double riskPercent, double stopLossPips)
{
double accountBalance = AccountBalance();
double riskAmount = accountBalance * (riskPercent / 100.0);
double tickSize = MarketInfo(_Symbol, MODE_TICKSIZE);
double tickValue = MarketInfo(_Symbol, MODE_TICKVALUE);
double stopLossPoints = stopLossPips * (PointValue / tickSize);
double lotSize = riskAmount / (stopLossPoints * tickValue);
return NormalizeDouble(lotSize, 2);
}
//+------------------------------------------------------------------+
//| Open buy trade |
//+------------------------------------------------------------------+
void OpenBuyTrade(double entryPrice, string comment)
{
double sl = entryPrice - InpStopLossPips * PointValue;
double tp = entryPrice + (InpStopLossPips * InpRRRatio * PointValue);
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = LotSize;
request.type = ORDER_TYPE_BUY;
request.price = entryPrice;
request.sl = sl;
request.tp = tp;
request.comment = comment;
request.type_filling = ORDER_FILLING_IOC;
if(!OrderSend(request, result))
Print("Buy order failed: ", GetLastError());
else
LastTradeTime = TimeCurrent();
}
//+------------------------------------------------------------------+
//| Open sell trade |
//+------------------------------------------------------------------+
void OpenSellTrade(double entryPrice, string comment)
{
double sl = entryPrice + InpStopLossPips * PointValue;
double tp = entryPrice - (InpStopLossPips * InpRRRatio * PointValue);
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = LotSize;
request.type = ORDER_TYPE_SELL;
request.price = entryPrice;
request.sl = sl;
request.tp = tp;
request.comment = comment;
request.type_filling = ORDER_FILLING_IOC;
if(!OrderSend(request, result))
Print("Sell order failed: ", GetLastError());
else
LastTradeTime = TimeCurrent();
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("EA Deinitialized: ", reason);
}