Close last trade with new order

 

Hi,


I have orderSend and OrderClose.


OrderSend works, OrderClose closes all trades.


I need to close only last trade.

So, If I have:


I want to close trade in red box.

for(int Counter = 0; Counter <= OrdersTotal()-1; Counter++)
   {
      if(OrderSelect(Counter,SELECT_BY_POS))
      {
         if( OrderSymbol() == _Symbol && (OrderType() == OP_BUY || OrderType() == OP_SELL))
         {
                
                 // Close Order
            int CloseTicket = OrderTicket();
            double CloseLots = OrderLots();
          //  while(IsTradeContextBusy()) Sleep(10);
                
         RefreshRates();            
            double ClosePrice = MarketInfo(_Symbol,MODE_BID);
          if(OrderType() == OP_SELL) ClosePrice = MarketInfo(_Symbol, MODE_ASK);
                
                    bool Closed = OrderClose(CloseTicket,CloseLots,ClosePrice,3); // close it
         
         if(Closed == true)
         {
            Print("Close All Market Orders with loss.");
         }
         }
         }
         }





Regards,
Dejan

 
  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?
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. 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 a 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.
                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().

  3. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  4. Dejan KrapezOrderClose closes all trades. I need to close only last trade.

    Do you stop the loop after closing the last order?

 
Dejan Krapez:

Hi,


I have orderSend and OrderClose.


OrderSend works, OrderClose closes all trades.


I need to close only last trade.

So, If I have:


I want to close trade in red box.





Regards,
Dejan

int LasOrder()
  {

int Last_TicketNumber=0;
double Last_Profit=0.0;
   int Last_Total=OrdersHistoryTotal()-1;


 for(int i=Last_Total; i>=Last_Total; i--)
        {
            if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
            {
            Last_TicketNumber=OrderTicket();
            Last_Profit=OrderProfit();
            break;
        //    Alert(OrderProfit()+" "+Last_Profit);         
            }    
        }  
    
       Alert("Last Order : ",string(Last_TicketNumber),"  Take Profit: ",DoubleToString(Last_Profit,2));
//----
   return(0);
  }