MQL4 - automated forex trading   /  

Forum

Login | Register | Language: ru cn en
Freelancer services needed to perform 150+ jobs of traders

closing trades with unprofitable result

Back to topics list To post a new topic, please log in or register
avatar
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);

}


}

 
MQL5.com Freelance: Developers' Source of Income (Infographic)

MQL5.com Freelance: Developers' Source of Income (Infographic)

On the occasion of the MQL5 Freelance Service fourth birthday, we have prepared an info-graphic demonstrating the service results for the entire time of its existence. The figures speak for themselves: more than 10 000 orders worth about $600,000 in total have been executed to date, while 3 000 customers and 300 developers have already used the service.

avatar
9466
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.
    //...
}


 
avatar
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?
 
Back to topics list  

To add comments, please log in or register