#include <Trade\Trade.mqh>

input double lotSize = 0.1;

// Variáveis globais para controle de ordens
bool orderOpened = false;
ulong orderTicket = 0;

// Variáveis para armazenar rompimento anterior
bool lastBreakoutAlta = false;
bool lastBreakoutBaixa = false;
double breakoutHigh = 0.0;
double breakoutLow = 0.0;

// Configurações de trailing stop
input int TslTriggerPoints = 15;  // Pontos de lucro antes do trailing stop ser ativado
input int TslPoints = 10;         // Trailing stop em pips

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

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

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    double bollingerUpper[], bollingerLower[], bollingerMiddle[];
    int bbPeriod = 20;
    double bbDeviation = 2.0;
    double slippage = 3 * _Point;

    int bbHandle = iBands(_Symbol, PERIOD_M2, bbPeriod, bbDeviation, 0, PRICE_CLOSE);

    if (bbHandle == INVALID_HANDLE) {
        Print("Erro ao obter handle das Bandas de Bollinger: ", GetLastError());
        return;
    }

    ArraySetAsSeries(bollingerUpper, true);
    ArraySetAsSeries(bollingerMiddle, true);
    ArraySetAsSeries(bollingerLower, true);

    if (CopyBuffer(bbHandle, 0, 0, 1, bollingerUpper) <= 0 ||
        CopyBuffer(bbHandle, 1, 0, 1, bollingerMiddle) <= 0 ||
        CopyBuffer(bbHandle, 2, 0, 1, bollingerLower) <= 0) {
        Print("Erro ao copiar buffer das Bandas de Bollinger: ", GetLastError());
        return;
    }

    double priceHigh = iHigh(_Symbol, PERIOD_M2, 0);
    double priceLow = iLow(_Symbol, PERIOD_M2, 0);
    double priceClose = iClose(_Symbol, PERIOD_M2, 0);
    double range = priceHigh - priceLow;

    bool tendenciaAltaH1 = VerificarTendenciaMacro(PERIOD_H1, 20);
    bool tendenciaBaixaH1 = !tendenciaAltaH1;

    CTrade trade;

    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionGetTicket(i) > 0)
        {
            ulong positionTicket = PositionGetTicket(i);
            double positionProfit = PositionGetDouble(POSITION_PROFIT);
            double positionPrice = PositionGetDouble(POSITION_PRICE_CURRENT);

            if (positionProfit > 0 && positionPrice >= bollingerMiddle[0] - slippage && positionPrice <= bollingerMiddle[0] + slippage)
            {
                if (trade.PositionClose(positionTicket))
                {
                    Print("Posição lucrativa fechada. Ticket: ", positionTicket);
                }
                else
                {
                    Print("Erro ao fechar a posição: ", GetLastError());
                }
            }
        }
    }

    if (lastBreakoutAlta && priceClose > breakoutHigh && tendenciaAltaH1)
    {
        double slBuy = priceClose - 10 * _Point;
        if (trade.Buy(lotSize, _Symbol, slBuy))
        {
            orderOpened = true;
            orderTicket = trade.ResultOrder();
            Print("Ordem de compra aberta! Ticket: ", orderTicket);
            lastBreakoutAlta = false;
        }
        else
        {
            Print("Erro ao abrir ordem de compra: ", trade.ResultRetcode(), " - ", GetLastError());
        }
    }

    if (lastBreakoutBaixa && priceClose < breakoutLow && tendenciaBaixaH1)
    {
        double slSell = priceClose + 10 * _Point;
        if (trade.Sell(lotSize, _Symbol, slSell))
        {
            orderOpened = true;
            orderTicket = trade.ResultOrder();
            Print("Ordem de venda aberta! Ticket: ", orderTicket);
            lastBreakoutBaixa = false;
        }
        else
        {
            Print("Erro ao abrir ordem de venda: ", trade.ResultRetcode(), " - ", GetLastError());
        }
    }

    if (range >= 30 * _Point && priceClose > bollingerUpper[0] && tendenciaAltaH1) {
        lastBreakoutAlta = true;
        breakoutHigh = priceHigh;
        Print("Rompimento de alta detectado.");
    }

    if (range >= 30 * _Point && priceClose < bollingerLower[0] && tendenciaBaixaH1) {
        lastBreakoutBaixa = true;
        breakoutLow = priceLow;
        Print("Rompimento de baixa detectado.");
    }

    TrailStop();
}

//+------------------------------------------------------------------+
//| Função para verificar a tendência macro em H1                     |
//+------------------------------------------------------------------+
bool VerificarTendenciaMacro(int timeframe, int maPeriod)
{
    // Adicionando o método de média móvel
    double maPrice = iMA(_Symbol, (ENUM_TIMEFRAMES)timeframe, maPeriod, 0, MODE_SMA, PRICE_CLOSE, 0);
    double closePrice = iClose(_Symbol, (ENUM_TIMEFRAMES)timeframe, 0);

    return closePrice > maPrice;
}

//+------------------------------------------------------------------+
//| Gerenciamento de trailing stop                                   |
//+------------------------------------------------------------------+
void TrailStop()
{
    CTrade trade;
    double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionGetTicket(i) > 0 && PositionSelectByTicket(PositionGetTicket(i)))
        {
            ulong ticket = PositionGetTicket(i);
            double currentSL = PositionGetDouble(POSITION_SL);
            double tp = PositionGetDouble(POSITION_TP);

            if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            {
                if (bid - PositionGetDouble(POSITION_PRICE_OPEN) > TslTriggerPoints * _Point)
                {
                    double newSL = bid - TslPoints * _Point;

                    if (newSL > currentSL || currentSL == 0)
                    {
                        trade.PositionModify(ticket, newSL, tp);
                        Print("Trailing Stop atualizado para compra.");
                    }
                }
            }
            else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
            {
                if (PositionGetDouble(POSITION_PRICE_OPEN) - ask > TslTriggerPoints * _Point)
                {
                    double newSL = ask + TslPoints * _Point;

                    if (newSL < currentSL || currentSL == 0)
                    {
                        trade.PositionModify(ticket, newSL, tp);
                        Print("Trailing Stop atualizado para venda.");
                    }
                }
            }
        }
    }
}

erro:

'NASDAQ_011.mq5'			1
Trade.mqh			
Object.mqh			
StdLibErr.mqh			
OrderInfo.mqh			
HistoryOrderInfo.mqh			
PositionInfo.mqh			
DealInfo.mqh			
possible loss of data due to type conversion	NASDAQ_011.mq5	44	57
'iMA' - wrong parameters count	NASDAQ_011.mq5	146	22
   built-in: int iMA(const string,ENUM_TIMEFRAMES,int,int,ENUM_MA_METHOD,int)	NASDAQ_011.mq5	146	22
1 errors, 1 warnings		2	2