MQL4 - automated forex trading   /  

Forum

Login | Register | Language: ru cn en
Update to MetaTrader 4 build 735 and Test the VPS for Free

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

}


}

 
Identify morale problems in your data science team with the help of sentiment analysis
Sentiment analysis attempts to gauge the mood of a group based on their online conversations with each other. This is a good way to learn valuable information about your data scientists' morale. How is your team's morale? You might turn to your change professionals for this information; however, even change experts tend to rely more on impressions and intuition than data and analyses. What a missed opportunity if you have data scientists on your team!
avatar
9763
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