10 000 traders are already selling signals and programs on the Market
MQL4.com is moving to MQL5.com. Sign up for the new web portal for 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);

}


}

 
Next Targets In USD/JPY, EUR/USD - SocGen
Japanese flow data show a steady acceleration in buying of foreign bonds and equities by Japanese investors, which has not overtaken foreign buying of Japanese assets, notes SocGen. "This is a source of comfort for yen bears, but the overnight move in USD/JPY has broken the recent range and exposed a clear risk of a flush out of yen shorts. USD/JPY 117 is a potential target," SocGen projects. For the EUR, SocGen notes the recent improvement in Eurozone data. "A straw in the wind, a swallow signalling economic summer or just a sign that things aren’t getting any worse?
avatar
10485
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