# Problems with an EA taking partial profits

1148

Hi,

this EA should close 50% of the position when TP1 is reached and modify the order by placing the stoploss to breakeven and setting TP2 as the take profit for the other 50%.

Unfortunately it doesn't work at all... Maybe somebody could help?

```//+------------------------------------------------------------------+
//|                                             e-PartialProfits.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+

extern double TP1 = 1;
extern double TP2 = 2;
bool STP_modified = false;
bool TP1_closed = false;

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

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
static datetime Time0;
if (Time0 == Time[0]) return(0);
Time0 = Time[0];

for (int i = OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() == Symbol())
{
{
if (Bid>=TP1)
{
if (!TP1_closed) TP1_closed = OrderClose(OrderTicket(), NormalizeDouble((OrderLots()/2),2), Bid, 3, CLR_NONE);
if (!STP_modified) STP_modified = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TP2,0);
if (!TP1_closed || !STP_modified) Time0=EMPTY_VALUE;
else Time0=Time[0];
}
}
else if (OrderType()==OP_SELL)
{
if (Bid<=TP1)
{
if (!TP1_closed) TP1_closed = OrderClose(OrderTicket(), NormalizeDouble((OrderLots()/2),2), Ask, 3, CLR_NONE);
if (!STP_modified) STP_modified = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TP2,0);
if (!TP1_closed || !STP_modified) Time0=EMPTY_VALUE;
else Time0=Time[0];
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+```
Moderator
18021

mar:

Hi,

this EA should close 50% of the position when TP1 is reached and modify the order by placing the stoploss to breakeven and setting TP2 as the take profit for the other 50%.

Unfortunately it doesn't work at all... Maybe somebody could help?

What error do you get ? ah, you aren't checking . . . What are Function return values ? How do I use them ?
1148

Interesting article, but I think I check the return values...?

```if (Bid>=TP1)
{
if (!TP1_closed) TP1_closed = OrderClose(OrderTicket(), NormalizeDouble((OrderLots()/2),2), Bid, 3, CLR_NONE);
if (!STP_modified) STP_modified = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),TP2,0);
if (!TP1_closed || !STP_modified) Time0=EMPTY_VALUE;
else Time0=Time[0];
}  ```

TP1_closed is a bool which is true, when TP1 is reached and half of the position is closed. STP_modified is true if the order was modified. In case that one of these variables is false, I set Time0 to EMPTY_VALUE so the EA will do everything again at the next tick and doesn't wait for the next bar.

So I am not sure, which possible errors you think about..

Moderator
18021

mar:

Interesting article, but I think I check the return values...?

Nope . . . nor do you report the error if the return value tells you that the OrderClose() or the OrderModify() failed . . . nor do you Print() any of the variables that would let you diagnose the error . . .