Please help - Expression is always false error

 
Hi

Could somebody please help me? 

i am trying to get the below code to compile in mql5 and keep receiving 2 errors for expression is always false for line number 44 and 66 (I have highlighted below)

 //+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    // Check if there are no open positions for the symbol
    if (!PositionSelect(Symbol()))
    {
        // Get previous weekly candle data
        double lastClose = iClose(Symbol(), PERIOD_W1, 0);
        double lastOpen = iOpen(Symbol(), PERIOD_W1, 0);
        double prevLow = iLow(Symbol(), PERIOD_W1, 1);
        double prevHigh = iHigh(Symbol(), PERIOD_W1, 1);

        MqlTradeRequest request;
        MqlTradeResult result;

        ZeroMemory(request);
        ZeroMemory(result);

        // If the last weekly candle was bullish
        if (lastClose > lastOpen)
        {
            request.action = TRADE_ACTION_DEAL;
            request.symbol = Symbol();
            request.volume = 1.0;
            request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
            request.sl = prevLow;
            request.tp = 0;
            request.type = ORDER_TYPE_BUY;
            request.type_filling = ORDER_FILLING_IOC;
            
            // Send the trade request
            if (OrderSend(request, result) == TRADE_RETCODE_DONE)
            {
                Print("Buy order placed successfully.");
            }
            else
            {
                Print("Failed to place buy order. Error code: ", GetLastError());
            }
        }
        // If the last weekly candle was bearish
        else if (lastClose < lastOpen)
        {
            request.action = TRADE_ACTION_DEAL;
            request.symbol = Symbol();
            request.volume = 1.0;
            request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
            request.sl = prevHigh;
            request.tp = 0;
            request.type = ORDER_TYPE_SELL;
            request.type_filling = ORDER_FILLING_IOC;
            
            // Send the trade request
            if (OrderSend(request, result) == TRADE_RETCODE_DONE)
            {
                Print("Sell order placed successfully.");
            }
            else
            {
                Print("Failed to place sell order. Error code: ", GetLastError());
            }
        }
    }

    return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+ 

Improperly formatted code edited by moderator.

 

In the future, please use the CODE button (Alt-S) when inserting code.

Code button in editor

 
First problem ...

If you reference the documentation for the OrderSend() function, what does it state about the return datatype and possible values?

bool  OrderSend(
   MqlTradeRequest&  request,      // query structure
   MqlTradeResult&   result        // structure of the answer
   );

Return Value

In case of a successful basic check of structures (index checking) returns true. However, this is not a sign of successful execution of a trade operation. For a more detailed description of the function execution result, analyze the fields of result structure.

Does it state that the return value is an enumeration of which TRADE_RETCODE_DONE is a possible value?

No, it states that the return value is a bool (boolean, true or false).

Second problem ...

Why are you trying to obtain OHLC data, and trade data, and trying to execute trades in what is supposed to be the initialisation event handler — OnInit()?

You should only be initialising the EA and nothing else. Your code should be in the OnTick() event handler.

 
Fernando Carreiro #:

In the future, please use the CODE button (Alt-S) when inserting code.

Thank you, first time on the website, will make sure I do this going forwards :)