Unable to understand the difference

 

Block 1

int trades_count(){
   int trades_counts_var=0;
   for(int i=0; i<OrdersTotal(); i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber){
         if(OrderType()==OP_BUY) trades_counts_var=trades_counts_var+1;
         if(OrderType()==OP_SELL) trades_counts_var=trades_counts_var+1;
        }
     }
   return(trades_counts_var);
  }

Block 2

int trades_count_type(string type){
   int trades_counts_var=0;
   for(int i=0; i<OrdersTotal(); i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber){
         if(OrderType()==OP_BUY && type=="buy") trades_counts_var=trades_counts_var+1;
         if(OrderType()==OP_SELL && type=="sell") trades_counts_var=trades_counts_var+1;
        }
     }
   return(trades_counts_var);
  }

I think that both are doing the same thing.

Can some one explain please?

 
Qæs:

Block 1

Block 2

I think that both are doing the same thing.

Can some one explain please?

No. Block 1 return count of BUY and SELL trades

Block 2 return only "type" passed in parameters. (BUY or SELL)

 
They could be combined and simplified.
int trades_count(int type=WRONG_VALUE){
   int n=0;
   for(int i=OrdersTotal()-1; i >= 0; --i) if(
      OrderSelect(i,SELECT_BY_POS)
   && OrderMagicNumber() == MagicNumber){
   && (type == WRONG_VALUE || type == OrderType() )
   && OrderSymbol()      == Symbol()
   ) ++n;
   return n;
}
 
William Roeder:
They could be combined and simplified.

What should I do if I want to use the code (you provided) directly in OnTick function instead of creating a separate function and calling it. I mean how I will handle the type variable for checking total opened orders, total opened buy orders and total opened sell orders?

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
 
Qæs: What should I do if I want to use the code directly instead of creating a separate function and calling it.

You should step away from the keyboard, take a programming class or at least read the book Programming for Dummies. Why would you want to make OnTick harder to understand?

 
William Roeder:

You should step away from the keyboard, take a programming class or at least read the book Programming for Dummies.

I was not expecting this from you. Whenever I asked about something here, I always waited for your comment because most of the times it was you, who got exactly what was in my mind and you pointed me to the right direction. (Is it someone else/new using THIS account now?)

William Roeder:
Why would you want to make OnTick harder to understand?.

With your help and guidance in this forum, I wrote a program two years ago. Then someone stole my laptop and (...)

Lets just say that writing "harder to understand OnTick" at least once after a long gap will help me in not to do this again.

Will you please guide me as you always did?

PS: Please check the code you shared.

int trades_count(int type=WRONG_VALUE){
   int n=0;
   for(int i=OrdersTotal()-1; i >= 0; --i) if(
      OrderSelect(i,SELECT_BY_POS)
//   && OrderMagicNumber() == MagicNumber){
   && (OrderMagicNumber() == MagicNumber)
   && (type == WRONG_VALUE || type == OrderType() )
   && OrderSymbol()      == Symbol()
   ) ++n;
   return n;
}
 

Qæs:

Lets just say that writing "harder to understand OnTick" at least once after a long gap will help me in not to do this again.

Will you please guide me as you always did?

PS: Please check the code you shared.

  1. You could write a thousand line function, repeating the (almost) same thing over and over. Do you really think you could get it to work? Do you really think you could understand it when it doesn't? Do you really expect to understand a month from now why two loops are almost the same but aren't and why?

  2. int nTrades = trades_count();
    int nBuys   = trades_count(OP_BUY);

    Do you think you could understand those two lines, what they do, why they are different?

  3. Check it for what?

 
William Roeder:
  1. You could write a thousand line function, repeating the (almost) same thing over and over. Do you really think you could get it to work? Do you really think you could understand it when it doesn't? Do you really expect to understand a month from now why two loops are almost the same but aren't and why?

  2. Do you think you could understand those two lines, what they do, why they are different?

  3. Check it for what?

I actually was stuck in "Why someone used the same loop again and again for the same one result which can be obtained using one loop?".

@remcous told that its not one result. but "Why two loops?" was still in my mind, and then you solved the puzzle.

I was in the middle of mixing everything when I came here again and saw your comment.

I thought to mix the solution that you shared, too. So I asked you all those things.

Thanks God I was not in front of you.

Reason: