Closing Partial Orders/Lots

 
Hi all,

I'm working on my first mq4 expert, and have run into a stumbling block.

I want my expert to close half of the lots when the first take profit goal is reached, and then close the other half at a later take profit or trailing stop. Problem is I can't figure out how to keep track of which orders have taken their half profit.

  for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol - make sure order is for current chart
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(MacdCurrent<MacdPrevious || Upper != NULL)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
                //create array to check if 1st PT reached.  use cnt or better ticket number as id
                else if(PRICE_CLOSE>OrderOpenPrice()+TakeProfit*Point) {
                   if(OrderClose(OrderTicket(),OrderLots()/2,Bid,3,Violet) == true) { // close half position
                     //tp1[OrderTicket()] = true;
                   }
                   return(0); //exit
                }
                
                
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
I was thinking about using some sort of an array to keep track, but it doesn't look like MQL has dictionaries/hashes.

Thanks for your help!
 
Why not just open two lots of half size each and then close one and let the other run. Remember to use different magic numbers for each lot for easy tracking.

Good luck.
 
how do you use different magic numbers for the same EA? i thought magic numbers were predeclared at the start of the script. also, i tried opening 2 lots and the problem with that is slippage, my second position often misses by a few pips, not cool
 
I just created 2 variables to hold the magic numbers:

int Magic1 = 5908;
int Magic2 = 3243;

Then you just substitute the variables in your order code.

I'm not sure about slippage with this technique. I use Stop and Limit orders. . .
 
slippage when entering the trade, i wrote a quick script for entering 2 trades real time, and they can differ anywhere from 2-4 pips depending on market speed.
Reason: