Indicators: 2 Moving Averages with Bollinger Bands

 

2 Moving Averages with Bollinger Bands:

"2 Moving Averages with Bollinger Bands" is a custom MT5 indicator that combines two configurable moving averages and optional Bollinger Bands. It generates real-time Buy and Sell arrows when crossovers occur, with optional alerts, sound, and email notifications. Suitable for all timeframes and symbols

2 Moving Averages with Bollinger Bands

Author: Mir Mostofa Kamal

 
//+------------------------------------------------------------------+
//| Expert Advisor: EMA Crossover Bot for BTCUSD & XAUUSD |
//+------------------------------------------------------------------+
#property strict

input double RiskPercent = 1.0; // Risk per trade (% of account)
input int StopLossPips = 1000; // Default SL in points (adjust for BTC/XAU)
input int TakeProfitPips = 2000; // Default TP in points
input ENUM_TIMEFRAMES Timeframe = PERIOD_D1;

string SymbolsToTrade[] = {"BTCUSD", "XAUUSD"};

int OnInit()
{
   Print("EMA Crossover EA Initialized");
   return(INIT_SUCCEEDED);
}

void OnTick()
{
   // Only run once per day (first tick of day)
   static datetime lastTime = 0;
   if (TimeCurrent() == lastTime) return;
   lastTime = TimeCurrent();

   // Skip trades on Fri, Sat, Sun
   int weekday = TimeDayOfWeek(TimeCurrent());
   if (weekday == 5 || weekday == 6 || weekday == 0) return;

   for (int i = 0; i < ArraySize(SymbolsToTrade); i++)
   {
      string symbol = SymbolsToTrade[i];
      if (!SymbolSelect(symbol, true)) continue;

      TradeSymbol(symbol);
   }
}

void TradeSymbol(string symbol)
{
   // Load EMAs
   double ema50_prev = iMA(symbol, Timeframe, 50, 0, MODE_EMA, PRICE_CLOSE, 1);
   double ema200_prev = iMA(symbol, Timeframe, 200, 0, MODE_EMA, PRICE_CLOSE, 1);
   double ema50_curr = iMA(symbol, Timeframe, 50, 0, MODE_EMA, PRICE_CLOSE, 0);
   double ema200_curr = iMA(symbol, Timeframe, 200, 0, MODE_EMA, PRICE_CLOSE, 0);

   // Check for existing trades
   if (PositionsTotal() > 0 && SymbolInOpenPositions(symbol)) return;

   // Risk-based lot size
   double lotSize = CalculateLotSize(symbol);

   // Entry conditions
   if (ema50_prev < ema200_prev && ema50_curr > ema200_curr)
   {
      // Buy
      OpenTrade(symbol, ORDER_TYPE_BUY, lotSize);
   }
   else if (ema50_prev > ema200_prev && ema50_curr < ema200_curr)
   {
      // Sell
      OpenTrade(symbol, ORDER_TYPE_SELL, lotSize);
   }
}

bool SymbolInOpenPositions(string symbol)
{
   for (int i = 0; i < PositionsTotal(); i++)
   {
      if (PositionGetSymbol(i) == symbol) return true;
   }
   return false;
}

double CalculateLotSize(string symbol)
{
   double balance = AccountBalance();
   double riskAmount = balance * RiskPercent / 100.0;
   double tickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
   double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);

   double slPoints = StopLossPips;
   double contractSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
   double lotSize = riskAmount / (slPoints * tickValue / tickSize);

   lotSize = NormalizeDouble(lotSize, 2); // Adjust precision
   return lotSize;
}

void OpenTrade(string symbol, ENUM_ORDER_TYPE type, double lotSize)
{
   double price = (type == ORDER_TYPE_BUY) ? SymbolInfoDouble(symbol, SYMBOL_ASK) : SymbolInfoDouble(symbol, SYMBOL_BID);
   double sl = (type == ORDER_TYPE_BUY) ? price - StopLossPips * _Point : price + StopLossPips * _Point;
   double tp = (type == ORDER_TYPE_BUY) ? price + TakeProfitPips * _Point : price - TakeProfitPips * _Point;

   MqlTradeRequest request = {};
   MqlTradeResult result = {};

   request.symbol = symbol;
   request.volume = lotSize;
   request.type = type;
   request.price = price;
   request.sl = sl;
   request.tp = tp;
   request.deviation = 50;
   request.magic = 123456;
   request.type_filling = ORDER_FILLING_IOC;
   request.action = TRADE_ACTION_DEAL;

   if (!OrderSend(request, result))
      Print("OrderSend failed for ", symbol, ": ", result.retcode);
   else
      Print("Trade placed for ", symbol, ": ", result.retcode);
}
 
EXCELLENT THANK YOU SIR M15 SCALPING WOW