Closing First and Last opposite trades out of 10 open trades

 
void CloseLast()   {
   
      OrdersTotal()==11; // 10 trades of 0.05 lots in one direction with distance of 150 and 1 trade of 0.5 lots in opposite direction ( i.e Hedge trade)
      int total = OrdersTotal();
      for (int i=OrdersTotal()-1;i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS)==true)
         int type   = OrderType();
               
   // Buy
         if( OrderMagicNumber()==MagicNumber
            &&
            (((Bid-OrderOpenPrice())/Point)>1500)
            &&
            OrderType()==OP_BUY
            )
            int TicketLast=OrderTicket();
             bool result = OrderClose(TicketLast, OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
   // Sell
         if( OrderMagicNumber()==MagicNumber
            &&
            (((OrderOpenPrice()-Ask)/Point)>1500)
            &&
            OrderType()==OP_SELL
            )
       TicketLast=OrderTicket();    
       result = OrderClose(TicketLast, OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
     }
   }
   //********************************************************************
   void CloseFirst() {
   
      OrdersTotal()==11;  // 10 trades of 0.05 lots in one direction with distance of 150 and 1 trade of 0.5 lots in opposite direction ( i.e Hedge trade)
      int total = OrdersTotal();
      for (int i=OrdersTotal()-1;i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS)==true)
         int type   = OrderType();
                 
   // Buy
         if( OrderMagicNumber()==MagicNumber
            &&
            (((Bid-OrderOpenPrice())/Point)<-1500)
            &&
            OrderType()==OP_BUY
            )
            int TicketFirst=OrderTicket();
            bool result = OrderClose(TicketFirst, OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
   // Sell
         if( OrderMagicNumber()==MagicNumber
            &&
            (((OrderOpenPrice()-Ask)/Point)<-1500)
            &&
            OrderType()==OP_SELL
            )
       TicketFirst=OrderTicket();    
      result = OrderClose(TicketFirst, OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
     }
   }

Hello Expert Please help me. I wasted so much time for creating function as described below-

I want closing First trade of Buy out of 10 open trades with same lot and last trades of Sell with higher lot one after other.But I am not getting expected result. Expert please suggest what is wrong in my code

<Deleted>

 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.  for (int i=0; i<=OrdersTotal(); i++)
         {
          if(OrderSelect(i-0,SELECT_BY_POS)==true) int type = OrderType();
          bool result = false;
          ⋮

    Your if statement does nothing. You continue even if the select fails. I prefer:

      for (int i=OrdersTotal - 1; i > 0; --i) if(
         OrderSelect(i,SELECT_BY_POS)
      && OrderMagicNumber()==MagicNumber
      ){
          int type = OrderType();
    
  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

  4. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    3. and check OrderSelect in case later positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

 
<Deleted>

please suggest if logic is correct or not. 

 
Jagdish Vasave - #: please suggest if logic is correct or not. 

What part of “edit your (original) post and use the CODE button (Alt-S)” was unclear to you?

 
William Roeder #:

What part of “edit your (original) post and use the CODE button (Alt-S)” was unclear to you?

I adit as per your suggestion, pl help me coding functions 
Reason: