Lots and OrderClose

 

I am trying to add to an EA when price reaches a TakeProfit level then reduce the open position to half lot size which then goes to a trailing stop.

I figured I would check to see if the current open order is the same lot size as originally defined in the EA and if price is greater than the TakeProfit level. If so then reduce the position by half. If the original lot size is not equal to the current position lot size then it would not reduce again.

The problem is that when I check the lot size in the current open order it always returns zero (I'm on IBFX if that matters). Can anyone please spot what I’m doing wrong?

Thank you

  if(OrdersTotal()>0 && OrderType() == OP_BUY)
   {
      OrderSelect(ticket,SELECT_BY_POS);
      if(OrderLots()==Lots && Bid-OrderOpenPrice()>=TakeProfit*Point*10)
      {
         half=Lots/2.0;
         OrderClose(OrderTicket(),half,Bid,3,DarkViolet);
      }
   }   
   if(OrdersTotal()>0 && OrderType() == OP_SELL)
   {
      OrderSelect(ticket,SELECT_BY_POS);
      if(OrderLots()==Lots && OrderOpenPrice()-Ask>=TakeProfit*Point*10)
      {
         half=Lots/2.0;
         OrderClose(OrderTicket(),half,Bid,3,DarkViolet);
      }
   } 
 
if(OrdersTotal()>0 && OrderType() == OP_BUY)
   {
      OrderSelect(ticket,SELECT_BY_POS); <------------------- change this to if(OrderSelect(ticket,SELECT_BY_POS))..... (1)

      if(OrderLots()==Lots && Bid-OrderOpenPrice()>=TakeProfit*Point*10)
      {
         half=Lots/2.0; 
         OrderClose(OrderTicket(),half,Bid,3,DarkViolet); <--- normalize the lot size to correct significance, See below qn.... (2)

      }
   }   
   if(OrdersTotal()>0 && OrderType() == OP_SELL)
   {
      OrderSelect(ticket,SELECT_BY_POS); <------------------- as above (1)
 
      if(OrderLots()==Lots && OrderOpenPrice()-Ask>=TakeProfit*Point*10)
      {
         half=Lots/2.0; <------------------- as above (2). See below qn.

         OrderClose(OrderTicket(),half,Bid,3,DarkViolet); <----- you close on the Ask, not Bid
      }
   } 

Qn: What data type is vrb 'half' did you use?
 

Still not enough..... When price reach TakeProfit level the whole order will be closed Then orderlots are zero

I think you mean a first profit level to close half position

What a bad solution in your code We can trade more pairs the same time .....

So more open trades the same time that EA is gonna mess other trades that way

look to all open trades and select then the one you manage

         total=OrdersTotal();
         for(cnt=0;cnt<total;cnt++)
           {
            OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
            if( OrderSymbol()!=Symbol()||OrderMagicNumber()!=Magic1
              ||OrderType()!=OP_BUY&&OrderType()!=OP_SELL) continue;
            .................

now stryker combine the goods and you have the code

 
diostar:
half=Lots/2.0; 
OrderClose(OrderTicket(),half,Bid,3,DarkViolet); <--- normalize the lot size to correct significance
Normalizing lot size assumes lot step is a multiple of 1, 0.1, 0.01, and fails for any other. Do it right.
double  minLot  = MarketInfo(Symbol(), MODE_MINLOT),
        lotStep = MarketInfo(Symbol(), MODE_LOTSTEP),
        half    = MathFloor(Lots/2/lotStep)*lotStep;
if (half < minLot) half = Lots; // Can't partial close below minimum.
:
 
WHRoeder:
Normalizing lot size assumes lot step is a multiple of 1, 0.1, 0.01, and fails for any other. Do it right.

No, you have normalize to the to the significance of min_lot. eg. if min lot is 0.05 or 0.01, then norm to 2 digits, if 0.1, to 1 digit, etc.

Looks like you misunderstood what he wants - he want it actually Lots/2 to close half the original lotsize.

 
diostar:

No, you have normalize to the to the significance of min_lot. eg. if min lot is 0.05 or 0.01, then norm to 2 digits, if 0.1, to 1 digit, etc.

Looks like you misunderstood what he wants - he want it actually Lots/2 to close half the original lotsize.

I misunderstood nothing, but did have a typo:

half    = MathFloor(Lots/2/lotStep)*lotStep;

IBFX for example use to have minlot of 0.1 and a lotstep of 0.01. Meaning lots can be 0.10, 0.11, 0.12... normalizing to the minlot means you can ONLY have 0.10, 0.20, etc. If the order was initially 0.33 lots then half MUST be 0.16 and the remainder 0.17. Not half=0.10 and remainder 0.23

If lot step was 0.05 then you can only have 0.05, 0.10, 0.15... But if you normalize half to 2 places you get 0.01, 0.02... which are NOT valid steps and the modify will FAIL.

You must make sure half is still valid (>=minlot AND a multiple of lot step) and the remainder is also still valid (>= minlot)

 

I was able to get it together from everyone's help. I would have never figured out the MathFloor/lotstep part -- many thanks!