English

## closing trades with unprofitable result

 2 schuhcreme 2009.11.26 02:44   hi, I programed a loop, which counts the profits and only if all trades together are profitable, they will be closed . I used OrderClosePrice() for OrderClose, but I tried Ask and Bid, nothing lets the EA make profits. Curiously sometimes all trades are closed with profits, but sometimes with losses and if I test the OrderProfit() calculation with Print(count) the result is positive, but the backtest-reports are negative. the EA have to close all trades. but the end result have to be positive I also used 0 slippage, but this does not help. The amount of trades is between 20 and 300. I tried only backtests. here is the code : ```double count=0; for(int i=0;i<=OrdersTotal();i++) { OrderSelect(i,SELECT_BY_POS,MODE_TRADES); count=count+ ( OrderProfit()+OrderSwap() +OrderCommission()) ; } if(count>0) { for(int i2=0;i2<=OrdersTotal();i2++) { OrderSelect(i2,SELECT_BY_POS,MODE_TRADES); Print(count); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Red); } } ``` similar
 12983 WHRoeder 2009.11.26 16:12 #   schuhcreme: I programed a loop, which counts the profits and only if all trades together are profitable, they will be closed . ... here is the code : ```for(int i2=0;i2<=OrdersTotal();i2++) { OrderSelect(i2,SELECT_BY_POS,MODE_TRADES); Print(count); OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Red); } ``` Once you close position zero, position one becomes zero so you skip ever other trade. Always count down. Always check for your magic number so you're not bothering other EAs or manual trades. Always check the symbol in case your EA is on multiple charts and you forgot to change the magic number. ```for(int openOrder = OrdersTotal() - 1; openOrder >= 0; openOrder--) if (     OrderSelect(openOrder, SELECT_BY_POS) &&  OrderMagicNumber() == MagicNumber // with my magic number, &&  OrderSymbol() == Symbol() ) { // in my chart.     //... } ``` similar
 2 schuhcreme 2009.11.26 17:44 #   I checked the amount of opened trades and the amount of closed trades. It is the same, no active trade exisits after closing all positions.. I think, the problem is the calculation of the Orderprofit ```OrderProfit()+OrderSwap() +OrderCommission() Even if I use a ticket array e.g. tickets[x]=SendOrder .... and close with a loop, which counts all array elements ... I think the calculation of orderprofits has another price then the Orderclose-function. But I don´t know why, slippage is 0 If I buy , I have to use Ask If i close this position I have to use Bid for Orderclose. Is that correct? ``` similar