Download MetaTrader 5

er returns error

To add comments, please log in or register
rafal85
22
rafal85  
hello
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double lot          = 0.01;      
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
static int oldOrdersHistoryTotal;
       
   if(oldOrdersHistoryTotal == 0) oldOrdersHistoryTotal = OrdersHistoryTotal();

   if(oldOrdersHistoryTotal != OrdersHistoryTotal() )
      {
      tt();
      }
      oldOrdersHistoryTotal = OrdersHistoryTotal();   
      
  }
  
//+------------------------------------------------------------------+

void tt()
{
bool result;
int cmd,error;

int total = OrdersHistoryTotal();

  for( int i=(OrdersHistoryTotal()-1);i>=0;i++)
{
    if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
cmd=OrderType();
double LOTS=OrderLots();


if(OrderProfit()>0)
{
      while(true)
    {
           result=close();
           
      if(result!=TRUE) { error=GetLastError(); Alert("Last1 = ",error); }
      else break;
    }
}

}
else { Print( "Error when order select ", GetLastError()); break; }
}
}

//+------------------------------------------------------------------+

int close()
{
bool result;
int cmd,error;
int total = OrdersTotal()-1;

  for( int i = total;i>0;i--)
{
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();

if(cmd==OP_BUY)
{
      while(true)
    {
           result=OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
           
      if(result!=TRUE) { error=GetLastError(); Alert("Last = ",error); }
      else break;
    }
}
               
if(cmd==OP_SELL)  
{
      while(true)
    {
           result=OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

      if(result!=TRUE) { error=GetLastError(); Alert("Last = ",error); }
      else break;
    }

}
else { Print( "Error when order select ", GetLastError()); break; }

}

}
return(false);
}

my code should close ALL opened positions except one when last closed order was profitable but it returns error

i will be very grateful if you can help me

in my opinion error is in that part of the code

if(OrderProfit()>0)
{
      while(true)
    {
           result=close();
           
      if(result!=TRUE) { error=GetLastError(); Alert("Last1 = ",error); }
      else break;
    }
}
Keith Watford
Moderator
10369
Keith Watford  

Firstly, you don't check for the last closed order, so any order in history that closed with profit will call the function close()

int close()

as the function is a bool, you should get into the habit of declaring it such

bool close()
if(OrderProfit()>0)
{
      while(true)
    {
           result=close();
           
      if(result!=TRUE) { error=GetLastError(); Alert("Last1 = ",error); }
      else break;
    }
}

The fuction close() can only return false, so result will always be false and you will be stuck in an endless loop.

rafal85
22
rafal85  

Thanks,

So what is the proper way to check for profitability of last closed order and how to close ALL orders except first after that?

Keith Watford
Moderator
10369
Keith Watford  
   static int oldOrdersHistoryTotal;

   if(oldOrdersHistoryTotal==0) oldOrdersHistoryTotal=OrdersHistoryTotal();

   if(oldOrdersHistoryTotal!=OrdersHistoryTotal())
     {
      datetime time_close=0;
      int last_ticket=0;
      for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
           {
            if(OrderCloseTime()>time_close)
              {
               time_close=OrderCloseTime();
               last_ticket=OrderTicket();
              }
           }
        }
      if(OrderSelect(last_ticket,SELECT_BY_TICKET) && OrderProfit()>0)
        {
         //Code to close orders

        }

      oldOrdersHistoryTotal=OrdersHistoryTotal();
     }

This should get you started

rafal85
22
rafal85  

thanks for that code but it dose not work

using that code I am able to close only 2 trades but not all of them

Keith Watford
Moderator
10369
Keith Watford  
rafal85:

thanks for that code but it dose not work

using that code I am able to close only 2 trades but not all of them


If it closes 2 trades, then it means that the code is working. The problem is your code that actually closes the trades
rafal85
22
rafal85  

hello mate

in my opinion, if I use your code, every time new order pops up in history it detects it and terminates closing existing orders.

I need code which would close ALL opened orders (no matter the profit is) except one when you close any of currently opened orders which is profitable.

I have been trying to do that for over a week but not succeeded.

To add comments, please log in or register