Order select , step by step easy way to use Orderelect() function to count trades

 

Hi,

If you're here, you may need help using OrderSelect() in your EA, right?

Add this custom function to your EA and when called upon, it will return the count of that EA's orders.

As you know, OrdersTotal() will return the total numbers of orders not just the ones place by a particular ea. So, here is hoe it works :

Include (copy /paste) #include <stdlib.mqh> at the top of your code. It will help you with error description if you'll have any errors

//+------------------------------------------------------------------+
#property copyright "MT4"
#include <stdlib.mqh>

Define a global int (before the OnInit() function) like this : int myTrades;

int        myTrades;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//---

The name you use in your EA for magic number must match the name this function uses so either change the name of the variable you use to MagicNumber or

replace MagicNumber in the function with your variable name for magic number. Either way is fine. If you don't know what magic number is look it up or ask.

So, copy/paste this function to the end of your EA

Thanks Thirteen for improving the code

//========================================================================
for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
   if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
      {
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
         if(OrderType()==OP_SELL || OrderType()==OP_BUY)
            count++;
      }
   else
      Print("Error selecting orders in CountTrades() function : ",ErrorDescription(GetLastError()));
   }


Then, throughout your code, whenever you need to know how many orders are open by this EA (it will count pending orders as well) you just have to call the function

by inserting this code:

myTrades = CountTrades();

That's it. It is that easy. Every time the function is called it will update the counted trades and store it in myTrades variable.

Feel free to use the code, change it to include OP_BUYSTOP, OP_BUYLIMIT, etc if you need to count pending orders as well.

Have fun and if you have any questions, ask away.

Enjoy

 
thrdel:

Hi,

If you're here, you may need help using OrderSelect() in your EA, right?

Add this custom function to your EA and when called upon, it will return the count of that EA's orders.

As you know, OrdersTotal() will return the total numbers of orders not just the ones place by a particular ea. So, here is hoe it works :

Include (copy /paste) #include <stdlib.mqh> at the top of your code. It will help you with error description if you'll have any errors

If you must print the Error Description then also print the error number, the number is unambiguous in it's meaning.
 
thrdel:

Hi,

If you're here, you may need help using OrderSelect() in your EA, right?

...

Have fun and if you have any questions, ask away.

Enjoy

Thank you for sharing your code.

I can't see a reason to use these double if statements :

   if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)
   continue;
   if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
The first one is unnecessary.
 
angevoyageur:

Thank you for sharing your code.

I can't see a reason to use these double if statements :

The first one is unnecessary.


The reason is that for some of us who understand this :

      OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

the code doesn't need any explanations but for a newbie I believe that the logic is easier to understand the other way. That's my opinion any way and which way is clearer or easier to understand, that's up to the reader.

Also, I thought we are going to try and stop this nonsense : RaptorUK "If you must print the Error Description then also print the error number, the number is unambiguous in it's meaning."

We all know that the error number is printed in the expert log even if there's no call to the Print() function, just by default, right?

The ErrorDescription() function provide a description in addition to the error number printed by default.

I fail to see what's the point of that comment. Or how it is intended to help .Do you?

 
thrdel:


The reason is that for some of us who understand this :

the code doesn't need any explanations but for a newbie I believe that the logic is easier to understand the other way. That's my opinion any way and which way is clearer or easier to understand, that's up to the reader.

I can't agree with that, but it's not important. As you wrote, it's up to the reader.

Also, I thought we are going to try and stop this nonsense : RaptorUK "If you must print the Error Description then also print the error number, the number is unambiguous in it's meaning."

We all know that the error number is printed in the expert log even if there's no call to the Print() function, just by default, right?

The ErrorDescription() function provide a description in addition to the error number printed by default.

I fail to see what's the point of that comment. Or how it is intended to help .Do you?

The error description in one log and the error number in an other log. I agree with RaptorUK, it's better to also print the error number.
 
thrdel:


The reason is that for some of us who understand this :

the code doesn't need any explanations but for a newbie I believe that the logic is easier to understand the other way. That's my opinion any way and which way is clearer or easier to understand, that's up to the reader.

Also, I thought we are going to try and stop this nonsense : RaptorUK "If you must print the Error Description then also print the error number, the number is unambiguous in it's meaning."

We all know that the error number is printed in the expert log even if there's no call to the Print() function, just by default, right?

The ErrorDescription() function provide a description in addition to the error number printed by default.

I fail to see what's the point of that comment. Or how it is intended to help .Do you?

