Download MetaTrader 5

How to close seclected orders

To add comments, please log in or register
Yu Tsai
163
Yu Tsai  

A new bee.  I want to close last 2 orders among multi orders, but not working.  Help please? Thank you very much in advance!

{
string lastOneTicket;
string lastSecondTicket;
string lastOneProfit;
string lastSecondProfit;

for(int i=(OrdersTotal()-1);i>=0;i--)
 {
   OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
    {
      lastOneTicket=OrderTicket();
      lastOneProfit=OrderProfit();
           break;
    }
 }


for(int i=(OrdersTotal()-1);i>=0;i--)
 {
   OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
    {
      lastSecondTicket=OrderTicket();
      lastSecondProfit=OrderProfit();
           break;
    }
 }
 
 if (lastOneProfit>lastSecondProfit)
 OrderClose(lastOneTicket&&lastSecondTicket),OrderLots(), Bid, Slippage, Red);

     return;
 }
Denis Sartakov
1185
Denis Sartakov  
Yu Tsai:

A new bee.  I want to close last 2 orders among multi orders, but not working.  Help please? Thank you very much in advance!

try this code, it will close all orders with profit:

//+------------------------------------------------------------------+
//|                                                         work.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
#include <stderror.mqh>
#include <stdlib.mqh>
#include <WinUser32.mqh>
//+---------------------------------------------------------------------------+
input int  ps32_Magic = 1;
//+---------------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   
}
//+------------------------------------------------------------------+
int    gs32_TicketToClose;
int    gs32_SlippagePips = 10;
double gd_Lot;
double gd_Price;
//+------------------------------------------------------------------+
void OnTick()
{
   CloseProfitOrders();
}
//+------------------------------------------------------------------+
void CloseProfitOrders()
{
   int s32_OrdersTotal;
   s32_OrdersTotal = OrdersTotal();
   
        for (int i = 0;  i < s32_OrdersTotal;  i++ )
        {
                if (!OrderSelect(i,SELECT_BY_POS))
                {
                    nt s32_LastError = GetLastError();
                    Print("OrderSelect( "+DoubleToStr(i,0)+", SELECT_BY_POS ) - Error #"+DoubleToStr(s32_LastError,0)); 
                       continue;
                }
                if (OrderMagicNumber() != ps32_Magic)
                   continue;
                
                if (OrderSymbol() != Symbol())
                   continue;
                
                if (OrderType() != OP_BUY && OrderType() != OP_SELL)
                   continue;
                
                if (OrderProfit() > 0)
                {
                   gs32_TicketToClose = OrderTicket();
                   gd_Lot             = OrderLots();
                   gd_Price           = OrderType() == OP_BUY ? Bid : Ask;
         
                   if (_CloseOrder(gs32_TicketToClose,gd_Lot,gd_Price))
                      continue;
                   else
                      break;
               }   
          }
}
//+------------------------------------------------------------------+
bool _CloseOrder(int s32_TicketToClose,double d_Lot,double d_Price)
{
   string sw_Message;

   while(IsTradeContextBusy())
   {
   }
   
   ResetLastError();
   
   if (OrderClose(s32_TicketToClose,d_Lot,d_Price,gs32_SlippagePips,clrNONE))
   {
      return(true);
   }
   
   int s32_LastError = GetLastError();
   sw_Message = "Order close error. Ticket ="+s32_TicketToClose+" Error code = "+s32_LastError+" : "+ErrorDescription(s32_LastError);
   Print(sw_Message);
   return(false);
}  
//+------------------------------------------------------------------+
To add comments, please log in or register