Help with this scalper

 

Hello, please can somebody help me with this code?

#include <Trade\Trade.mqh>
CTrade trade;
// Parametri di input
input int    FastEMAPeriod    = 2;          // Periodo EMA veloce
input int    SlowEMAPeriod    = 4;          // Periodo EMA lenta
input int    ATRShortPeriod   = 14;         // Periodo ATR breve
input int    ATRLongPeriod    = 50;         // Periodo ATR lungo
input double SLMultiplier     = 3.5;        // Moltiplicatore ATR per SL
input double TPMultiplier     = 4.0;        // Moltiplicatore ATR per TP
input int    MaxPyramidLevels = 4;          // Numero massimo di livelli pyramiding
input double InitialLotSize   = 0.1;        // Dimensione lotti iniziale
input double PyramidLotSize   = 0.05;       // Dimensione lotti per pyramiding
input double MaxLossLimit     = -150.0;     // Perdita massima cumulativa
input int    MagicNumber      = 20250631;   // Numero magico unico
input double ProfitThreshold  = 0.05;       // Soglia profitto per pyramiding
input double MomentumThreshold = 0.03;      // Soglia momentum
input double MaxDrawdownPerTrade = -100.0;  // Perdita massima per trade
input long   MaxTradeDuration = 30;         // Durata massima trade (secondi)
input double GroupProfitTarget = 0.1;       // Target profitto di gruppo
// Handle degli indicatori
int emaFastHandle, emaSlowHandle, atrShortHandle, atrLongHandle;
//+------------------------------------------------------------------+
int OnInit()
{
   // Inizializzazione handle indicatori
   emaFastHandle = iMA(_Symbol, PERIOD_M1, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
   emaSlowHandle = iMA(_Symbol, PERIOD_M1, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
   atrShortHandle = iATR(_Symbol, PERIOD_M1, ATRShortPeriod);
   atrLongHandle = iATR(_Symbol, PERIOD_M1, ATRLongPeriod);
   if(emaFastHandle == INVALID_HANDLE || emaSlowHandle == INVALID_HANDLE ||
      atrShortHandle == INVALID_HANDLE || atrLongHandle == INVALID_HANDLE)
   {
      Print("[Init] Errore inizializzazione handle per ", _Symbol);
      return INIT_FAILED;
   }
   if(!SymbolSelect(_Symbol, true))
   {
      Print("[Init] Impossibile selezionare il simbolo ", _Symbol);
   }
   // Verifica versione MT5
   int build = (int)TerminalInfoInteger(TERMINAL_BUILD);
   Print("[Init] Versione MT5 Build: ", IntegerToString(build));
   Print("[Init] EA Micro-Scalping v2.08 avviato per ", _Symbol, " M1");
   return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   Print("[Deinit] EA terminato per ", _Symbol);
   IndicatorRelease(emaFastHandle);
   IndicatorRelease(emaSlowHandle);
   IndicatorRelease(atrShortHandle);
   IndicatorRelease(atrLongHandle);
}
//+------------------------------------------------------------------+
void OnTick()
{
   // Recupero dati indicatori
   double emaFast[1], emaSlow[1], atrShort[1], atrLong[1];
   if(CopyBuffer(emaFastHandle, 0, 0, 1, emaFast) < 0 ||
      CopyBuffer(emaSlowHandle, 0, 0, 1, emaSlow) < 0 ||
      CopyBuffer(atrShortHandle, 0, 0, 1, atrShort) < 0 ||
      CopyBuffer(atrLongHandle, 0, 0, 1, atrLong) < 0)
   {
      Print("[Tick] Errore lettura indicatori per ", _Symbol);
      return;
   }
   double fastEMA = emaFast[0];
   double slowEMA = emaSlow[0];
   double atrShortValue = atrShort[0];
   double atrLongValue = atrLong[0];
   // Calcolo segnali
   bool isBuySignal = fastEMA > slowEMA;
   bool isSellSignal = fastEMA < slowEMA;
   double momentum = MathAbs(fastEMA - slowEMA) / atrShortValue;
   // Raccolta posizioni
   int totalPositions = PositionsTotal();
   ulong positionTickets[20];
   int positionCount = 0;
   double totalProfit = 0.0;
   for(int i = 0; i < totalPositions && positionCount < 20; i++)
   {
      ulong ticket = PositionGetTicket(i);
      if(PositionSelectByTicket(ticket) && PositionGetString(POSITION_SYMBOL) == _Symbol &&
         PositionGetInteger(POSITION_MAGIC) == MagicNumber)
      {
         positionTickets[positionCount] = ticket;
         totalProfit += PositionGetDouble(POSITION_PROFIT);
         positionCount++;
      }
   }
   // Dati di mercato
   double askPrice, bidPrice, spread, minStopLevel;
   if(!GetMarketPrices(_Symbol, askPrice, bidPrice, spread, minStopLevel))
   {
      Print("[Tick] Errore recupero prezzi per ", _Symbol);
      return;
   }
   // Dati storici
   double prevHigh = iHigh(_Symbol, PERIOD_M1, 1);
   double prevLow = iLow(_Symbol, PERIOD_M1, 1);
   double prevClose = iClose(_Symbol, PERIOD_M1, 1);
   double prev2Close = iClose(_Symbol, PERIOD_M1, 2);
   // 1) Entrata iniziale
   if(positionCount == 0 && (isBuySignal || isSellSignal))
   {
      bool entryCondition = (isBuySignal && askPrice > prevHigh + atrShortValue * 0.5 && prevClose > prev2Close) ||
                           (isSellSignal && bidPrice < prevLow - atrShortValue * 0.5 && prevClose < prev2Close);
      if(entryCondition && momentum >= MomentumThreshold)
      {
         MqlTradeRequest request;
         MqlTradeResult result;
         request.action = TRADE_ACTION_DEAL;
         request.symbol = _Symbol;
         request.magic = MagicNumber;
         request.volume = InitialLotSize;
         request.type = isBuySignal ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
         request.price = isBuySignal ? askPrice : bidPrice;
         request.deviation = 3;
         if(trade.OrderSend(request, result))
         {
            PrintFormat("[OpenInit] %s, Ticket=%I64d, Simbolo=%s", isBuySignal ? "BUY" : "SELL", result.order, _Symbol);
            ApplyDynamicLevels(result.order, isBuySignal, askPrice, bidPrice, atrShortValue, atrLongValue, spread, minStopLevel);
         }
         return;
      }
   }
   // 2) Pyramiding
   if(positionCount > 0 && PositionSelectByTicket(positionTickets[0]))
   {
      double firstProfit = PositionGetDouble(POSITION_PROFIT);
      if(firstProfit >= ProfitThreshold)
      {
         double momentumCheck = MathAbs(fastEMA - slowEMA) / atrShortValue;
         for(int k = positionCount; k <= MaxPyramidLevels; k++)
         {
            bool pyramidCondition = (isBuySignal && askPrice > prevHigh + atrShortValue * 0.5 && prevClose > prev2Close) ||
                                   (isSellSignal && bidPrice < prevLow - atrShortValue * 0.5 && prevClose < prev2Close);
            if(!pyramidCondition || momentumCheck < MomentumThreshold) break;
            MqlTradeRequest request;
            MqlTradeResult result;
            request.action = TRADE_ACTION_DEAL;
            request.symbol = _Symbol;
            request.magic = MagicNumber;
            request.volume = PyramidLotSize;
            request.type = isBuySignal ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
            request.price = isBuySignal ? askPrice : bidPrice;
            request.deviation = 3;
            if(trade.OrderSend(request, result))
            {
               PrintFormat("[Pyramid #%d] Ticket=%I64d, Simbolo=%s", k, result.order, _Symbol);
               ApplyDynamicLevels(result.order, isBuySignal, askPrice, bidPrice, atrShortValue, atrLongValue, spread, minStopLevel);
            }
         }
      }
      // Chiusura di gruppo
      if(totalProfit >= GroupProfitTarget || totalProfit <= MaxLossLimit)
      {
         for(int j = 0; j < positionCount; j++)
            if(PositionSelectByTicket(positionTickets[j]))
               trade.PositionClose(positionTickets[j]);
         PrintFormat("[GroupClose] Chiusura %d posizioni, Profitto=%.2f, Simbolo=%s", positionCount, totalProfit, _Symbol);
         return;
      }
   }
   // 3) Trailing stop e uscite
   for(int j = 0; j < positionCount; j++)
   {
      if(PositionSelectByTicket(positionTickets[j]))
      {
         double profit = PositionGetDouble(POSITION_PROFIT);
         bool isBuy = PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY;
         double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         double currentPrice;
         if(!GetCurrentPrice(_Symbol, isBuy, currentPrice))
         {
            Print("[Tick] Errore recupero prezzo corrente per ", _Symbol);
            continue;
         }
         long openTime = PositionGetInteger(POSITION_TIME);
         double currentSL = PositionGetDouble(POSITION_SL);
         double currentTP = PositionGetDouble(POSITION_TP);
         // Calcolo high/low
         int barsSinceOpen = (int)((TimeCurrent() - openTime) / PeriodSeconds(PERIOD_M1));
         double highSinceOpen = openPrice, lowSinceOpen = openPrice;
         for(int i = 0; i <= barsSinceOpen && i < 10; i++)
         {
            double high = iHigh(_Symbol, PERIOD_M1, i);
            double low = iLow(_Symbol, PERIOD_M1, i);
            if(high > highSinceOpen) highSinceOpen = high;
            if(low < lowSinceOpen) lowSinceOpen = low;
         }
         // Uscite
         if(TimeCurrent() - openTime > MaxTradeDuration && profit < 0)
         {
            trade.PositionClose(positionTickets[j]);
            PrintFormat("[TimeExit] Ticket=%I64d chiuso, Profitto=%.2f, Durata=%d, Simbolo=%s", 
                        positionTickets[j], profit, TimeCurrent() - openTime, _Symbol);
            continue;
         }
         if(profit <= MaxDrawdownPerTrade)
         {
            trade.PositionClose(positionTickets[j]);
            PrintFormat("[MaxDrawdown] Ticket=%I64d chiuso, Profitto=%.2f, Simbolo=%s", 
                        positionTickets[j], profit, _Symbol);
            continue;
         }
         // Trailing stop
         double newSL = isBuy ? MathMin(openPrice + (highSinceOpen - openPrice) * 0.1, openPrice + atrShortValue * SLMultiplier) :
                               MathMax(openPrice - (openPrice - lowSinceOpen) * 0.1, openPrice - atrShortValue * SLMultiplier);
         double newTP = isBuy ? MathMax(openPrice - (openPrice - currentPrice) * 0.1, openPrice - atrShortValue * TPMultiplier) :
                               MathMin(openPrice + (currentPrice - openPrice) * 0.1, openPrice + atrShortValue * TPMultiplier);
         double minStopDistance = MathMax(minStopLevel + spread, atrShortValue * 0.5);
         if(isBuy && newSL < openPrice + minStopDistance) newSL = openPrice + minStopDistance;
         if(!isBuy && newSL > openPrice - minStopDistance) newSL = openPrice - minStopDistance;
         if(isBuy && newTP > openPrice - minStopDistance) newTP = openPrice - minStopDistance;
         if(!isBuy && newTP < openPrice + minStopDistance) newTP = openPrice + minStopDistance;
         if((isBuy && newSL < currentSL) || (!isBuy && newSL > currentSL) ||
            (isBuy && newTP > currentTP) || (!isBuy && newTP < currentTP))
         {
            if(trade.PositionModify(positionTickets[j], newSL, newTP))
               PrintFormat("[Trail] Ticket=%I64d, SL=%.2f, TP=%.2f, Simbolo=%s", 
                           positionTickets[j], newSL, newTP, _Symbol);
         }
      }
   }
}
//+------------------------------------------------------------------+
bool GetMarketPrices(string symbol, double ask, double bid, double spread, double minStopLevel)
{
   double tempAsk = 0.0;
   if(!SymbolInfoDouble(symbol, SYMBOL_ASK, tempAsk)) return false;
   ask = tempAsk;
   double tempBid = 0.0;
   if(!SymbolInfoDouble(symbol, SYMBOL_BID, tempBid)) return false;
   bid = tempBid;
   double tempSpread = 0.0;
   if(!SymbolInfoDouble(symbol, SYMBOL_SPREAD, tempSpread)) return false;
   spread = tempSpread * _Point;
   double tempMinStop = 0.0;
   if(!SymbolInfoDouble(symbol, SYMBOL_TRADE_STOPS_LEVEL, tempMinStop)) return false;
   minStopLevel = tempMinStop * _Point;
   return true;
}
//+------------------------------------------------------------------+
bool GetCurrentPrice(string symbol, bool isBuy, double price)
{
   double tempPrice = 0.0;
   bool result = isBuy ? SymbolInfoDouble(symbol, SYMBOL_BID, tempPrice) : SymbolInfoDouble(symbol, SYMBOL_ASK, tempPrice);
   if(result) price = tempPrice;
   return result;
}
//+------------------------------------------------------------------+
void ApplyDynamicLevels(ulong ticket, bool isBuy, double ask, double bid, double atrShort, double atrLong, double spread, double minStopLevel)
{
   if(!PositionSelectByTicket(ticket)) return;
   double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
   double volatilityFactor = 1.0 + (atrShort / atrLong);
   double slDistance = MathMax(atrShort * SLMultiplier * volatilityFactor, minStopLevel + spread);
   double tpDistance = MathMax(atrShort * TPMultiplier * volatilityFactor, minStopLevel + spread * 2);
   double newSL = isBuy ? openPrice + slDistance : openPrice - slDistance;
   double newTP = isBuy ? openPrice - tpDistance : openPrice + tpDistance;
   if(trade.PositionModify(ticket, newSL, newTP))
      PrintFormat("[DynamicSLTP] Ticket=%I64d, SL=%.2f, TP=%.2f, Simbolo=%s", ticket, newSL, newTP, _Symbol);
}
'SymbolInfoDouble' - no one of the overloads can be applied to the function call ScalpBeast.mq5 262 8
could be one of 2 function(s) ScalpBeast.mq5 262 8
   built-in: double SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE) ScalpBeast.mq5 262 8
   built-in: bool SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE,double&) ScalpBeast.mq5 262 8
'SymbolInfoDouble' - no one of the overloads can be applied to the function call ScalpBeast.mq5 266 8
could be one of 2 function(s) ScalpBeast.mq5 266 8
   built-in: double SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE) ScalpBeast.mq5 266 8
   built-in: bool SymbolInfoDouble(const string,ENUM_SYMBOL_INFO_DOUBLE,double&) ScalpBeast.mq5 266 8


Thanks

 
Jaouahr YounesHello, please can somebody help me with this code?

Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

NOTE: some details in your code are common for AI-generated, so it can be hardly helped (would need considerable human review/corrections before being usable in a live trading environment).

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.07.07
  • www.mql5.com
The largest freelance service with MQL5 application developers
 

Why did you post your coding question in the MT5 General section (a miscellaneous catch-all category) instead of the MT5 EA section (non-indicator coding)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. I have moved this thread.