MQL4 - automated forex trading   /  

Forum

Login | Register | Language: ru cn en
Read Financial Magazines in MetaTrader 4 and MetaTrader 5!

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);

}


}

 
Betterment's bad news play at getting in good with RIAs and investors
Brooke’s Note: Given the nosebleed pace of advancement by software-based advisors, most methods of inserting sunshine into portfolios have been snapped up. Now one robo-firm is peeling off from the crowd by taking the opposite tack — delivering more bad news more quickly. “If it bleeds it leads” has long been a maxim in the new business; now one VC-fueled automated advisor will give you truth that you can — and should — handle.
avatar
9809
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