# Thread: How to count all open trades for specific pair with magicnumber?

1. ## How to count all open trades for specific pair with magicnumber?

Hi Funyoo,

Can you teach me the code if I want to count all open trades for a specific pair with specific Magic number?

Thanks much
Jidonk

2. void CloseAllOrders()
{

bo=0;so=0;
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
if (OrderSymbol()==Symbol() && OrderMagicNumber()== Reference)
{
bo++;

if (OrderType()==OP_SELL )

so++;

}

}
}
}

3. ## To COUNT not to Close

Hi Perky_Z,

Thanks for your help. But what I need is to COUNT all open trade not to Close all trades.

I'm using:

AllMyOrder = OrdersTotal();

but it seems to count all open trades regardless the the pair/magic.

Thanks

4. Originally Posted by jidonk
Hi Perky_Z,

Thanks for your help. But what I need is to COUNT all open trade not to Close all trades.

I'm using:

AllMyOrder = OrdersTotal();

but it seems to count all open trades regardless the the pair/magic.

Thanks
CloseAllOrders() is just the name that the function has been called (it's probably been cut'n'pasted from elsewhere), rather than what it actually does. You can rename it if you wish.

The function will return a count of both open buy (this is the value bo) and open sell (this is the value so) orders.

5. Thanks Jezzer,

Will try...

Rgds

6. No problem. but bear in mind that, because it's been set out as a function as it has, you will need to call it somehow. Unless there's a need for it to be set out in that way, you may as well scrap the name together with the first left brace and the last right brace.

7. Jezzer,

I just can't make it works....

Below is my code...

on top:

extern int Magic = 123;
extern string SpecificPairs="EURUSD";
extern double Lots = 0.1;
extern bool IncreaseLot = TRUE;

then I put this:

int order_total = 0;
order_total = OrdersTotal();
if (IncreaseLot) {
if (order_total == 0) addlot = 0.0;
if (order_total == 1) addlot = 0.1;
if (order_total == 2) addlot = 0.2;

then on OrderSend function I put Lots + addlot as the volume
myticket = OrderSend(Symbol(), OP_SELL, Lots + addlot, Bid, 2,....etc

Now, how can I change the code to works only with specific pair and magic?

Thanks

8. Originally Posted by jidonk
Jezzer,

Now, how can I change the code to works only with specific pair and magic?

Thanks
You aren't selecting any orders with your code. You need to follow Perky's instructions.

Code:
```int order_total=0;
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if ((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderSymbol()==Symbol() && OrderMagicNumber()== Magic)
{order_total++;}

}```
That will return a count for order_total. You then just continue with the rest of your code.

9. Awesome......problem solved!

Thank you very much Jezzer

Rgds

10. ## Adding position after some floating loss

Hi Jezzer,

Hopefully you don't mind teaching me another one:

for the EA to open another position ONLY when the previous position(s) are in a floating loss condition? I'm referring to dollar value of the loss, not based on pip step. (and still based on the specific pairs and magic)

For example,