Problem with OrdersCount++

David Diez  

I wrote a simple code to test OrdersCount having (3) different trades opened:

void OnTick(){
   CTrade trade;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
      int OrdersCount=0;
      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){}
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){}
            OrdersCount++;Print("OrdersCount is ",OrdersCount," symbol: ",SName);
            }
         }
      }
   }

Print result is this:

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: EURAUD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: USDCAD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: AUDCAD

---

It's getting the right symbols, but count them as single ones.

What I'm trying is to get OrdersCount=3;

---

Any help is welcome.

amando  

You start the count on each symbol new and only check the actual symbol.

to get all positions you dont have to select the symbol first

Nikolaos Pantzos  
David Diez:

I wrote a simple code to test OrdersCount having (3) different trades opened:

Print result is this:

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: EURAUD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: USDCAD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: AUDCAD

---

It's getting the right symbols, but count them as single ones.

What I'm trying is to get OrdersCount=3;

---

Any help is welcome.

Move the variable reset upper.

void OnTick(){
   CTrade trade;
   int OrdersCount=0;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
         for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){}
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){}
            OrdersCount++;Print("OrdersCount is ",OrdersCount," symbol: ",SName);
            }
         }
      }
   }





David Diez  
amando:

You start the count on each symbol new and only check the actual symbol.

to get all positions you dont have to select the symbol first

Also thought that, check the result:

void OnTick(){
   CTrade trade;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
      int OrdersCount=0;
      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         OrdersCount++;Print("OrdersCount is ",OrdersCount,", symbol: ",SName);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){}
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){}
            //OrdersCount++;Print("OrdersCount is ",OrdersCount,", symbol: ",SName);
            }
         }
      }
   }

2021.07.20 19:53:30.657 ProfitLocker (AUDCAD,H4) OrdersCount is 1, symbol: XTIUSD

2021.07.20 19:53:30.657 ProfitLocker (AUDCAD,H4) OrdersCount is 2, symbol: XTIUSD

2021.07.20 19:53:30.657 ProfitLocker (AUDCAD,H4) OrdersCount is 3, symbol: XTIUSD

OrdersCount is 1, 2, 3... should have just 3, it's not?

---

Edit: I made this change into the whole code and this is stopping at 1.

David Diez  
Nikolaos Pantzos:

Move the variable reset upper.





Same,

2021.07.20 20:09:09.521 ProfitLocker (AUDCAD,H4) OrdersCount is 1, symbol: EURAUD

2021.07.20 20:09:09.521 ProfitLocker (AUDCAD,H4) OrdersCount is 2, symbol: USDCAD

2021.07.20 20:09:09.521 ProfitLocker (AUDCAD,H4) OrdersCount is 3, symbol: AUDCAD

---

Stopping at 1 into the whole code.

David Diez  

This:

void OnTick(){
   CTrade trade;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
      int OrdersCount=0;
      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         OrdersCount++;Print("OrdersCount is ",OrdersCount);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){}
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){}
            }
         }
      }
   }

And this:

void OnTick(){
   CTrade trade;int OrdersCount=0;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){}
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){}
            OrdersCount++;Print("OrdersCount is ",OrdersCount);
            }
         }
      }
   }

