[go: up one dir, main page]

0% found this document useful (0 votes)
456 views5 pages

SMC Scalping EA

The document is a code for a MetaTrader 5 Expert Advisor (EA) named SMC_Scalping_EA, designed for scalping trades based on specific market conditions. It includes parameters for risk management, trade limits, and news avoidance, and implements logic for detecting market structure changes and executing trades accordingly. The EA calculates lot sizes based on account risk and incorporates mechanisms for opening buy and sell trades based on identified setups.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
456 views5 pages

SMC Scalping EA

The document is a code for a MetaTrader 5 Expert Advisor (EA) named SMC_Scalping_EA, designed for scalping trades based on specific market conditions. It includes parameters for risk management, trade limits, and news avoidance, and implements logic for detecting market structure changes and executing trades accordingly. The EA calculates lot sizes based on account risk and incorporates mechanisms for opening buy and sell trades based on identified setups.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//+------------------------------------------------------------------+

//| 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"

//--- Input Parameters


input int InpTimeframeLower = PERIOD_M5; // Lower Timeframe (M5 or M15)
input int InpTimeframeHigher = PERIOD_H1; // Higher Timeframe (H1)
input double InpRiskPercent = 1.0; // Risk per Trade (% of Account)
input double InpRRRatio = 2.0; // Risk-to-Reward Ratio
input int InpMaxTrades = 3; // Max Open Trades
input int InpOBLookback = 50; // Lookback for Order Blocks
input double InpStopLossPips = 10.0; // Default Stop Loss (pips)
input bool InpAvoidNews = true; // Avoid High-Impact News

//--- Global Variables


double PointValue; // Point value for current symbol
double LotSize; // Calculated lot size
int TotalTrades; // Current open trades
datetime LastTradeTime; // Time of last trade

//+------------------------------------------------------------------+
//| 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;

// Count open trades


TotalTrades = CountOpenTrades();
if(TotalTrades >= InpMaxTrades) return;

// Avoid news if enabled


if(InpAvoidNews && IsHighImpactNews()) return;
// Get trend from higher timeframe
bool isBullishTrend = IsHigherTimeframeBullish();

// Check for SMC setups


CheckSMCSetup(isBullishTrend);
}

//+------------------------------------------------------------------+
//| 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);

// Detect liquidity sweep and MSS


bool bullishSweep = false, bearishSweep = false;
bool bullishMSS = false, bearishMSS = false;

// Liquidity Sweep: Price breaks recent high/low


double recentHigh = high[1];
double recentLow = low[1];
if(close[0] > recentHigh) bearishSweep = true; // Sweeps high, potential bearish
if(close[0] < recentLow) bullishSweep = true; // Sweeps low, potential bullish

// MSS: Break of structure


for(int i = 2; i < InpOBLookback; i++)
{
if(high[i] > high[i-1] && close[0] > high[i]) bullishMSS = true;
if(low[i] < low[i-1] && close[0] < low[i]) bearishMSS = true;
}

// Find Order Blocks


double bullishOB = 0, bearishOB = 0;
for(int i = 2; i < InpOBLookback; i++)
{
// Bullish OB: Last bearish candle before strong bullish move
if(close[i] < close[i+1] && close[i+1] > high[i-1])
bullishOB = low[i];
// Bearish OB: Last bullish candle before strong bearish move
if(close[i] > close[i+1] && close[i+1] < low[i-1])
bearishOB = high[i];
}

// External OB: Swing high/low


double extBullishOB = iLow(_Symbol, InpTimeframeHigher, iLowest(_Symbol,
InpTimeframeHigher, MODE_LOW, 20, 1));
double extBearishOB = iHigh(_Symbol, InpTimeframeHigher, iHighest(_Symbol,
InpTimeframeHigher, MODE_HIGH, 20, 1));

// Breaker Block: Failed OB after MSS


double breakerBullish = 0, breakerBearish = 0;
if(bearishMSS && bullishOB > 0 && close[0] < bullishOB)
breakerBearish = bullishOB; // Failed bullish OB becomes bearish breaker
if(bullishMSS && bearishOB > 0 && close[0] > bearishOB)
breakerBullish = bearishOB; // Failed bearish OB becomes bullish breaker
// Mitigation Block: Partially consumed OB
double mitigationBullish = bullishOB > 0 && close[0] > bullishOB && close[0] <
(bullishOB + InpStopLossPips * PointValue) ? bullishOB : 0;
double mitigationBearish = bearishOB > 0 && close[0] < bearishOB && close[0] >
(bearishOB - InpStopLossPips * PointValue) ? bearishOB : 0;

// 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);
}

You might also like