MQL4 check please for closing half trade

 

Will this EA monitor open trades for the chart and close half the trade on reaching a certain target profit and move the stop loss to plus 10 pips of the opening price?

Have I missed anything in the code?

How could I stop it acting on the half closed order the next time the tick runs?

//+------------------------------------------------------------------+
//| Close half trade and move SL to +10.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property show_inputs

extern double SL = 100; //move to plus 10 pips / generally use the ATR value from the chart
extern double TP = 500; //50pips or otherwise specified or ATR value from the chart

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

//+------------------------------------------------------------------+
//| Find open positions |
//+------------------------------------------------------------------+

//----

double halfLot;

for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY && Ask-OrderOpenPrice() >= TP) {
halfLot = MathRound(OrderLots()/2,2);
OrderClose(OrderTicket(),halfLot,Ask,NULL,Green);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SL,OrderTakeProfit(),0,Green);
}
if(OrderType()==OP_SELL && OrderOpenPrice()-Bid >= TP) {
halfLot = MathRound(OrderLots()/2,2);
OrderClose(OrderTicket(),halfLot,Ask,NULL,Red);
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SL,OrderTakeProfit(),0,Red);
}
}
}

//----
//+------------------------------------------------------------------+

}




 
Buy orders should be closed by Bid.
Replace
to
OrderOpenPrice()+SL*Point
and
TP to TP*Point
 
SanMiguel:

What would be a way to stop it from closing out half of the next trade?

For example, I have alot of 0.10 open. It reaches a target profit of 50pips, so the code closes half the trade and resets the stop loss to +10 of openprice.

So, we now have a lot of 0.05. I want the code to leave this alone until I either close it out manually or it hits a stop loss.

 
Replace
if(OrderType()==OP_BUY && Ask-OrderOpenPrice() >= TP) 
to
if(OrderType()==OP_BUY && Ask-OrderOpenPrice() >= TP&& OrderLots()>0.05) 
 

Err not all the lots would be 0.05 or less with this method.

I might have a lot open at 0.07 cut down to 0.04 or

one at 0.16 cut down to 0.08 or

one open at 0.5 cut down to 0.25

 
The problem is not with the closed half, but the other half. Once you close the first, how do you know not to close the second half again.
  1. Do not set an initial TP. Once you close half, set the TP for the second.
  2. Open two orders each for half size. Set one with TP, one without.
  3. Use two different magic numbers
Reason: