Download MetaTrader 5

How to check current order ?

To add comments, please log in or register
baharin
78
baharin  

Hi...

How could i check the current order is BUY or SELL ?.

I want to do like this :-

IF current order is BUY then

do something

IF current order is SELL then

do something


Refer to the terminal, I have one post on SELL order. But once I test my expert advisor, the program return the OrderType value as OP_BUY ?. Why ? It shoud be return the current order is SELL right ?...

This is the trace message:

[TRACE-4] Close buy and open sell for 0, current post: 0.

This is my code that the program refer to:-
      if (OrdersTotal() > 0) {
         TicketNo4 = OrderTicket();
         if (OrderType() == OP_BUY) {
            Print ("[TRACE-4] Close buy and open sell for " + TicketNo4 + ", current post:" + OrderType());
            //CloseBuy(TicketNo4);
            //TicketNo4 = OpenSell();
          }
                
      }
Keith Watford
Moderator
10561
Keith Watford  
You haven't selected any order
baharin
78
baharin  
GumRai:
You haven't selected any order

Hi..


I have change my code like this.... is it ok ?

      if (OrdersTotal() > 0) {
         TicketNo4 = OrderTicket();
         if (OrderSelect(TicketNo4, SELECT_BY_TICKET) == true) {
            if (OrderType() == OP_BUY) {
               Print ("[TRACE-4] Close buy and open sell for " + TicketNo4 + ", current post:" + OrderType() + ", No:" + OrdersTotal() + ", op_buy:" + OP_BUY + ", op_sell" + OP_SELL);
               //CloseBuy(TicketNo4);
               //TicketNo4 = OpenSell();
             }
         }
Tjipke de Vries
6370
Tjipke de Vries  
bh_hensem:

Hi..


I have change my code like this.... is it ok ?


You can't be sure selecting this way if the selected trade is from your EA / strategy
Ian Venner
2584
Ian Venner  
bh_hensem:

Hi..

I have change my code like this.... is it ok ?

No, you called OrderTicket() before you selected an order. The order information functions get their values from the last order selected by OrderSelect().
Daniel Petrovai
440
Daniel Petrovai  
bh_hensem:

I want to do like this :-

IF current order is BUY then do something

IF current order is SELL then do something


All you need to do is to correctly select the order and once selected, do whatever you want with it.

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)                          // select order using OrderSelect function
         Print("Failed to select order, error : "+ErrorDescription(GetLastError())); // if function fails, print error to let me know what's wrong
      if(OrderSymbol() != Symbol() || OrderMagicNumber() !=MagicNumber)continue;     // if the order is of different symbol or magic number, skip over to the next order
      if(OrderType()==OP_BUY)
         {
         // your code here
         }

      if(OrderType()==OP_SELL)
         {
          // your code here
         }
        

Hope it helps.

Keith Watford
Moderator
10561
Keith Watford  

thrdel,

there is a problem with your code

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)                          // select order using OrderSelect function
         Print("Failed to select order, error : "+ErrorDescription(GetLastError())); // if function fails, print error to let me know what's wrong

//------------If OrderSelect fails, you continue as if it had been successful

      if(OrderSymbol() != Symbol() || OrderMagicNumber() !=MagicNumber)continue;     // if the order is of different symbol or magic number, skip over to the next order
      if(OrderType()==OP_BUY)
         {
         // your code here
         }

      if(OrderType()==OP_SELL)
         {
          // your code here
         }
        
Daniel Petrovai
440
Daniel Petrovai  
GumRai:

thrdel,

there is a problem with your code


Well, I guess we can throw a continue in there at no extra cost if you insist but as far as warning messages, even without it, you still not going to get any.

Thanks for pointing that out.

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)                          // select order using OrderSelect function
         {
         Print("Failed to select order, error : "+ErrorDescription(GetLastError())); // if function fails, print error to let me know what's wrong
         continue;
         }
      if(OrderSymbol() != Symbol() || OrderMagicNumber() !=MagicNumber)continue;     // if the order is of different symbol or magic number, skip over to the next order
      if(OrderType()==OP_BUY)
         {
         // your code here
         }

      if(OrderType()==OP_SELL)
         {
          // your code here
         }
To add comments, please log in or register