Partial Close EA

 

I am trying to write an EA that will close a portion of the trade at a predetermined price (or pip) level, and allow the remainder of the original trade to carry on to the next target and the remainder to the next and final target: I have the code written up but it doesn't close the portion of the trade out when it should. I am changing the lot size with the OrderClose function, does anyone know if this is correct? I've also tried OrderModify but I think that is only to reposition stoploss and takeprofit. Any help woud be appreciated.

 
YenTrader2:
I am trying to write an EA that will close a portion of the trade at a predetermined price (or pip) level, and allow the remainder of the original trade to carry on to the next target and the remainder to the next and final target: I have the code written up but it doesn't close the portion of the trade out when it should. I am changing the lot size with the OrderClose function, does anyone know if this is correct? I've also tried OrderModify but I think that is only to reposition stoploss and takeprofit. Any help woud be appreciated.

there are number of ways to do this .. fastest would be to define a variable that holds the single position you want to close and then check with the total lots open .. then in the OrderClose just close this lot everytime the conditions are met and adjust the other lots.

-guyver

 

That's pretty much what I've done using a percentage of OrderLots() function. My code compiles with no errors and when it runs there are no error messages in the journal, but it is not closing any trades. This is something that I scoured the internet to try and find, and nothing, am I missing something? There are numerous threads with simple buy and sell scriots, I think this would be pretty valuble.

void CheckForClose()

{

double lots,perclots1,perclots2,perclots3;

//---- go trading only for first tiks of new bar

if(Volume[0]>1) return;

//---- get Percentage of Lots

if(OrderLots()>0.0) return;

perclots1 = ((Percentage_To_Close1/100)*OrderLots());

perclots2 = ((Percentage_To_Close2/100)*OrderLots());

perclots3 = ((Percentage_To_Close3/100)*OrderLots());

//----

for(int i=0;i<OrdersTotal();i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)

break;

if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol())

continue;

//---- check order type for 1st Profit Target

if(OrderType()==OP_BUY)

{

if(Ask > (First_Profit_Target*Point) && Ask < ((First_Profit_Target*Point)+(2*Point)))

OrderClose(OrderTicket(),perclots1,Bid,3,Blue);

break;

}

if(OrderType()==OP_SELL)

{

if(Bid ((First_Profit_Target*Point)-(2*Point)))

OrderClose(OrderTicket(),perclots1,Ask,3,Red);

break;

}

}

//----

}

 

// See my reply below -- lets see if this solves the problem .. i'll check back later

-guyver

YenTrader2:
That's pretty much what I've done using a percentage of OrderLots() function. My code compiles with no errors and when it runs there are no error messages in the journal, but it is not closing any trades. This is something that I scoured the internet to try and find, and nothing, am I missing something? There are numerous threads with simple buy and sell scriots, I think this would be pretty valuble.

void CheckForClose()

{

make sure the code knows the size of " point " as well

double lots,perclots1,perclots2,perclots3;

//---- go trading only for first tiks of new bar

if(Volume[0]>1) return; // If you really need it

//---- get Percentage of Lots

if(OrderLots()>0.0) return; ??..

perclots1 = ((Percentage_To_Close1/100)*OrderLots());

// -- correct this -- from above to -- perclots1 = NormalizeDouble(OrderLots() * Percentage_To_Close1/100,c); // where c could be 1 for normal and 2 for micro

perclots2 = ((Percentage_To_Close2/100)*OrderLots());

perclots3 = ((Percentage_To_Close3/100)*OrderLots());

//----

for(int i=0;i<OrdersTotal();i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)

break;

if(OrderMagicNumber()!=MagicNumber || OrderSymbol()!=Symbol())

continue;

//---- check order type for 1st Profit Target

if(OrderType()==OP_BUY)

{

if(Ask > (First_Profit_Target*Point) && Ask < ((First_Profit_Target*Point)+(2*Point)))

OrderClose(OrderTicket(),perclots1,Bid,3,Blue);

you need to compare this with open price

break;

}

if(OrderType()==OP_SELL)

{

if(Bid ((First_Profit_Target*Point)-(2*Point)))

OrderClose(OrderTicket(),perclots1,Ask,3,Red);

break;

}

}

