# Problem with calculating collective back to back losses.

15

Hi

I am trying to write a code which will give me a sum total of back to back losses including commissions.

I am able to retrieve the last loss and commissions but have no clue as to how to retrieve the sum total of losses when it happens in a row.

Here's what I am talking about. This is the list of my profit loss as an example.

Profit , Loss , Loss2 , Profit2, Loss3, Loss4, Loss5.

I am able to retrieve the details of Loss5.

However, I am looking for a sum total of Loss3+Loss4+Loss5.

```double SumTotal()
{//1
for(int i = (OrdersHistoryTotal()-1); i >= 0; i --)
{ //2
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if((OrderMagicNumber()==iMagicNumber)&&(OrderSymbol()==Symbol()))
{  //3
{//4
double commission =  MathAbs ( OrderCommission() );
double profitloss =  MathAbs ( OrderProfit() );
double achieve = commission + profitloss;
Print("Commission for the order is            ",commission);
Print("Profit for the order is                ",profitloss);
Print("achieve for the order is               ",achieve);
return(achieve);

}//4
else {return(0);}
} //3

} //2
return(0);```

7863

```double SumTotal()
{
double achieve = 0;
for(int i = OrdersHistoryTotal()-1; i >= 0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))    continue;
if(OrderMagicNumber()!=iMagicNumber)                continue;
if(OrderSymbol()!=Symbol())                         continue;
if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue;

double commission = OrderCommission(),
profitloss = OrderProfit(),
swap       = OrderSwap(),
orderProfit= commission + profitloss + swap;
achieve += orderProfit;
Print("Commission for the order is            ",commission);
Print("Swap for the order is                  ",swap);
Print("Profit for the order is                ",profitloss);
Print("OrderProfit for the order is           ",orderProfit);
}
return(achieve);
}```
15

Konstantin Nikitin:

Thank You for your help Nikitin.

But it still is not counting the whole thing. It is only giving out the last loss.

7863

deedeed :

Thank You for your help Nikitin.

But it still is not counting the whole thing. It is only giving out the last loss.

Summarize all profits
`achieve += orderProfit;`

And come back

`return(achieve);`
3023

deedeed:

Hi

I am trying to write a code which will give me a sum total of back to back losses including commissions.

I am able to retrieve the last loss and commissions but have no clue as to how to retrieve the sum total of losses when it happens in a row.

Here's what I am talking about. This is the list of my profit loss as an example.

Profit , Loss , Loss2 , Profit2, Loss3, Loss4, Loss5.

I am able to retrieve the details of Loss5.

However, I am looking for a sum total of Loss3+Loss4+Loss5.

Try this:

```double SumTotal(const int MN)
{
double achieve=0.0;
for(int i=(OrdersHistoryTotal()-1); i>=0; i --)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
{
printf("OrderSelect failed with error #%d",GetLastError());
continue;
}
if(OrderMagicNumber()==MN && OrderSymbol()==_Symbol && (OrderType()==OP_BUY || OrderType()==OP_SELL))
{
printf("Magic number: %d",MN);
double profitloss=OrderProfit();
double commission=OrderCommission();
double swap=OrderSwap();
double sum=profitloss+commission+swap;
if(sum<0.0)
{
Print("Profit for the order is                ",profitloss);
Print("Commission for the order is            ",commission);
Print("Swap for the order is                  ",swap);
achieve+=sum;
}
else return(achieve);
}
}
return(achieve);
}```

If the last order is profit then it returns zero otherwise it returns sum of negative profits in the row.

15

Konstantin Nikitin:
Summarize all profits

And come back

When there are two or more consecutive losses.. it is only picking up the last loss. Just tested it. Should I send you the screenshots?

Regards

7863

```double SumTotal(const int t=0)
{
// t = 0 all orders, t <0 losing orders, t> 0 profitable orders
double achieve = 0;
for(int i = OrdersHistoryTotal()-1; i >= 0; i--)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))    continue;
if(OrderMagicNumber()!=MagicNumber)                 continue;
if(OrderSymbol()!=Symbol())                         continue;
if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue;

double commission = OrderCommission(),
profitloss = OrderProfit(),
swap       = OrderSwap(),
orderProfit= commission + profitloss + swap;
Print("Commission for the order is            ",commission);
Print("Swap for the order is                  ",swap);
Print("Profit for the order is                ",profitloss);
Print("OrderProfit for the order is           ",orderProfit);

achieve += (t==0 ? orderProfit : (t>0 && orderProfit>0) ? orderProfit : (t<0 && orderProfit<0) ? orderProfit : 0);
}
return(achieve);
}```
20210

deedeed: When there are two or more consecutive losses.. it is only picking up the last loss.
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum