Download MetaTrader 5

Problems with an EA taking partial profits

To add comments, please log in or register
mar
1146
mar 2013.12.27 16:06 

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 |
//|                                               Copyright 2013, MR |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MR"
#property link      ""

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 (OrderType()==OP_BUY)
            {
               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);
}
//+------------------------------------------------------------------+
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.27 16:22  
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 ?
mar
1146
mar 2013.12.27 16:49  

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..

Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2013.12.27 19:33  
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 . . .
To add comments, please log in or register