//----

}
 

I appreciate your help, but I think the problem lies in the fact that I'm using a variable with the OrderClose Function, I'm sure you can close a percentage of the lot size depending on what "triggers" the close function. If in the case of a long position, if the market ticks down and back up it would keep closing the partials. I'm not sure if my thought is coherent, but I'm using a price to close the position, and the price could keep bouncing and hitting the target.

I have a new thought to accomplish the same goal of multiple orders with multiple tp's. Have the EA divide the lot size and close each portion of the lot at a tp level.

As a side note isn't this how a hidden SL or TP is accomplished? Use the order close function to close the trade at a price point rather than have them set in the order function as a tp or sl. But it would have to be an EA rather than a script so it would keep running to monitor the price?.

 
YenTrader2:
I appreciate your help, but I think the problem lies in the fact that I'm using a variable with the OrderClose Function, I'm sure you can close a percentage of the lot size depending on what "triggers" the close function. If in the case of a long position, if the market ticks down and back up it would keep closing the partials. I'm not sure if my thought is coherent, but I'm using a price to close the position, and the price could keep bouncing and hitting the target.

I have a new thought to accomplish the same goal of multiple orders with multiple tp's. Have the EA divide the lot size and close each portion of the lot at a tp level.

As a side note isn't this how a hidden SL or TP is accomplished? Use the order close function to close the trade at a price point rather than have them set in the order function as a tp or sl. But it would have to be an EA rather than a script so it would keep running to monitor the price?.

First , the code of yours can work if you do some necessary changes additionaly i thought you would add /extend this to add more level of tp's. Anyhow for your second question yes EA cause it needs to monitor prices

-guyver

 

MA Cross with Partail Close

I hope everyone is doing well. I know there are many MA cross EA's, however, I can't find one with a Partail close option. Does any know of a basic MA cross EA with a partial close option? Thanks!

 

...

Partial close is the same as regular, but you have to add a code that would look something like this :
if (OrderLot!=OrderLots())

{

OrderLot = MathMax(minLot,OrderLot);

if (OrderLots() < 2.0*minLot)

OrderLot = OrderLots();

}

where OrderLot is the part you wish to close and minLot equals MarketInfo(Symbol(),MODE_MINLOT). The check if the current lot size is less than 2 times minimal lot isze is necessary since if it is less than it you would be left with a part that is less than the minimal lot size and it could cause errors

SuperSix:
I hope everyone is doing well. I know there are many MA cross EA's, however, I can't find one with a Partail close option. Does any know of a basic MA cross EA with a partial close option? Thanks!
 

Some brokers don't allow partial close.

Check if you can close a part of the position manually before trying with EA.

 

I think that it has to do with a FIFO rule (since with partial closing you can potentially avoid FIFO rule). So the brokers that have the FIFO rule probably will not allow partial closing either

 
void PartialClose() { double Lotstep = MarketInfo(Symbol(), MODE_LOTSTEP); int x = 0; if(Lotstep == 0.01) x = 2; else x = 1; for(int i = OrdersTotal()-1; i >= 0; i--){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false)continue; if( Magic == OrderMagicNumber() ) {int Tk=10;double LOTS = OrderLots();double Dividelot = NormalizeDouble(LOTS * 0.5, x); if(OrderType() == OP_BUY){ if(Bid > OrderOpenPrice()+(Tk*Point)){ OrderClose(OrderTicket(), Dividelot, Bid, 3, Red); } } if(OrderType() == OP_SELL){ if(Ask< OrderOpenPrice()-(Tk*Point)){ OrderClose(OrderTicket(),Dividelot, Ask, 3, Red); } } } }}

can any body correct me this code , it still continues to divide the order