Closing out half lots. - page 19

 
Just thought it would only be fair to update this thread, as a lot of people helped me understand how to get this down and working! This is how I have it working sweet as a nut and closing at the right times. FirstTarget first, if that is true and has successfully closed, then the EMA close, and then the TwoRatio_Buy target...

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   static datetime partclosedonce;
   static datetime partclosedtwice;
   static datetime partclosedthird;
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);
   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
   double half_1st=MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
   double half_2nd=MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
   double Target_2 =MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/6);
   double TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/3);  

   
for(int c=OrdersTotal()-1; c>=0; c--)
    {
      if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         
          if(OrderOpenTime() != partclosedonce)  
           if(OrderType()==OP_BUY && Bid >= FirstTarget_Buy+(Point/2) && OrderLots()>minLot) 
              {
              bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half_1st,Bid,5,Blue);
              }
               
          if(Close_Half_Order_Buy==True && OrderOpenPrice() > OrderStopLoss())
              {
              MoveToBreakEven(); 
              }
              if(Close_Half_Order_Buy==True)
                 {
                 partclosedonce = OrderOpenTime();
                 } 
      
          if(partclosedonce == OrderOpenTime() && partclosedonce != partclosedtwice && OrderOpenTime() != partclosedtwice)
           if(Bid - OrderOpenPrice() > Point / 2. 
            && OrderType()==OP_BUY && EMA_Bar < EMA_MA && OrderLots()>minLot)
              {
              bool EMA_Buy_Close=OrderClose(OrderTicket(),half_2nd,Bid,5,CLR_NONE);
              if(EMA_Buy_Close==True)partclosedtwice = OrderOpenTime();
              }     
                 
          if(OrderOpenTime() != partclosedthird)
           if(OrderType()==OP_BUY && Bid >= TwoRatio_Buy+(Point/2) && OrderLots()>minLot)
              {
              bool Two_Ratio_Buy=OrderClose(OrderTicket(),Target_2,Bid,5,CLR_NONE);
              if(Two_Ratio_Buy==True)partclosedthird = OrderOpenTime();
              }     
        }
     }
}
Reason: