How to have an EA close a partial trade

 

Hi--

I've been working on an EA to manage my trades. The idea is to have 3 minis, micros or full contracts on and then, as different TPs are hit, close out one of 3 Lots declared in the inputs and then manage the BE and SL positions along the way. According to what I have found, I should simply be able to include a value for the OrderLots()--however, it always closes out all three contracts. Would anyone have any ideas for me to try? This one has my head swimming.

Here's some of my code:

... Close_Lots = Lots1;

...

if (OrderType() == OP_BUY && top_tp <= Bid)
if(OrderClose(OrderTicket(), Close_Lots, Bid, 5, Red ))

Thanks for anything you can provide.

 
jcbwrites:

Hi--

I've been working on an EA to manage my trades. The idea is to have 3 minis, micros or full contracts on and then, as different TPs are hit, close out one of 3 Lots declared in the inputs and then manage the BE and SL positions along the way. According to what I have found, I should simply be able to include a value for the OrderLots()--however, it always closes out all three contracts. Would anyone have any ideas for me to try? This one has my head swimming.

Here's some of my code:

... Close_Lots = Lots1;

...

if (OrderType() == OP_BUY && top_tp <= Bid)
if(OrderClose(OrderTicket(), Close_Lots, Bid, 5, Red ))

Thanks for anything you can provide.

If you use OrderLots() it will close the whole trade. If you want to close 1/3 of the opened lots you can use Close_Lots = NormalizeDouble(OrderLots()/3,1);

If your broker allows micro lots precision can be set to 2 insted of 1.

 
robofx.org wrote >>

If you use OrderLots() it will close the whole trade. If you want to close 1/3 of the opened lots you can use Close_Lots = NormalizeDouble(OrderLots()/3,1);

If your broker allows micro lots precision can be set to 2 insted of 1.

Thanks. I was wonderibng about whether or not to try normalizing--on the agenda for today. What I have is 3 inputs for the 3 lots as desired and they could be any combination of minis, micros or full contracts. The desire is to close out the first one then the next, etc. I'll give this a try and see what happens.

Thanks again for the help.

 
jcbwrites:

Thanks. I was wonderibng about whether or not to try normalizing--on the agenda for today. What I have is 3 inputs for the 3 lots as desired and they could be any combination of minis, micros or full contracts. The desire is to close out the first one then the next, etc. I'll give this a try and see what happens.

Thanks again for the help.

If you continue to experience problems show your code so we can help you better.

 
robofx.org wrote >>

If you continue to experience problems show your code so we can help you better.

OK robofx - thanks for the offer--here is the code and the tester log results. The input value for Close_Lots can be full, mini or micro but with a demo account I can only test it to 1 decimal place. Thanks again.

--------------Log Output-------------------

Alert: Stop hit--Closed Contract 425GBPUSD ticket no 1
close #1 buy 0.30 GBPUSD at 1.6570 at price 1.6579
tp=1.6582 hidden_sl=1.654
open #1 buy 0.30 GBPUSD at 1.6570 ok

----------------------------------------------

void Check_TP()
{
happy = false; //set the flag in case
int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
bool result = false;
if ( OrderSymbol()==Symbol() )
if (OrderType() == OP_BUY && hidden_tp <= Bid)
if(OrderClose(OrderTicket(), NormalizeDouble(Close_Lots, 1), Bid, 5, Red ))
{
happy = true;
if (ShowAlerts) Alert("Profit taken on ", OrderSymbol(), " ticket no ", OrderTicket());
}
else
{
int err=GetLastError();
if (ShowAlerts)
Print("TP Close of ", OrderSymbol(), " ticket no ", OrderTicket()," failed with error (",err,"): ",ErrorDescription(err));
}
if (OrderType() == OP_SELL && hidden_tp >= Ask)
if(OrderClose(OrderTicket(), NormalizeDouble(Close_Lots, 1), Ask, 5, Red ))
{
happy = true;
if (ShowAlerts) Alert("Profit taken on ", OrderSymbol(), " ticket no ", OrderTicket());
}
else
{
int err2=GetLastError();
if (ShowAlerts)
Print("TP Close of ", OrderSymbol(), " ticket no ", OrderTicket()," failed with error (",err2,"): ",ErrorDescription(err));
}
}
return;
}//End of Check_TP Function