Nonsense ? what nonsense ? no, not all errors are output without Print()ing them . . . the error number is unambiguous and is easier to report by Users without error . . . if you disagree that's OK, I'm just trying to make a suggestion, ignore it if you wish, I won't lose any sleep. Just please stop being so hostile . . .
 
thrdel:


The reason is that for some of us . . .

the code doesn't need any explanations but for a newbie I believe that the logic is easier to understand the other way. . . .

I agree with angevoyaguer. The two statements, I believe, are mutually exclusive. So, the first IF statement is not needed and may actually be more confusing to newer programmers.

As an example, the following is my current trade count function (for build 509):

bool CountOpenTrades(int& count[], bool verbose = false) {
   int c_pending = 0, c_buy = 0, c_sell = 0;
      
   GetLastError();   
   for (int i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderSelect(i, SELECT_BY_POS))
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == EAMN) // EAMN is a global variable that contains the magic number
            switch (OrderType()) {
               case OP_BUYLIMIT:
               case OP_BUYSTOP:
                  c_pending++;
               case OP_BUY:
                  c_buy++; break;
               case OP_SELLLIMIT:
               case OP_SELLSTOP:
                  c_pending++;
               case OP_SELL:
                  c_sell++; break;
               default:
                  break;
            }
   
   int error = GetLastError();
   if (error > 0) {
      if (verbose)
         Print ("CountOpenTrades(): An unexpected error has occurred.  Error # ", error);
      return (false);
   }
   
   count[0] = c_buy + c_sell; // total open trades
   count[1] = c_pending;      // total pending trades
   count[2] = c_buy;          // total buy trades
   count[3] = c_sell;         // total sell trades

   return (true);
}

My function isn't perfect...but it works for me at the moment. It will be interesting to see how I can improve it when I rewrite it for build 600+.

 
Thirteen:

I agree with angevoyaguer. The two statements, I believe, are mutually exclusive. So, the first IF statement is not needed and may actually be more confusing to newer programmers.

As an example, the following is my current trade count function (for build 509):

My function isn't perfect...but it works for me at the moment. It will be interesting to see how I can improve it when I rewrite it for build 600+.


Same here if I may quote you "My function isn't perfect...but it works"; Anyone care to prove me wrong?

I believe we are disputing now which one's wrong : x++ ; x=x+1 or x+=1 if you want to increment x by 1 ?

For an intermediate level I say maybe but for a newbie ? Really ? More confusing ?

Well, good to have more opinions I guess.

I would still wait for a beginner to hear his/hers opinion if it is confusing or not.

 
angevoyageur:
I can't agree with that, but it's not important. As you wrote, it's up to the reader.
The error description in one log and the error number in an other log. I agree with RaptorUK, it's better to also print the error number.


So you think that error number in one log and error description in another log is bad but error number in one log and error description and another error number in another log is better ?

Care to explain why? I'm by any means no expert, maybe I learn something today.

 
thrdel:


So you think that error number in one log and error description in another log is bad but error number in one log and error description and another error number in another log is better ?

Care to explain why? I'm by any means no expert, maybe I learn something today.

You are trying to address your code to newbies, right ? My opinion is that for a newbie in programming it's sufficient to learn how to use and check one log. As you know there two logs available when you are running a code attached to a live chart. So, as your code have some controls on what is output to Experts log and not to Journal log, it's more easy to use the Experts log, for verbose (description) output and for error number output.

Also, I suggest you to calm down, as most of your posts in this thread, seems too much aggressive or hostile. We are all trying to help and/or learn.

 
thrdel:


Same here if I may quote you "My function isn't perfect...but it works"; Anyone care to prove me wrong?

. . .


I mean no disrespect, but I disagree. The statement:

if(OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber)
   continue;

is inefficient coding. The FOR loop works without it, so why use that code? Also, I believe there is a logic error inside the FOR loop...what happens after OrderSelect() returns false (assuming for the moment it does) and the error message is printed to the log? The very next statement is an IF statement which uses OrderSymbol() and OrderMagicNumber(), but those functions may not return the correct information because the OrderSelect() failed. You may be able to correct that logic error by adding the Continue operator after printing the error message to the log or by rewriting the contents of the FOR loop without the logical negation NOT. For example:

for(trade=OrdersTotal()-1;trade>=0;trade--)
   {
   if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
      {
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
         if(OrderType()==OP_SELL || OrderType()==OP_BUY)
            count++;
      }
   else
      Print("Error selecting orders in CountTrades() function : ",,ErrorDescription(GetLastError()));
   }

.

Reason: