How to count number of opend positions?

 

Hi guys.

I create another cause this subject is not same. Tell me if i have to upgrade the another.

So, I want to know how count the number of opened positions like OrderSend is excuted, count=1, if a second same OrderSend is exeuted, count=2, etc. And how to write:

if( count==X)

   Stop open position

I create MagicNumber for select if necessary.

I know how create a "Boucle For" but I don't know  how to write all of that.

Many thanks for your time passed to answer of my questions.


Kane.

 
double GetInformation(int parameter)
{
        int     countOrdersBuy  = 0;
        int     countOrdersSell = 0;
        int     total = OrdersTotal();
        for(int i=total-1; i>=0; i--)
        {
                if(OrderSelect(i, SELECT_BY_POS))
                {
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                        {
                                if(OrderType()==OP_BUY )
                                {
                                        switch(parameter)
                                        {
                                                case  0: countOrdersBuy++ ;   break;
                                                case  1: countOrdersBuy++ ;   break;
                                        }
                                }
                                if(OrderType()==OP_SELL)
                                {
                                        switch(parameter)
                                        {
                                                case  0: countOrdersSell++;   break;
                                                case -1: countOrdersSell++;   break;
                                        }
                                }
                        }
                }
        }
        switch(parameter)
        {
                case  0: return(countOrdersBuy  + countOrdersSell);
                case  1: return(countOrdersBuy );
                case -1: return(countOrdersSell);
        }
}

 just check the example (part from my own EA) above. parameter 1 is Long Postion count, -1 Short Postion, and 0 for summe.

(sorry for poor english, hope the code helps u) 

 
Please
Mihawk:

...

... 

 just check the example (part from my own EA) above. parameter 1 is Long Postion count, -1 Short Postion, and 0 for summe.

(sorry for poor english, hope the code help u) 

Please look for edit link on top right of your comment and edit your code using SRC button.

 

 

Hi Mihawk


thanks for the code i will look that tomorrow. No problem for your poor english, mine is poor too :) So after i can write:

if (countOrdersBuy != X)
   Trade allowed

Cause countorder will be increase by countOrderBuy ++?

Good night :)

 
phi.nuts:
Please

Please look for edit link on top right of your comment and edit your code using SRC button.

 


thanks for the advice! Edited
 
Kane59:

Hi Mihawk


thanks for the code i will look that tomorrow. No problem for your poor english, mine is poor too :) So after i can write:

Cause countorder will be increase by countOrderBuy ++?

Good night :)


you can use my code as follow

if (GetInformation( 1) < X) // for Long Position, or ( -1) for Short, ( 0 )  for all if hedge allowed
   Trade allowed
 
Mihawk:

 just check the example (part from my own EA) above. parameter 1 is Long Postion count, -1 Short Postion, and 0 for summe.

double GetInformation(int parameter)
{
:
   case  0: return(countOrdersBuy  + countOrdersSell);
   case  1: return(countOrdersBuy );
   case -1: return(countOrdersSell);
You should use explanatory variable names and self documenting code. A variable named "parameter" says nothing. Your routine doesn't get any information, it get the open order count. Better:
#define GOOC_BUY   1
#define GOOC_SELL -1
#define GOOC_BOTH  0
double GetOpenOrderCount(int parameter)
{
:
                case GOOC_BOTH: return(countOrdersBuy  + countOrdersSell);
                case GOOC_BUY:  return(countOrdersBuy );
                case GOOC_SELL: return(countOrdersSell);
///
int nBuy = GetOpenOrderCount(GOOC_BUY);
Still it can't be used for pending orders. Best:
extern int      Magic.Number                    = 20121113;
bool    MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
    if (!OrderSelect(iWhat, eSelect, ePool)    ) return (false);
    if (OrderMagicNumber() != Magic.Number     ) return (false);
    if (OrderSymbol()      != chart.symbol     ) return (false);
    if (ePool != MODE_HISTORY                  ) return (true);
    return(OrderType() <= OP_SELL); // Avoid cr/bal forum.mql4.com/32363#325360
                                    // https://forum.mql4.com/30708
                                    // Never select canceled orders.
}
int MyOrdersTotal(int orderType=-1, int ePool=MODE_TRADES){ #define OP_ALL -1
    if (ePool == MODE_TRADES)   int iPos = OrdersTotal()        - 1;
    else                            iPos = OrdersHistoryTotal() - 1;
    for(int nOrders=0; iPos >= 0; iPos--)
      if (MySelect(iPos, SELECT_BY_POS, ePool)) 
        if (ordertype == OP_ALL || orderType == OrderType()) nOrders++;
    return(nOrders);
}
///
int nBuys = MyOrdersTotal(OP_BUY),
    nSell = MyOrdersTotal(OP_SELL),
    nAll  = MyOrderTotal(),
    nHistory = MyOrderTotal(OP_ALL, MODE_HISTORY);
Use of the magic number and pair filtering is done once in MySelect, not in every orderSelect loop. MyOrdersTotal (does what OrdersTotal() does with filtering My) can select All types and can be used for both open orders and history.
 

Thanks you guys for your replies. There is so many code wich i have to study :)

Thanks for informations :)

 
WHRoeder, i have to add you example code with the Miawk'code or it's independent?
 
Study the code and you will know.
 
WHRoeder:
You should use explanatory variable names and self documenting code. A variable named "parameter" says nothing. Your routine doesn't get any information, it get the open order count. Better:Still it can't be used for pending orders. Best:Use of the magic number and pair filtering is done once in MySelect, not in every orderSelect loop. MyOrdersTotal (does what OrdersTotal() does with filtering My) can select All types and can be used for both open orders and history.

WHRoeder:
You should use explanatory variable names and self documenting code. A variable named "parameter" says nothing. Your routine doesn't get any information, it get the open order count. Better:Still it can't be used for pending orders. Best:Use of the magic number and pair filtering is done once in MySelect, not in every orderSelect loop. MyOrdersTotal (does what OrdersTotal() does with filtering My) can select All types and can be used for both open orders and history.

this is really good! I've learnt many things from your code, thanks!