Are giving the same result (now they're 2):

2021.07.20 20:21:45.096 ProfitLocker (AUDCAD,H4) OrdersCount is 1

2021.07.20 20:21:45.096 ProfitLocker (AUDCAD,H4) OrdersCount is 2

---

The second solution would allow me to make some other calcs I think, but still doesn't remain as the total order count (2).

Maybe it would remain at the higher value if I set a backcounter loop in the whole tool, I will try this and edit after.

---

Edit: no way, now I'm thinking this is being stopped due to a 'zero divide'. This is the zero divide:

double GlobalTarget=NormalizeDouble(OrdersCount*(1/100)*Balance,2); // Still with correct Balance and OrdersCount value=1, this is throwing zero.
// Some other lines...
double Target=NormalizeDouble(TotalProfit/GlobalTarget,4); // Then there is a 'zero divide' into this line.

Idea?

rrocchi  
David Diez:

This:

And this:

Are giving the same result (now they're 2):

2021.07.20 20:21:45.096 ProfitLocker (AUDCAD,H4) OrdersCount is 1

2021.07.20 20:21:45.096 ProfitLocker (AUDCAD,H4) OrdersCount is 2

---

The second solution would allow me to make some other calcs I think, but still doesn't remain as the total order count (2).

Maybe it would remain at the higher value if I set a backcounter loop in the whole tool, I will try this and edit after.

---

Edit: no way, now I'm thinking this is being stopped due to a 'zero divide'. This is the zero divide:

Idea?



I wrote this code now on the forum, it should be correct and compile  fine (but I did not tested it)



#include <Trade\PositionInfo.mqh>

CPositionInfo           posicao;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int PositionsCounter(string choosenSymbol = "all")
{
    int count = 0;

    for (int i = 0; i < PositionsTotal() ; i++) {

        if(posicao.SelectByIndex(i)) {

            if(1
                    && ( (posicao.Symbol() == choosenSymbol && choosenSymbol != "all") || (choosenSymbol == "all") )
                    && ( (posicao.PositionType() == POSITION_TYPE_BUY) || (posicao.PositionType() == POSITION_TYPE_SELL) )
              ) {

                count++;
                Print("Current POSITION being counted now is number " + IntegerToString(count) + " [ the position Symbol is : " + posicao.Symbol() + " ] "  );
            }
        }
    }

    Print("Finished: TOTAL Positions counted = " + IntegerToString(count) );
    return(count);

}






int GetPositionsCount;

GetPositionsCount = PositionsCounter();  // it will count ALL opened position, from ANY symbol, BUY and SELL positions type.

GetPositionsCount = PositionsCounter("EURUSD"); //it will count just EURUSD opened positions, of types: BUY and SELL  

GetPositionsCount = PositionsCounter("CHFJPY"); //it will count just CHFJPY opened positions, of types: BUY and SELL  

GetPositionsCount = PositionsCounter(_Symbol);  //it will count just the positions of the CURRENT Symbol, which is on the current chart window.



Hope it helps you

rrocchi  
David Diez:

I wrote a simple code to test OrdersCount having (3) different trades opened:

Print result is this:

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: EURAUD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: USDCAD

2021.07.20 19:34:26.467 ProfitLocker (AUDCAD,H4) OrdersCount is 1 symbol: AUDCAD

---

It's getting the right symbols, but count them as single ones.

What I'm trying is to get OrdersCount=3;

---

Any help is welcome.


Orders and Positions are different things. What you are trying to do is to count POSITIONS (and not Orders). Correct Nomenclature is very important. 


Order is for execution, an execution Order, which has several types, and once an order is executed, it generates a POSITION. 

Positions can be type BUY and SELL, they exists until being closed by TP/SL or by another ORDER to close such Position. 


This concept is very important, to not say essencial, to figure out how to make a good algorithm, by knowing the purposes and relations between ORDERS and POSITIONS.

Feel free to ask any questions you want :)

rrocchi  
David Diez:

This:


Edit: no way, now I'm thinking this is being stopped due to a 'zero divide'. This is the zero divide:

double GlobalTarget=NormalizeDouble(OrdersCount*(1/100)*Balance,2); // Still with correct Balance and OrdersCount value=1, this is throwing zero.
// Some other lines...
double Target=NormalizeDouble(TotalProfit/GlobalTarget,4); // Then there is a 'zero divide' into this line.

Idea?


yes, 

if OrdersCount is zero, then GlobalTarget will be zero, and TotalProfit/Globaltarget will result a Zero Divide error. 

//You have to check its value before making the mathematics.. 

double GlobalTarget=NormalizeDouble(OrdersCount*(1/100)*Balance,2); 

// .... some other lines.. 

double Target= 0;

if (GlobalTarget > 0) {
   Target = NormalizeDouble(TotalProfit/GlobalTarget,4); 
}


Another suggestion, use the correct Symbol DIGITS to make the normalization, not a fixed value (like 4 as you used), because using a fixed value will cause erros on other symbols.. Unless you don't plan to use your code to anything else.. even if this is the case, as a good best practices, is better to program always the way it fits anything on the future.. it will save debugging time someday :) 


if (GlobalTarget > 0) {
   Target = NormalizeDouble(TotalProfit/GlobalTarget,_Digits); 
}
David Diez  
rrocchi:


Orders and Positions are different things. What you are trying to do is to count POSITIONS (and not Orders). Correct Nomenclature is very important. 


Order is for execution, an execution Order, which has several types, and once an order is executed, it generates a POSITION. 

Positions can be type BUY and SELL, they exists until being closed by TP/SL or by another ORDER to close such Position. 


This concept is very important, to not say essencial, to figure out how to make a good algorithm, by knowing the purposes and relations between ORDERS and POSITIONS.

Feel free to ask any questions you want :)

