Close part of Trade and Trailingstop

 

Hi guys,

for example my EA opened a long position with 0.2 Lots and it should close 0.1 at a higher level. At this moment i want the EA to set the StopLoss at BE.

Do i have to open 2 positions each 0.1?

How can i manage it?

 
FamWue:

Hi guys,

for example my EA opened a long position with 0.2 Lots and it should close 0.1 at a higher level. At this moment i want the EA to set the StopLoss at BE.

Do i have to open 2 positions each 0.1?

How can i manage it?

You can use OrderClose() to close part of a position,  just specify the lot size . . .  it's the second parameter in the OrderClose() call
 
FamWue:

Hi guys,

for example my EA opened a long position with 0.2 Lots and it should close 0.1 at a higher level. At this moment i want the EA to set the StopLoss at BE.

Do i have to open 2 positions each 0.1?     No Not Needed

How can i manage it?


extern double Lots1 = 0.1;
extern double Lots2 = 0.1;

double Lots;

int start(){
Lots = Lots1 + Lots2;


You open your trade with Lots

and partially close first part Lots1 when OrderLots() > Lots2  

or you can say also when  OrderLots() > Lots - Lots1

 
FamWue: Do i have to open 2 positions each 0.1? i want the EA to set the StopLoss at BE.
  1. Test for SL at BE. (Already did partial close.) Set SL at BE. Do a partial close (from my code.)
  2. Also remember the remaining order gets a new ticket number.
//+------------------------------------------------------------------+
//| Partial order close.                                             |
//+------------------------------------------------------------------+
#define INF 0x6FFFFFFF // Not quite infinite, Jul 2029, or 1,879,048,191
bool     CloseOrder(int ticket=EMPTY, double size=INF){     // INF == entire.
   if(ticket == EMPTY)  ticket = OrderTicket();
   if(GetTradeContext() < TC_LOCKED)                           return(false);
   // https://www.mql5.com/en/forum/139345 Refresh rates (GTC) then update OCP.
   if(!MySelect(ticket, SELECT_BY_TICKET)){
      Alert("OrderSelect(",ticket,",ticket) failed: ", GetLastError());
                                                               return(false); }
   double   minLot      = MarketInfo(symbol.chart, MODE_MINLOT),
            lotStep     = MarketInfo(symbol.chart, MODE_LOTSTEP),
            sizeCurr    = OrderLots(),
            sizeClose   = MathRound(size/lotStep)*lotStep,
            sizeRem     = sizeCurr - sizeClose;
   if(sizeClose < minLot)                                      return(false);
   if(sizeRem   < minLot){ sizeClose = sizeCurr;      // Close all
      color op.color = IfI(color.Buy, color.Sell);                            }
   else     op.color = Aqua;
   SetNeedToRefresh();
   if(OrderClose( ticket, sizeClose, OrderClosePrice(),
               Slippage.Pips*pips2points, op.color  ))         return(true);
   Alert("OrderClose(ticket=", ticket, ", ...) [1] failed: ", GetLastError());
                                                               return(false);
}  // CloseOrder
bool     MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
   if(!OrderSelect(iWhat, eSelect, ePool) )  return (false);
   if(OrderMagicNumber() != magic.number  )  return (false);
   if(OrderSymbol()      != symbol.chart  )  return (false);
   if(ePool != MODE_HISTORY               )  return (true);
   return(OrderType() <= OP_SELL);  // Avoid cr/bal https://www.mql5.com/en/forum/126192
                                    // https://www.mql5.com/en/forum/124726
                                    // Never select canceled orders.
}