//+------------------------------------------------------------------+
//|                                                     MA_Cross.mq5 |
//|                        Expert Advisor based on MA Cross Strategy |
//+------------------------------------------------------------------+
#property strict

#include <Trade/Trade.mqh>

input double lotSize = 0.1;              // Lot size
input double takeProfitPips = 20;        // Take profit in pips
input double stopLossPips = 400;         // Stop loss in pips
input double trailingStopPips = 20;      // Trailing stop in pips

double ma20, ma200, ma10;
double trailingStopLevel;
CTrade trade;
double prevClose1, prevClose2;  // Declare earlier for wide scope usage

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

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Calculate moving averages
    ma20 = iMA(NULL, 0, 20, 0, MODE_EMA, PRICE_CLOSE);
    ma200 = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE);
    ma10 = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE);

    double closePrice = iClose(NULL, 0, 0);
    prevClose1 = iClose(NULL, 0, 1);
    prevClose2 = iClose(NULL, 0, 2);

    // Check if there is an existing position for the current symbol
    if (PositionSelect(_Symbol))
    {
        ENUM_POSITION_TYPE positionType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
        if (positionType == POSITION_TYPE_BUY && CanOpenSell()) // Rule 2
        {
            trade.PositionClose(_Symbol);
        }
        else if (positionType == POSITION_TYPE_SELL && CanOpenBuy()) // Rule 1
        {
            trade.PositionClose(_Symbol);
        }
    }

    // Open buy order according to rule 1
    if (CanOpenBuy() && IsEMA10SlopeValid(true) && !IsPriceConsolidating()) 
    {
        if (trade.Buy(lotSize, NULL, 0, 0, 0, "Buy order by crossover"))
        {
            Print("Buy order opened successfully.");
        }
        else
        {
            Print("Error opening buy order: ", trade.ResultRetcode(), " - ", GetLastError());
        }
    }

    // Open sell order according to rule 2
    if (CanOpenSell() && IsEMA10SlopeValid(false) && !IsPriceConsolidating()) 
    {
        if (trade.Sell(lotSize, NULL, 0, 0, 0, "Sell order by crossover"))
        {
            Print("Sell order opened successfully.");
        }
        else
        {
            Print("Error opening sell order: ", trade.ResultRetcode(), " - ", GetLastError());
        }
    }

    // Manage Trailing Stop for existing positions
    ManageTrailingStop();
}

//+------------------------------------------------------------------+
//| Check if a buy order can be opened                               |
//+------------------------------------------------------------------+
bool CanOpenBuy()
{
    return (ma20 > ma200 && prevClose1 > ma200 && prevClose2 > ma200);
}

//+------------------------------------------------------------------+
//| Check if a sell order can be opened                              |
//+------------------------------------------------------------------+
bool CanOpenSell()
{
    return (ma20 < ma200 && prevClose1 < ma200 && prevClose2 < ma200);
}

//+------------------------------------------------------------------+
//| Check if the EMA10 has the desired slope                         |
//+------------------------------------------------------------------+
bool IsEMA10SlopeValid(bool isForBuy)
{
    double ema10Previous = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE, 1);  // Correct previous MA
    double ema10Slope = ma10 - ema10Previous;
    double requiredSlope = isForBuy ? 0.001 : -0.001;
    return (isForBuy ? ema10Slope > requiredSlope : ema10Slope < requiredSlope);
}

//+------------------------------------------------------------------+
//| Check if the price is consolidating                              |
//+------------------------------------------------------------------+
bool IsPriceConsolidating()
{
    int atrHandle = iATR(NULL, 0, 14);
    double atrBuffer[1];

    if (CopyBuffer(atrHandle, 0, 0, 1, atrBuffer) != 1)
        return false;

    double atrValue = atrBuffer[0];
    double priceRange = iHigh(NULL, 0, 0) - iLow(NULL, 0, 0);
    return (priceRange < 1.5 * atrValue);
}

//+------------------------------------------------------------------+
//| Manage Trailing Stop                                             |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionSelectByIndex(i))
        {
            ulong position_ticket = PositionGetInteger(POSITION_TICKET);
            double currentStopLoss = PositionGetDouble(POSITION_SL);
            double tp = PositionGetDouble(POSITION_TP);
            
            trailingStopLevel = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) ?
                                NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID) - trailingStopPips * _Point, _Digits)
                                : NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK) + trailingStopPips * _Point, _Digits);

            if ((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && trailingStopLevel > currentStopLoss) ||
                (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && trailingStopLevel < currentStopLoss))
            {
                trade.PositionModify(position_ticket, trailingStopLevel, tp);
            }
        }
    }
}

erro:

'HTF_Wendel_Cassiano2_V2_ Arrumando_0005_Lot_001.mq5'			1
Trade.mqh			
Object.mqh			
StdLibErr.mqh			
OrderInfo.mqh			
HistoryOrderInfo.mqh			
PositionInfo.mqh			
DealInfo.mqh			
'iMA' - wrong parameters count	HTF_Wendel_Cassiano2_V2_ Arrumando_0005_Lot_001.mq5	106	28
   built-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int)	HTF_Wendel_Cassiano2_V2_ Arrumando_0005_Lot_001.mq5	106	28
'PositionSelectByIndex' - undeclared identifier	HTF_Wendel_Cassiano2_V2_ Arrumando_0005_Lot_001.mq5	135	13
'i' - some operator expected	HTF_Wendel_Cassiano2_V2_ Arrumando_0005_Lot_001.mq5	135	35
3 errors, 0 warnings		4	1