# Count how many lost orders from the last profit order

49

Hi, I wonder if someone can help me. I need to calculate total lost orders from my order history, since the last profit order closed. I can count the orders but i am struggling with the equation of selecting it from the last profit order. Below is my script. Please can someone help out?

```int TotalLostOrders()
{
#define ASCENDING -1
int total=0;
int hstTotal = OrdersHistoryTotal();
for(int i=0; i < hstTotal; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
if(OrderMagicNumber()== Magic && OrderProfit() < 0)

total++;

}
else Print(__FUNCTION__,"Failed to select order",GetLastError());
}
return (total);
```
20210

SmartSarb: need to calculate total lost orders from my order history, since the last profit order closed.
1. `for(int i=0; i < hstTotal; i++)`
You are processing the earliest orders not the latest. You count all looser but you don't reset the count at a winner. Or you can process the latest and stop counting when you find the latest winner.

2. `OrderProfit() < 0`
Profit = OrderProfit + commission + swap.

3. `if(OrderMagicNumber()== Magic`
You are processing orders from any chart, not the current one.

4. There are other things in history besides closed orders.

5. You assume history is ordered by date, it's not.
2392

whroeder1:
1. You are processing the earliest orders not the latest. You count all looser but you don't reset the count at a winner. Or you can process the latest and stop counting when you find the latest winner.

2. Profit = OrderProfit + commission + swap.

3. You are processing orders from any chart, not the current one.

4. There are other things in history besides closed orders.

5. You assume history is ordered by date, it's not.

Points #1 and #5 are contradictory.

20210

nicholishen: Points #1 and #5 are contradictory.

It is mostly ordered ascending. Therefor the code is mostly "processing the earliest orders." Therefor #1 and #5 are not mostly "contradictory."

2392

SmartSarb:

Hi, I wonder if someone can help me. I need to calculate total lost orders from my order history, since the last profit order closed. I can count the orders but i am struggling with the equation of selecting it from the last profit order. Below is my script. Please can someone help out?

Like whroeder said, you need to sort your orders programmatically before you analyse them. Here is a class to do just that

```void OnStart()
{
//---
MQL4OrderPool pool;
pool.Init(  Symbol(),
0,             // magic
MODE_HISTORY,  // mode
ORDERS_FILLED, // filter by order type filled = OP_BUY+OP_SELL
SORT_CLOSE_TIME_DESCENDING);// default sort mode
int losing_orders = 0;
for(int i=0;i<pool.Total();i++)
{
if(pool[i].OrderProfit() < 0)
losing_orders++;
else
break;
}
// or you can also use
losing_orders=0;
for(MQL4Order *ord = pool.GetFirstNode();ord!=NULL;ord = pool.GetNextNode())
{
if(ord.OrderProfit() < 0)
losing_orders++;
else
break;
}
}```

so that you can count losing orders.

or if you're feeling lazy you can just copy/paste this function...

```int LosingOrdersSinceProfitOrder(int magic)
{
MQL4OrderPool pool;
pool.Init(Symbol(),magic,MODE_HISTORY,ORDERS_FILLED,SORT_CLOSE_TIME_DESCENDING);
int losing = 0;
if(pool.Total() < 2)
return losing;
if(pool[0].OrderProfit() > 0)
{
for(int i=1;i<pool.Total();i++)
{
if(pool[i].OrderProfit() < 0)
losing_orders++;
else
break;
}
}
return losing;
}```
Files:
49

nicholishen:

Like whroeder said, you need to sort your orders programmatically before you analyse them. Here is a class to do just that

so that you can count losing orders.

or if you're feeling lazy you can just copy/paste this function...

Thanks alot guys. You are superb

28

nicholi shen:

Like whroeder said, you need to sort your orders programmatically before you analyse them. Here is a class to do just that

so that you can count losing orders.

or if you're feeling lazy you can just copy/paste this function...

Thank you so much