Download MetaTrader 5

OrderClose rare multiple close?

To add comments, please log in or register
Ask your broker to open the official channel. Thousands of traders will read the news!
Dominic Gilbert
1639
Dominic Gilbert 2013.11.28 09:33 
I know I've been pretty incessant with regards to OrderClose() function and multiple closes, but it does something quite strange.

Usually what would happen at target 1, 2 or 3 is the open positions predefined lot value will be closed out. However, what I have seen (maybe ONCE) is when price >= target 1, it will close out the correct lots it should do, but instead it will do it within 3 exits. So if I check my history, instead of having one part close of 0.03, i'll have 3x 0.01 closes at >= target 1.

Any thoughts would be greatly appreciated - (also, I HAVE read regarding RefreshRates(), but am not entirely sure on the placement of them? Is there a better way of using them than the example shown in the manual of RefreshRates?)
//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   static datetime Closed_FirstTarget;
   datetime        FetchDateOfFirstClose = GlobalVariableGet(" EURAUD_GV_1st ");
   
   static datetime Closed_EMA_Target;
   datetime        FetchDateOfEMAClose = GlobalVariableGet(" EURAUD_GV_EMA ");
   
   static datetime Closed_ThirdTarget;
   datetime        FetchDateOfThirdClose = GlobalVariableGet(" EURAUD_GV_3rd ");
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);
   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);
   
   int PositionIndex;    //  <-- this variable is the index used for the loop
   int TotalNumberOfOrders;   //  <-- this variable will hold the number of orders currently in the Trade pool
   TotalNumberOfOrders = OrdersTotal();    // <-- we store the number of Orders in the variable
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     bool CurrentOrder = OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES);
      if(  ! CurrentOrder ) continue;  
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         if( CurrentOrder == True)  
            {
               double Target_2 =MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;
               TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / ( RewardRatio / 2 )); 
            }          


          //<<-- Second Target Order Close function -->>\\ 
          if(OrderOpenTime() != FetchDateOfThirdClose && OrderType()==OP_BUY && Bid - TwoRatio_Buy > - Point )
              {
                if( Target_2 - minLot > - Point )
                  
                  {  
                  bool Two_Ratio_Buy=OrderClose(OrderTicket(),Target_2,Bid,5,CLR_NONE);
                  if(Two_Ratio_Buy!=TRUE)Print("Two_Ratio_Buy Last Error = ",GetLastError(), " On: ", OrderSymbol());
                  if(Two_Ratio_Buy==True)Print("Two_Ratio_Buy Target Closed: ", OrderLots(), " On: ", OrderSymbol());
                  if(Two_Ratio_Buy==True)Closed_ThirdTarget = GlobalVariableSet(" EURAUD_GV_3rd ", OrderOpenTime());
                     Alert("Second Target Hit At: ", TwoRatio_Buy," on: ", Symbol());  
                  }
                
                if( minLot - Target_2 > - Point )
                  {
                  bool Two_Ratio_Buy1=OrderClose(OrderTicket(),OrderLots(),Bid,5,CLR_NONE);
                  if(Two_Ratio_Buy1!=TRUE)Print("Two_Ratio_Buy Last Error = ",GetLastError(), " On: ", OrderSymbol());
                  if(Two_Ratio_Buy1==True)Print("Two_Ratio_Buy Target Closed: ", OrderLots(), " On: ", OrderSymbol());
                  if(Two_Ratio_Buy1==True)Closed_ThirdTarget = GlobalVariableSet(" EURAUD_GV_3rd ", OrderOpenTime());
                     Alert("Could only close minLot at Second Target: ", TwoRatio_Buy," on: ", Symbol());               
                  } 

...
To add comments, please log in or register