Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

'ORDERSELECTBYPOS' - undeclared identifier WendeTestando01.mq5 182 28 'OrderSelect' -…

  1. User

    'ORDER_SELECT_BY_POS' - undeclared identifier Wende_Testando_01.mq5 182 28 'OrderSelect' - wrong parameters count Wende_Testando_01.mq5 182 13 built-in: bool OrderSelect(ulong) Wende_Testando_01.mq5 182 13 possible loss of data due to type conversion from 'long' to 'int' Wende_Testando_01.mq5 184 34 'TRADE_ACTION_CLOSE' - undeclared identifier Wende_Testando_01.mq5 196 38 'ORDER_VOLUME' - undeclared identifier Wende_Testando_01.mq5 198 53 'OrderGetDouble' - no one of the overloads can be applied to the function call Wende_Testando_01.mq5 198 38 could be one of 2 function(s) Wende_Testando_01.mq5 198 38 built-in: double OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE) Wende_Testando_01.mq5 198 38 built-in: bool OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE,double&) Wende_Testando_01.mq5 198 38

    erros acima meu codigo a ser corrigido abaixo:

    //±-----------------------------------------------------------------+ //| Expert.mq5 | //| Copyright 2024, MetaQuotes Software Corp.| //| http://www.metaquotes.net/| //±-----------------------------------------------------------------+ #property copyright "Copyright 2024" #property link "http://www.metaquotes.net/" #property version "1.01" #property strict

    // Parâmetros do indicador input int smaLength = 20; // Comprimento da SMA input int emaLength = 9; // Comprimento da EMA input int rsiLength = 14; // Comprimento do RSI input double atrMultiplier = 1.0; // Multiplicador ATR para SL input double tp1Multiplier = 1.5; // Multiplicador TP1 input double tp2Multiplier = 3.0; // Multiplicador TP2 input double tp3Multiplier = 4.5; // Multiplicador TP3 input int sma200Length = 200; // Comprimento da SMA200

    // Parâmetros do Trend Cloud input int emaShortLength = 9; // Comprimento da EMA curta input int emaLongLength = 26; // Comprimento da EMA longa

    // Handles dos indicadores int smaHandle; int emaHandle; int rsiHandle; int atrHandle; int sma200Handle; int emaShortHandle; int emaLongHandle;

    // Inicialização int OnInit() { // Cria handles para os indicadores smaHandle = iMA(NULL, 0, smaLength, 0, MODE_SMA, PRICE_CLOSE); emaHandle = iMA(NULL, 0, emaLength, 0, MODE_EMA, PRICE_CLOSE); rsiHandle = iRSI(NULL, 0, rsiLength, PRICE_CLOSE); atrHandle = iATR(NULL, 0, 14); sma200Handle = iMA(NULL, 0, sma200Length, 0, MODE_SMA, PRICE_CLOSE); emaShortHandle = iMA(NULL, 0, emaShortLength, 0, MODE_EMA, PRICE_CLOSE); emaLongHandle = iMA(NULL, 0, emaLongLength, 0, MODE_EMA, PRICE_CLOSE);

    if (smaHandle == INVALID_HANDLE || emaHandle == INVALID_HANDLE || rsiHandle == INVALID_HANDLE || atrHandle == INVALID_HANDLE || sma200Handle == INVALID_HANDLE || emaShortHandle == INVALID_HANDLE || emaLongHandle == INVALID_HANDLE)
    {
        Print("Erro ao criar handles para os indicadores.");
        return INIT_FAILED;
    }
    
    Print("Robô iniciado");
    return(INIT_SUCCEEDED);
    

    }

    // Função para o gerenciamento de ordens void OnTick() { double smaBuffer[], emaBuffer[], rsiBuffer[], atrBuffer[], sma200Buffer[]; double emaShortBuffer[], emaLongBuffer[];

    // Cálculo dos indicadores
    if (CopyBuffer(smaHandle, 0, 0, 2, smaBuffer) <= 0 ||
        CopyBuffer(emaHandle, 0, 0, 2, emaBuffer) <= 0 ||
        CopyBuffer(rsiHandle, 0, 0, 1, rsiBuffer) <= 0 ||
        CopyBuffer(atrHandle, 0, 0, 1, atrBuffer) <= 0 ||
        CopyBuffer(sma200Handle, 0, 0, 1, sma200Buffer) <= 0 ||
        CopyBuffer(emaShortHandle, 0, 0, 2, emaShortBuffer) <= 0 ||
        CopyBuffer(emaLongHandle, 0, 0, 2, emaLongBuffer) <= 0)
    {
        Print("Erro ao copiar os buffers dos indicadores.");
        return;
    }
    
    double smaCurrent = smaBuffer[0];
    double smaPrevious = smaBuffer[1];
    double emaCurrent = emaBuffer[0];
    double emaPrevious = emaBuffer[1];
    double rsiCurrent = rsiBuffer[0];
    double atrCurrent = atrBuffer[0];
    double sma200Current = sma200Buffer[0];
    double emaShortCurrent = emaShortBuffer[0];
    double emaShortPrevious = emaShortBuffer[1];
    double emaLongCurrent = emaLongBuffer[0];
    double emaLongPrevious = emaLongBuffer[1];
    
    double priceCurrent = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    
    // Verifica condições de compra e venda
    if (emaPrevious < smaPrevious && emaCurrent > smaCurrent && rsiCurrent < 30 && priceCurrent > sma200Current &&
        emaShortPrevious <= emaLongPrevious && emaShortCurrent > emaLongCurrent)
    {
        // Sinal de compra
        if (PositionsTotal() == 0 || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
        {
            // Fechar posições de venda
            CloseOrders(POSITION_TYPE_SELL);
            // Abrir ordem de compra
            OpenBuyOrder(atrCurrent); // Passando o valor do ATR
        }
    }
    else if (emaPrevious > smaPrevious && emaCurrent < smaCurrent && rsiCurrent > 70 && priceCurrent < sma200Current &&
             emaShortPrevious >= emaLongPrevious && emaShortCurrent < emaLongCurrent)
    {
        // Sinal de venda
        if (PositionsTotal() == 0 || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
        {
            // Fechar posições de compra
            CloseOrders(POSITION_TYPE_BUY);
            // Abrir ordem de venda
            OpenSellOrder(atrCurrent); // Passando o valor do ATR
        }
    }
    

    }

    // Função para abrir ordem de compra void OpenBuyOrder(double atrCurrent) { double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) - atrCurrent * atrMultiplier, _Digits); double tp1 = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) + atrCurrent * tp1Multiplier, _Digits);

    // Enviar ordem de compra
    MqlTradeRequest request;
    MqlTradeResult result;
    
    ZeroMemory(request);
    ZeroMemory(result);
    
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = 0.1;
    request.type = ORDER_TYPE_BUY;
    request.price = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
    request.sl = sl;
    request.tp = tp1; // Modificar conforme a necessidade
    request.deviation = 10;
    request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
    request.type_time = ORDER_TIME_GTC;
    
    if (!OrderSend(request, result))
    {
        Print("Erro ao abrir ordem de compra: ", GetLastError());
    }
    

    }

    // Função para abrir ordem de venda void OpenSellOrder(double atrCurrent) { double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) + atrCurrent * atrMultiplier, _Digits); double tp1 = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) - atrCurrent * tp1Multiplier, _Digits);

    // Enviar ordem de venda
    MqlTradeRequest request;
    MqlTradeResult result;
    
    ZeroMemory(request);
    ZeroMemory(result);
    
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;
    request.volume = 0.1;
    request.type = ORDER_TYPE_SELL;
    request.price = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
    request.sl = sl;
    request.tp = tp1; // Modificar conforme a necessidade
    request.deviation = 10;
    request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
    request.type_time = ORDER_TIME_GTC;
    
    if (!OrderSend(request, result))
    {
        Print("Erro ao abrir ordem de venda: ", GetLastError());
    }
    

    }

    // Função para fechar ordens abertas void CloseOrders(int orderType) { for (int i = OrdersTotal() - 1; i >= 0; i--) { ulong ticket = 0; if (OrderSelect(i, ORDER_SELECT_BY_POS)) { int orderTypeCurrent = OrderGetInteger(ORDER_TYPE); if (orderTypeCurrent == orderType) { ticket = OrderGetTicket(i); if (ticket > 0) { MqlTradeRequest request; MqlTradeResult result;

                    ZeroMemory(request);
                    ZeroMemory(result);
    
                    request.action = TRADE_ACTION_CLOSE;
                    request.symbol = _Symbol;
                    request.volume = OrderGetDouble(ORDER_VOLUME); // Volume da ordem
                    request.position = ticket; // Identificador da posição
                    request.price = (orderType == ORDER_TYPE_SELL) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                    request.deviation = 10;
                    request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
                    request.type_time = ORDER_TIME_GTC;
    
                    if (!OrderSend(request, result))
                    {
                        Print("Erro ao fechar ordem: ", GetLastError());
                    }
                    else
                    {
                        Print("Ordem fechada com sucesso.");
                    }
                }
            }
        }
    }
    

    }

    // Função para calcular os Order Blocks (não implementado no exemplo) void CalculateOrderBlocks() { // Implementar lógica de Order Blocks }

  2. Assistant

    Para corrigir os erros apresentados no código MQL5, vamos abordar cada um deles:

    1. 'ORDER_SELECT_BY_POS' - undeclared identifier

      Isso ocorre porque ORDER_SELECT_BY_POS é para uso com funções OrderSelect, que são para ordens pendentes. No entanto, você precisa usar PositionSelect e PositionGetTicket para obtenção de posições abertas.

    2. 'OrderSelect' - wrong parameters count

      O problema aqui é que você está usando OrderSelect incorretamente ao invés de usar PositionSelect. PositionSelect só precisa de um parâmetro, o símbolo da ordem (_Symbol).

    3. possible loss of data due to type conversion from 'long' to 'int'

      Isso ocorre porque você está usando índices no loop da função CloseOrders. OrdersTotal() retorna um long e não um int. Vamos ajustar a variável do índice para ser long.

    4. 'TRADE_ACTION_CLOSE' - undeclared identifier

      Isso ocorre porque a ação de fechar posição deve ser especificada pela constante TRADE_ACTION_DEAL e definir a posição a ser fechada.

    5. 'ORDER_VOLUME' - undeclared identifier

      Você deve usar PositionGetDouble(POSITION_VOLUME) para obter o volume da posição ao invés de OrderGetDouble.

    6. 'OrderGetDouble' - no one of the overloads can be applied to the function call

      Como mencionado, você deve usar PositionGetDouble para obter informações de volumes de posições abertas.

    Aqui está o código corrigido:

    //+------------------------------------------------------------------+
    //|                                                      Expert.mq5  |
    //|                        Copyright 2024, MetaQuotes Software Corp. |
    //|                                       http://www.metaquotes.net/ |
    //+------------------------------------------------------------------+
    
    #property copyright "Copyright 2024"
    #property link      "http://www.metaquotes.net/"
    #property version   "1.01"
    #property strict
    
    // Parâmetros do indicador
    input int smaLength = 20;          // Comprimento da SMA
    input int emaLength = 9;           // Comprimento da EMA
    input int rsiLength = 14;          // Comprimento do RSI
    input double atrMultiplier = 1.0;  // Multiplicador ATR para SL
    input double tp1Multiplier = 1.5;  // Multiplicador TP1
    input double tp2Multiplier = 3.0;  // Multiplicador TP2
    input double tp3Multiplier = 4.5;  // Multiplicador TP3
    input int sma200Length = 200;      // Comprimento da SMA200
    
    // Parâmetros do Trend Cloud
    input int emaShortLength = 9;      // Comprimento da EMA curta
    input int emaLongLength = 26;      // Comprimento da EMA longa
    
    // Handles dos indicadores
    int smaHandle;
    int emaHandle;
    int rsiHandle;
    int atrHandle;
    int sma200Handle;
    int emaShortHandle;
    int emaLongHandle;
    
    // Inicialização
    int OnInit()
    {
        // Cria handles para os indicadores
        smaHandle = iMA(NULL, 0, smaLength, 0, MODE_SMA, PRICE_CLOSE);
        emaHandle = iMA(NULL, 0, emaLength, 0, MODE_EMA, PRICE_CLOSE);
        rsiHandle = iRSI(NULL, 0, rsiLength, PRICE_CLOSE);
        atrHandle = iATR(NULL, 0, 14);
        sma200Handle = iMA(NULL, 0, sma200Length, 0, MODE_SMA, PRICE_CLOSE);
        emaShortHandle = iMA(NULL, 0, emaShortLength, 0, MODE_EMA, PRICE_CLOSE);
        emaLongHandle = iMA(NULL, 0, emaLongLength, 0, MODE_EMA, PRICE_CLOSE);
    
        if (smaHandle == INVALID_HANDLE || emaHandle == INVALID_HANDLE || rsiHandle == INVALID_HANDLE || atrHandle == INVALID_HANDLE || sma200Handle == INVALID_HANDLE || emaShortHandle == INVALID_HANDLE || emaLongHandle == INVALID_HANDLE)
        {
            Print("Erro ao criar handles para os indicadores.");
            return INIT_FAILED;
        }
    
        Print("Robô iniciado");
        return(INIT_SUCCEEDED);
    }
    
    // Função para o gerenciamento de ordens
    void OnTick()
    {
        double smaBuffer[], emaBuffer[], rsiBuffer[], atrBuffer[], sma200Buffer[];
        double emaShortBuffer[], emaLongBuffer[];
    
        // Cálculo dos indicadores
        if (CopyBuffer(smaHandle, 0, 0, 2, smaBuffer) <= 0 ||
            CopyBuffer(emaHandle, 0, 0, 2, emaBuffer) <= 0 ||
            CopyBuffer(rsiHandle, 0, 0, 1, rsiBuffer) <= 0 ||
            CopyBuffer(atrHandle, 0, 0, 1, atrBuffer) <= 0 ||
            CopyBuffer(sma200Handle, 0, 0, 1, sma200Buffer) <= 0 ||
            CopyBuffer(emaShortHandle, 0, 0, 2, emaShortBuffer) <= 0 ||
            CopyBuffer(emaLongHandle, 0, 0, 2, emaLongBuffer) <= 0)
        {
            Print("Erro ao copiar os buffers dos indicadores.");
            return;
        }
    
        double smaCurrent = smaBuffer[0];
        double smaPrevious = smaBuffer[1];
        double emaCurrent = emaBuffer[0];
        double emaPrevious = emaBuffer[1];
        double rsiCurrent = rsiBuffer[0];
        double atrCurrent = atrBuffer[0];
        double sma200Current = sma200Buffer[0];
        double emaShortCurrent = emaShortBuffer[0];
        double emaShortPrevious = emaShortBuffer[1];
        double emaLongCurrent = emaLongBuffer[0];
        double emaLongPrevious = emaLongBuffer[1];
    
        double priceCurrent = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    
        // Verifica condições de compra e venda
        if (emaPrevious < smaPrevious && emaCurrent > smaCurrent && rsiCurrent < 30 && priceCurrent > sma200Current &&
            emaShortPrevious <= emaLongPrevious && emaShortCurrent > emaLongCurrent)
        {
            // Sinal de compra
            if (PositionsTotal() == 0 || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
            {
                // Fechar posições de venda
                ClosePositions(POSITION_TYPE_SELL);
                // Abrir ordem de compra
                OpenBuyOrder(atrCurrent); // Passando o valor do ATR
            }
        }
        else if (emaPrevious > smaPrevious && emaCurrent < smaCurrent && rsiCurrent > 70 && priceCurrent < sma200Current &&
                 emaShortPrevious >= emaLongPrevious && emaShortCurrent < emaLongCurrent)
        {
            // Sinal de venda
            if (PositionsTotal() == 0 || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            {
                // Fechar posições de compra
                ClosePositions(POSITION_TYPE_BUY);
                // Abrir ordem de venda
                OpenSellOrder(atrCurrent); // Passando o valor do ATR
            }
        }
    }
    
    // Função para abrir ordem de compra
    void OpenBuyOrder(double atrCurrent)
    {
        double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) - atrCurrent * atrMultiplier, _Digits);
        double tp1 = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) + atrCurrent * tp1Multiplier, _Digits);
    
        // Enviar ordem de compra
        MqlTradeRequest request;
        MqlTradeResult result;
    
        ZeroMemory(request);
        ZeroMemory(result);
    
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol;
        request.volume = 0.1;
        request.type = ORDER_TYPE_BUY;
        request.price = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
        request.sl = sl;
        request.tp = tp1; // Modificar conforme a necessidade
        request.deviation = 10;
        request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
        request.type_time = ORDER_TIME_GTC;
    
        if (!OrderSend(request, result))
        {
            Print("Erro ao abrir ordem de compra: ", GetLastError());
        }
    }
    
    // Função para abrir ordem de venda
    void OpenSellOrder(double atrCurrent)
    {
        double sl = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) + atrCurrent * atrMultiplier, _Digits);
        double tp1 = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) - atrCurrent * tp1Multiplier, _Digits);
    
        // Enviar ordem de venda
        MqlTradeRequest request;
        MqlTradeResult result;
    
        ZeroMemory(request);
        ZeroMemory(result);
    
        request.action = TRADE_ACTION_DEAL;
        request.symbol = _Symbol;
        request.volume = 0.1;
        request.type = ORDER_TYPE_SELL;
        request.price = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
        request.sl = sl;
        request.tp = tp1; // Modificar conforme a necessidade
        request.deviation = 10;
        request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
        request.type_time = ORDER_TIME_GTC;
    
        if (!OrderSend(request, result))
        {
            Print("Erro ao abrir ordem de venda: ", GetLastError());
        }
    }
    
    // Função para fechar posições abertas
    void ClosePositions(int positionType)
    {
        for (long i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(_Symbol))
            {
                int positionTypeCurrent = PositionGetInteger(POSITION_TYPE);
                if (positionTypeCurrent == positionType)
                {
                    ulong ticket = PositionGetTicket(i);
                    if (ticket > 0)
                    {
                        MqlTradeRequest request;
                        MqlTradeResult result;
    
                        ZeroMemory(request);
                        ZeroMemory(result);
    
                        request.action = TRADE_ACTION_DEAL;
                        request.symbol = _Symbol;
                        request.volume = PositionGetDouble(POSITION_VOLUME); // Volume da posição
                        request.position = ticket; // Identificador da posição
                        request.price = (positionType == POSITION_TYPE_SELL) ? SymbolInfoDouble(_Symbol, SYMBOL_BID) : SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                        request.deviation = 10;
                        request.type_filling = ORDER_FILLING_IOC; // Alterado para IOC
                        request.type_time = ORDER_TIME_GTC;
    
                        if (!OrderSend(request, result))
                        {
                            Print("Erro ao fechar posição: ", GetLastError());
                        }
                        else
                        {
                            Print("Posição fechada com sucesso.");
                        }
                    }
                }
            }
        }
    }
    
    // Função para calcular os Order Blocks (não implementado no exemplo)
    void CalculateOrderBlocks()
    {
        // Implementar lógica de Order Blocks
    }
    

    Nesta versão ajustada:

    1. OrderSelect foi corrigido para PositionSelect.
    2. CloseOrders foi renomeado para ClosePositions para refletir melhor o que a função faz.
    3. Usados PositionGetDouble e PositionGetTicket para obter informações das posições.

    Esta versão deve eliminar os erros listados e funcionar corretamente.