Have an error 4051 in my code and can't find where is coming from

 

Here's the code for it

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnTick()
  {
//---
// Checks the time for trading;-------------------------------------------------------------------------------
   if(Hour()> 1 &&  Hour() < 22)   //time condition less than 12am and greater than 2am
     {
      //---------------------------------------------------------------------------------------------------------------------------
      if(AccountNumber() ==  52946085) //check the trading id of the account
        {

         double sum = 0;
         for(int a=0; a <= OrdersHistoryTotal(); a++)
           {
            if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
              {
               sum = sum + OrderProfit();
               if(sum < 49)
                 {
                  double lot = 0.1;
                  double fastma = iMA("",5,3,0,MODE_EMA,PRICE_CLOSE,0);
                  double slowma = iMA("",5,15,0,MODE_EMA,PRICE_CLOSE,0);
                  double checkma = iMA("",5,3,1,MODE_EMA,PRICE_CLOSE,0);
                  double checkma2 = iMA("",5,15,1,MODE_EMA,PRICE_CLOSE,0);

                  // ------------------------------------------------------------------------------------------------------------------
                  //Opening Signal for bullish
                  if(fastma > slowma + 1 * Point && checkma < checkma2)
                    {

                     double openlong = OrderSend("EURUSD",OP_BUY,lot,Ask,3,0,Ask+40*Point,"SXG",7899);
                     SendNotification("OPENED EURUSD");
                     Alert(" OPENED EURUSD");

                     Sleep(300000);//wait for 15minutes
                    }

                  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                 }
              }

           }
         //-----------------------------------------------------------------------------------------------------------------------------
         //closing bracket for time check and account check
        }
      else
        {
         //SendNotification(
         SendNotification("Account didnt connect because of: wrong account number");

        }
     }

   Print(GetLastError());
  }
 
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.          double sum = 0;
             for(int a=0; a <= OrdersHistoryTotal(); a++)
               {
                if(OrderSelect(a,SELECT_BY_POS,MODE_HISTORY))
                  {
                   sum = sum + OrderProfit();

    MT4:

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

    3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
                "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

      Broker History
      FXCM
      Commission - <TICKET>
      Rollover - <TICKET>

      >R/O - 1,000 EUR/USD @0.52

      #<ticket>  N/A
      OANDA
      Balance update
      Financing (Swap: One entry for all open orders.)

  3.                sum = sum + OrderProfit();
                   if(sum < 49)

    Do your sum. Then after the loop, decide what to do. Currently you will be generating many orders.