0% found this document useful (0 votes)
1 views

smc bot

The document outlines an Expert Advisor (EA) for trading XAUUSD using a 1:3 risk-reward ratio based on the Average True Range (ATR). It includes functions for initializing the EA, detecting new bars, calculating lot sizes based on risk, and placing buy or sell orders based on bullish or bearish order blocks. Key parameters include ATR period, risk percentage, and multipliers for stop loss and take profit calculations.

Uploaded by

Lobo S
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

smc bot

The document outlines an Expert Advisor (EA) for trading XAUUSD using a 1:3 risk-reward ratio based on the Average True Range (ATR). It includes functions for initializing the EA, detecting new bars, calculating lot sizes based on risk, and placing buy or sell orders based on bullish or bearish order blocks. Key parameters include ATR period, risk percentage, and multipliers for stop loss and take profit calculations.

Uploaded by

Lobo S
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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

//| Expert Advisor: SMC 1:3 RR for XAUUSD |


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

#include <Trade/Trade.mqh> // Include trade functions

input int ATRPeriod = 14; // ATR Period


input double RiskPercent = 1.0; // Risk per trade (% of account)
input int SLMultiplier = 1; // Stop Loss in ATR multiples
input int TPMultiplier = 3; // Take Profit in ATR multiples

CTrade trade; // Trade object for order placement

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if a new bar has formed
if (!IsNewBar()) return;

// Get ATR using handle


int atrHandle = iATR(_Symbol, PERIOD_H1, ATRPeriod);
if (atrHandle == INVALID_HANDLE)
{
Print("Failed to create ATR handle: ", GetLastError());
return;
}
double atrBuffer[];
ArraySetAsSeries(atrBuffer, true);
CopyBuffer(atrHandle, 0, 1, 1, atrBuffer);
double atr = atrBuffer[0];
IndicatorRelease(atrHandle);

if (atr <= 0)
{
Print("Invalid ATR value: ", atr);
return;
}

double lotSize = CalculateLotSize(RiskPercent, atr * SLMultiplier * 10); //


Convert to points
if (lotSize <= 0)
{
Print("Invalid lot size: ", lotSize);
return;
}

double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);


double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double slDistance = atr * SLMultiplier * 10; // Convert ATR to points
double tpDistance = atr * TPMultiplier * 10; // Convert ATR to points

// Detect Order Block (simplified logic)


if (IsOrderBlockBullish())
{
double slPrice = ask - slDistance;
double tpPrice = ask + tpDistance;
if (slPrice > 0 && tpPrice > ask) // Ensure valid SL and TP
{
trade.Buy(lotSize, _Symbol, ask, slPrice, tpPrice, "SMC Buy");
if (trade.ResultRetcode() != TRADE_RETCODE_DONE)
Print("Buy Order failed with error #", GetLastError());
}
}
else if (IsOrderBlockBearish())
{
double slPrice = bid + slDistance;
double tpPrice = bid - tpDistance;
if (slPrice > bid && tpPrice > 0 && tpPrice < bid) // Ensure valid SL and TP
{
trade.Sell(lotSize, _Symbol, bid, slPrice, tpPrice, "SMC Sell");
if (trade.ResultRetcode() != TRADE_RETCODE_DONE)
Print("Sell Order failed with error #", GetLastError());
}
}
}

//+------------------------------------------------------------------+
//| Check if new bar has formed |
//+------------------------------------------------------------------+
bool IsNewBar()
{
static datetime lastBarTime = 0;
datetime currentBarTime = iTime(_Symbol, PERIOD_H1, 0);
if (currentBarTime != lastBarTime)
{
lastBarTime = currentBarTime;
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| Detect Bullish Order Block (example logic) |
//+------------------------------------------------------------------+
bool IsOrderBlockBullish()
{
double high = iHigh(_Symbol, PERIOD_D1, 1);
double low = iLow(_Symbol, PERIOD_D1, 1);
double prevClose = iClose(_Symbol, PERIOD_D1, 2);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
return (ask > low && ask < high && prevClose < low);
}

//+------------------------------------------------------------------+
//| Detect Bearish Order Block (example logic) |
//+------------------------------------------------------------------+
bool IsOrderBlockBearish()
{
double high = iHigh(_Symbol, PERIOD_D1, 1);
double low = iLow(_Symbol, PERIOD_D1, 1);
double prevClose = iClose(_Symbol, PERIOD_D1, 2);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
return (bid < high && bid > low && prevClose > high);
}

//+------------------------------------------------------------------+
//| Calculate Lot Size based on risk |
//+------------------------------------------------------------------+
double CalculateLotSize(double riskPercent, double slPoints)
{
double accountBalance = AccountInfoDouble(ACCOUNT_BALANCE);
if (accountBalance <= 0)
{
Print("Account balance is zero or negative: ", accountBalance);
return 0;
}

double riskAmount = accountBalance * (riskPercent / 100);


double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double contractSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE);

if (tickValue == 0 || tickSize == 0 || contractSize == 0)


{
Print("Invalid market parameters: tickValue=", tickValue, ", tickSize=",
tickSize, ", contractSize=", contractSize);
return 0;
}

double pointValue = tickValue / tickSize;


double riskPerPoint = slPoints * pointValue;
if (riskPerPoint == 0)
{
Print("Risk per point is zero, cannot calculate lot size");
return 0;
}

double lotSize = riskAmount / riskPerPoint;


lotSize = lotSize / contractSize;
return NormalizeDouble(MathMax(lotSize, 0.01), 2); // Minimum lot size 0.01
}
//+------------------------------------------------------------------+

You might also like