Got it, using PosCounter++ instead but having the same problem.

David Diez  
rrocchi:



I wrote this code now on the forum, it should be correct and compile  fine (but I did not tested it)






Hope it helps you

Finally the solution was too easy, just by using PositionsTotal() we get the total count.

But still have a problem cause I need to count positions matching some conditions and I can't get the total count but just an iteration.

Please look at the example:

void OnTick(){
   CTrade trade;
   for(int s=0;s<SymbolsTotal(true);s++){
      string SName=SymbolName(s,true);
      if(ManageAll==false){SName=_Symbol;}
      //+------------------------------------------ Internal loop ---!
      int ProfitLongs=0,ProfitShorts=0;
      double PositiveBuy=0,PositiveSell=0;
      for(int i=0;i<PositionsTotal();i++){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            double PP=PositionGetDouble(POSITION_PROFIT);
            double PO=PositionGetDouble(POSITION_PRICE_OPEN);
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
               if(PP>0&&PositionGetDouble(POSITION_SL)<PO){ProfitLongs++;PositiveBuy+=PP;} // Count total LONG profit orders and their profit sum.
               }
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
               if(PP>0&&PositionGetDouble(POSITION_SL)>PO){ProfitShorts++;PositiveSell+=PP;} // Count total SHORT profit orders and their profit sum.
               }
            Print("PosCounter is ",PositionsTotal()); // This is OK.
            Print("ProfitLongs are ",ProfitLongs,", PositiveBuy is ",PositiveBuy);
            Print("ProfitShorts are ",ProfitShorts,", PositiveSell is ",PositiveSell);
            }
         }
      }
   }

Having now 8 positions (6 longs, 2 shorts ), most of them running on profit. This is the print result:

2021.07.22 00:22:02.988 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.988 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 0, PositiveBuy is 0.0

2021.07.22 00:22:02.988 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 1, PositiveBuy is 8.73

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 0, PositiveBuy is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 1, PositiveSell is 6.76

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 0, PositiveBuy is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 1, PositiveBuy is 1.04

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 1, PositiveBuy is 0.92

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 1, PositiveBuy is 0.38

2021.07.22 00:22:02.989 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

2021.07.22 00:22:02.990 ProfitLocker_PositionsTotal (GBPUSD,H4) OrdersCount is 8

2021.07.22 00:22:02.990 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitLongs are 0, PositiveBuy is 0.0

2021.07.22 00:22:02.990 ProfitLocker_PositionsTotal (GBPUSD,H4) ProfitShorts are 0, PositiveSell is 0.0

---

Sometimes not counting, then profitsum=0, sometimes counting 1 independently with its current profit.

Actually the purpose is quite simple, just need this workng to set the PartialTarget from the profit side but also a GlobalTarget using the same criteria.

Reason: