| / | Forum |
|
MarketForce
2008.03.12 20:04
Hello all, I'm coding a hedging strategy where I enter a trade on a symbol and place an opposite direction trade on the hedge symbol. When the sum of the profits of the two orders are >= a set profit amount in dollars, I want to close both orders. My problem is that the expert seems to close both orders as soon as one of the the orders hits the desired profit level and not when the sum of the two orders is at the desired profit level. Below is a sample of the code, can anyone help me figure out what I'm doing wrong? Keep in mind that more often than not, one order will have a positive profit and the other one will have a negative profit. The base symbol is given MagicNumber 1 and the hedge trade is given MagicNumber 2. I have included a Comment instruction that shows the ProfitTotal and it shows the correct value, but for some reason the expert seems to only look at the individual order profits when deciding to close both orders. Thanks in advance for any help!!! // MONITOR OVERALL PROFIT AND CLOSE ORDERS WHEN SET PROFIT IS REACHED -- int total = OrdersTotal(); for (i = 0; i < total; i++) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); // If only the base symbol trade was closed after profit was reached // close the hedge order as well if (total==1 && OrderMagicNumber()==2) { if (OrderType()==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(Hedge,MODE_BID),3,Aqua); Sleep(5000); RefreshRates(); } if (OrderType()==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(Hedge,MODE_ASK),3,Aqua); Sleep(5000); RefreshRates(); } } if (OrderMagicNumber()==1) { double Profit1=OrderProfit(); } if (OrderMagicNumber()==2) { double Profit2=OrderProfit(); } double ProfitTotal=(Profit1)+(Profit2); // Add profit of two orders together, then compare to SecureProfit if (ProfitTotal>=SecureProfit) // If profit is met, close both orders { if (OrderMagicNumber()==1) { if (OrderType()==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Aqua); Sleep(5000); RefreshRates(); } if (OrderType()==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),Ask,3,Aqua); Sleep(5000); RefreshRates(); } } if (OrderMagicNumber()==2) { if (OrderType()==OP_BUY) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(Hedge,MODE_BID),3,Aqua); Sleep(5000); RefreshRates(); } if (OrderType()==OP_SELL) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(Hedge,MODE_ASK),3,Aqua); Sleep(5000); RefreshRates(); } } } } |
|
Secrets of MetaTrader 4 Client Terminal: Alerting System How to be aware of what happens in the terminal and on your account without permanent looking at the monitor. System events; custom events; wave and executable files; electronic messages; setting up SMTP server access; publications; setting up FTP server access. |
|
phy
2008.03.12 20:53
Looks like... Each time through your loop, Profit1 and Profit2 are set to 0 when they are declared. One of them gets set to the profit of a ticket. So, the test if ( ProfitTotal >= SecureProfit ) is not doing what you want. ProfitTotal is equal to Profit1 +0 or it equals 0 + Profit2 at the time of the test. |
|
MarketForce
2008.03.13 19:49
phy wrote:
Looks like... Each time through your loop, Profit1 and Profit2 are set to 0 when they are declared. One of them gets set to the profit of a ticket. So, the test if ( ProfitTotal >= SecureProfit ) is not doing what you want. ProfitTotal is equal to Profit1 +0 or it equals 0 + Profit2 at the time of the test.
What you're saying makes total sense, I just can't figure out how to correct it. I'm thinking that maybe I need to add the profits together in a loop, then check to see if the ProfitTotal has been met, then close the orders in another loop. If you have any suggestions, please let me know! Thanks |
|
phy
2008.03.13 20:37
"I'm thinking that maybe I need to add the profits together in a loop, then check to see if the ProfitTotal has been met, then close the orders in another loop." That sounds reasonable... |