//+------------------------------------------------------------------+
//|                                                      ScalpingEA.mq5 |
//|                        Copyright 2024, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict

// Input parameters for the expert
input double lotSize = 0.1;
input int emaPeriod = 5;
input int smaPeriod = 20;
input int rsiPeriod = 14;
input int bollingerPeriod = 20;
input double bollingerDeviation = 2.0;
input double stopLossPips = 10;
input double takeProfitPips = 20;

// Handles for the indicators
int EMAHandle;
int SMAHandle;
int RSIHandle;
int BBHandle;

// Define ORDER_BUY and ORDER_SELL if not already defined
#ifndef ORDER_BUY
#define ORDER_BUY 0
#endif

#ifndef ORDER_SELL
#define ORDER_SELL 1
#endif

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Initialize the indicators
   EMAHandle = iMA(_Symbol, _Period, emaPeriod, 0, MODE_EMA, PRICE_CLOSE);
   SMAHandle = iMA(_Symbol, _Period, smaPeriod, 0, MODE_SMA, PRICE_CLOSE);
   RSIHandle = iRSI(_Symbol, _Period, rsiPeriod, PRICE_CLOSE);
   BBHandle = iBands(_Symbol, _Period, bollingerPeriod, 0, bollingerDeviation, PRICE_CLOSE);

   // Check if the indicators were created successfully
   if (EMAHandle == INVALID_HANDLE || SMAHandle == INVALID_HANDLE || RSIHandle == INVALID_HANDLE || BBHandle == INVALID_HANDLE)
     {
      Print("Erro ao criar os indicadores.");
      return (INIT_FAILED);
     }

   return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Release the indicator handles
   IndicatorRelease(EMAHandle);
   IndicatorRelease(SMAHandle);
   IndicatorRelease(RSIHandle);
   IndicatorRelease(BBHandle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double emaCurrent = 0.0;
   double smaCurrent = 0.0;
   double rsiCurrent = 0.0;
   double bbUpperCurrent = 0.0, bbLowerCurrent = 0.0;
   double bandsBuffer[];

   if (CopyBuffer(EMAHandle, 0, 0, 1, bandsBuffer) > 0)
      emaCurrent = bandsBuffer[0];
   if (CopyBuffer(SMAHandle, 0, 0, 1, bandsBuffer) > 0)
      smaCurrent = bandsBuffer[0];
   if (CopyBuffer(RSIHandle, 0, 0, 1, bandsBuffer) > 0)
      rsiCurrent = bandsBuffer[0];
   if (CopyBuffer(BBHandle, 0, 0, 1, bandsBuffer) > 0)
      bbUpperCurrent = bandsBuffer[0];
   if (CopyBuffer(BBHandle, 1, 0, 1, bandsBuffer) > 0)
      bbLowerCurrent = bandsBuffer[0];

   double price = iClose(_Symbol, _Period, 0);

   // Check for open positions to avoid duplicate trades
   bool isPositionOpen = false;
   for (int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if (PositionSelect(_Symbol))
        {
         if (PositionGetInteger(POSITION_MAGIC) == 0)
           {
            isPositionOpen = true;
            break;
           }
        }
     }

   // Check buy conditions
   if (emaCurrent > smaCurrent && rsiCurrent < 70 && price < bbUpperCurrent && !isPositionOpen)
     {
      // Open buy order
      OpenOrder(ORDER_BUY, lotSize, stopLossPips, takeProfitPips);
     }

   // Check sell conditions
   if (emaCurrent < smaCurrent && rsiCurrent > 30 && price > bbLowerCurrent && !isPositionOpen)
     {
      // Open sell order
      OpenOrder(ORDER_SELL, lotSize, stopLossPips, takeProfitPips);
     }
  }
//+------------------------------------------------------------------+
//| Function to open orders                                          |
//+------------------------------------------------------------------+
void OpenOrder(int tradeType, double tradeLotSize, double slPips, double tpPips)
  {
   double price, sl, tp;

   if (tradeType == ORDER_BUY)
     {
      price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      sl = price - slPips * _Point;
      tp = price + tpPips * _Point;
     }
   else
     {
      price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
      sl = price + slPips * _Point;
      tp = price - tpPips * _Point;
     }

   // Validate Stop Loss and Take Profit levels
   double stopLevel;
   if (!SymbolInfoDouble(_Symbol, SYMBOL_TRADE_STOPS_LEVEL, stopLevel))
     {
      Print("Erro ao obter nível de Stop Loss.");
      return;
     }
   double minSLTPDistance = stopLevel * _Point;

   if (MathAbs(sl - price) < minSLTPDistance || MathAbs(tp - price) < minSLTPDistance)
     {
      Print("Stop Loss ou Take Profit estão muito próximos do preço atual.");
      return;
     }

   MqlTradeRequest request;
   MqlTradeResult result;

   ZeroMemory(request);
   ZeroMemory(result);

   request.action = TRADE_ACTION_DEAL;
   request.symbol = _Symbol;
   request.volume = tradeLotSize;
   request.type = tradeType;
   request.price = price;
   request.sl = sl;
   request.tp = tp;
   request.deviation = 10;  // Ajuste a variação conforme necessário
   request.magic = 0;
   request.comment = "Scalping";

   // Try different filling modes
   ENUM_ORDER_TYPE_FILLING fillingModes[] = {ORDER_FILLING_FOK, ORDER_FILLING_IOC, ORDER_FILLING_RETURN};
   bool orderSent = false;

   for (int i = 0; i < ArraySize(fillingModes); i++)
     {
      request.type_filling = fillingModes[i];

      if (OrderSend(request, result))
        {
         Print("Ordem aberta com sucesso: ", result.order);
         orderSent = true;
         break;
        }
      else
        {
         Print("Erro ao abrir ordem com filling mode ", EnumToString(fillingModes[i]), ": ", GetLastError());
        }
     }

   if (!orderSent)
     {
      Print("Falha ao abrir ordem após tentar todos os filling modes.");
     }
  }
//+------------------------------------------------------------------+



'SymbolInfoDouble' - no one of the overloads can be applied to the function call	Wende_Testando_01.mq5	138	9
could be one of 2 function(s)	Wende_Testando_01.mq5	138	9
   built-in: double SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE)	Wende_Testando_01.mq5	138	9
   built-in: bool SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE,double&)	Wende_Testando_01.mq5	138	9
possible use of uninitialized variable 'stopLevel'	Wende_Testando_01.mq5	143	29
implicit enum conversion	Wende_Testando_01.mq5	160	19