look at my code,why report 0 zero,but on test there was no trade order?

 
why did my code has no trade order?
//+------------------------------------------------------------------+
//|                                                      celue1.mq5  |
//|                        Copyright 2024, MetaQuotes Software Corp.  |
//|                                       https://www.mql5.com        |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>

input int lookbackPeriod = 30;  
input double lotSize = 0.1;     

double lastMaxHigh = 0.0;
double lastMinLow = 0.0;

int OnInit()
{
    Print("Expert Advisor initialized");
    return(INIT_SUCCEEDED);
}

void OnTick()
{
    string symbol = Symbol();
    Print("OnTick called with symbol: ", symbol);

    double highs[], lows[];
    ArraySetAsSeries(highs, true);
    ArraySetAsSeries(lows, true);

    int copiedHigh = CopyHigh(symbol, PERIOD_CURRENT, 0, lookbackPeriod, highs);
    int copiedLow = CopyLow(symbol, PERIOD_CURRENT, 0, lookbackPeriod, lows);

    Print("Copied high prices: ", copiedHigh, " Copied low prices: ", copiedLow);

    if (copiedHigh <= 0 || copiedLow <= 0)
    {
        Print("Error copying high/low prices: ", GetLastError());
        return;
    }

    int maxHighIndex = ArrayMaximum(highs);
    int minLowIndex = ArrayMinimum(lows);

    Print("Max high index: ", maxHighIndex, " Min low index: ", minLowIndex);

    if (maxHighIndex < 0 || minLowIndex < 0)
    {
        Print("Error finding maximum or minimum price index");
        return;
    }

    double maxPrice = highs[maxHighIndex];
    double minPrice = lows[minLowIndex];

    if (maxPrice > lastMaxHigh)
    {
        lastMaxHigh = maxPrice;
        lastMinLow = minPrice;
        Print("Opening buy order");
        OpenBuyOrder();
    }
    else if (minPrice < lastMinLow)
    {
        lastMaxHigh = maxPrice;
        lastMinLow = minPrice;
        Print("Opening sell order");
        OpenSellOrder();
    }
}

void OnDeinit(const int reason)
{
    Print("Expert Advisor deinitialized with reason: ", reason);
}

void OpenBuyOrder()
{
    CTrade trade;
    MqlTradeRequest request;
    MqlTradeResult result;
    ZeroMemory(request);
    ZeroMemory(result);

    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = lotSize;
    request.type = ORDER_TYPE_BUY;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
    request.deviation = 10;  
    request.magic = 0;       
    request.comment = "Buy Order";

    if (!trade.OrderSend(request, result))
    {
        Print("Error opening buy order: ", GetLastError());
    }
    else
    {
        Print("Buy order opened successfully");
    }
}

void OpenSellOrder()
{
    CTrade trade;
    MqlTradeRequest request;
    MqlTradeResult result;
    ZeroMemory(request);
    ZeroMemory(result);

    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = lotSize;
    request.type = ORDER_TYPE_SELL;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
    request.deviation = 10;  
    request.magic = 0;       
    request.comment = "Sell Order";

    if (!trade.OrderSend(request, result))
    {
        Print("Error opening sell order: ", GetLastError());
    }
    else
    {
        Print("Sell order opened successfully");
    }
}

void CloseAllBuyOrders()
{
    CTrade trade;
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionSelect((string)i))
        {
            ENUM_POSITION_TYPE positionType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
            if (positionType == POSITION_TYPE_BUY)
            {
                MqlTradeRequest request;
                MqlTradeResult result;
                ZeroMemory(request);
                ZeroMemory(result);

                request.action = TRADE_ACTION_DEAL;
                request.symbol = PositionGetString(POSITION_SYMBOL);
                request.volume = PositionGetDouble(POSITION_VOLUME);
                request.type = ORDER_TYPE_SELL;
                request.position = PositionGetInteger(POSITION_TICKET);
                request.price = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_BID);
                request.deviation = 10;  
                request.magic = 0;       
                request.comment = "Close Buy Order";

                if (!trade.OrderSend(request, result))
                {
                    Print("Error closing buy order: ", GetLastError());
                }
                else
                {
                    Print("Buy order closed successfully");
                }
            }
        }
    }
}

void CloseAllSellOrders()
{
    CTrade trade;
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        if (PositionSelect((string)i))
        {
            ENUM_POSITION_TYPE positionType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
            if (positionType == POSITION_TYPE_SELL)
            {
                MqlTradeRequest request;
                MqlTradeResult result;
                ZeroMemory(request);
                ZeroMemory(result);

                request.action = TRADE_ACTION_DEAL;
                request.symbol = PositionGetString(POSITION_SYMBOL);
                request.volume = PositionGetDouble(POSITION_VOLUME);
                request.type = ORDER_TYPE_BUY;
                request.position = PositionGetInteger(POSITION_TICKET);
                request.price = SymbolInfoDouble(PositionGetString(POSITION_SYMBOL), SYMBOL_ASK);
                request.deviation = 10;  
                request.magic = 0;       
                request.comment = "Close Sell Order";

                if (!trade.OrderSend(request, result))
                {
                    Print("Error closing sell order: ", GetLastError());
                }
                else
                {
                    Print("Sell order closed successfully");
                }
            }
        }
    }
}
 
Traders and coders are coding for free:
  • if it is interesting for them personally, or
  • if it is interesting for many members of this forum.

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

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.10.29
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Hi

Check the logs while you test the EA there are messages which tell you what the error is here. In your case it’s unsupported filling mode for the lot size which you need to define in the order request.

You can use something like this:

request.type_filling = GetFilling(Symbol());
…
ENUM_ORDER_TYPE_FILLING GetFilling( const string Symb, const uint Type = ORDER_FILLING_FOK )
{
  const ENUM_SYMBOL_TRADE_EXECUTION ExeMode = (ENUM_SYMBOL_TRADE_EXECUTION)::SymbolInfoInteger(Symb, SYMBOL_TRADE_EXEMODE);
  const int FillingMode = (int)::SymbolInfoInteger(Symb, SYMBOL_FILLING_MODE);

  return((FillingMode == 0 || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
         (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
           ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
          (ENUM_ORDER_TYPE_FILLING)Type);
}

that I do not recommend printing out all those messages in the OnTick function, those are printed every tick and then you cannot see the important messages with errors print those logs only when something new is happening, when you send order request or modify trades, detect a signal not on every tick.

Have a nice day👍📊