# OrderProfit + Loss & CloseAllTrades doesn't work

227

Hi Everyone I've been stuck on something for over a week.  No matter what I try, I cannot get this to work.  I want my EA to simply add the profit and loss of two open trades together, and if the combined profit and loss exceeds a certain profit margin, it must close both trades.  But it always closes both trades in a loss.  It is as if it closes the first trade in a loss as soon as the other trade goes in profit, but then fails to close the profitable trade until it goes into a loss.  It always prints dLoss=0.  So it is obviously not calculating the losses properly.  Is this because OrderProfit() does not take losses into account?  What is the opposite function for OrderProfit()?  The best code I have so far is this one.  Will someone please look and see if you notice any problems.  Thank you very much.

```void CloseAllinProfit(){
int total=0,result;
double dProfit=0,dLoss=0;
for(int i=OrdersTotal()-1; i>=0; i--){
if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==Symbol()){
dProfit += OrderProfit() + OrderCommission() + OrderSwap();
if(dProfit>0) dProfit++;
if(dProfit<0) dLoss++;
Print("dProfit ",dProfit);
Print("dLoss ",dLoss);
Print("OrderProfit ",OrderProfit());
}
}
if(total>=2){
if(dProfit+dLoss>=Profit)
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
return;
}
//+------------------------------------------------------------------------+```
15086

1. Your posted code does nothing, as total is initialized to zero and not updated. Always post the real code.
2. Why are you incrementing dProfit, when it's the sum of orders' profits.
3. What is the meaning of \$Balance Change + number of losses (dProfit + dLoss)
4. You've exited the OrderSelect loop, you can't call OrderClose with those parameters.
5. Why do you always close an order?
```   if(dProfit+dLoss>=Profit) result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );```
6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
227

WHRoeder:
1. Your posted code does nothing, as total is initialized to zero and not updated. Always post the real code.
2. Why are you incrementing dProfit, when it's the sum of orders' profits.
3. What is the meaning of \$Balance Change + number of losses (dProfit + dLoss)
4. You've exited the OrderSelect loop, you can't call OrderClose with those parameters.
5. Why do you always close an order?
6. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Wow, lots of mistakes/problems.  Thank you very much for your help.  I will work on these issues.