Closing out half lots. - page 18

 


Ok (UPDATE!) so basically, I have got rid of the continuous closing of "half of a half of a half etc." on the lots when the second OrderClose() is called (where the all blue if statement is - apparently this works ok!) - HOWEVER, I do NOT want this to be called AT ALL unless the FIRST OrderClose() function is "True" and was successful. "Close_Half_Order_Buy==True" does not work either...

Would love someone's help to finish this :)

( I have removed a lot of the "Prints", for the sake of clarity on the meat of the code.)

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   static datetime partclosedonce;
   static datetime partclosedtwice; 
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);

   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);

   double half=MathFloor(OrderLots()/2/lotStep)*lotStep;
   
   double EMA_Bar = iClose(NULL, PERIOD_H1, 1);
   double EMA_MA = iMA(Symbol(),60,21,0,1,0,0);

   
for(int c=OrdersTotal()-1; c>=0; c--)
      {
      if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES)==true)

      double FirstTarget_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/2);
      	Print("FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
      
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderLots()>minLot)
               if(OrderOpenTime() != partclosedonce)
            {
            
            if(OrderType()==OP_BUY && Bid >= FirstTarget_Buy+(Point/2)) 
               {
               bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half,Bid,5,Blue);
               if(Close_Half_Order_Buy!=TRUE)Print("Close_Half_Order_Buy Last Error = ",GetLastError());
               }
            }
         
      if(Close_Half_Order_Buy==True && OrderOpenPrice() > OrderStopLoss())
         {
         MoveToBreakEven(); 
         }
          if(Close_Half_Order_Buy==True)
               {
               partclosedonce = OrderOpenTime();
               } 
      
      if(partclosedonce != partclosedtwice && OrderOpenTime() != partclosedtwice) // I dont want this to run UNLESS the first OrderClose() is successful.
         {
          if(OrderType()==OP_BUY && EMA_Bar < EMA_MA)
               {
               bool EMA_Buy_Close=OrderClose(OrderTicket(),half,Bid,5,CLR_NONE);
               if(EMA_Buy_Close!=TRUE)Print("EMA_Buy_Close Last Error = ",GetLastError());
               if(EMA_Buy_Close==True)partclosedtwice = OrderOpenTime();
               }     
          }        
     }
}


	          
 
Anyone?
 
DomGilberto:


Ok (UPDATE!) so basically, I have got rid of the continuous closing of "half of a half of a half etc." on the lots when the second OrderClose() is called (where the all blue if statement is - apparently this works ok!) - HOWEVER, I do NOT want this to be called AT ALL unless the FIRST OrderClose() function is "True" and was successful. "Close_Half_Order_Buy==True" does not work either...

Would love someone's help to finish this :)

The following code compiles (by declaring the MagicNumber variable and commenting out the MoveToBreakEven() function) but was not tested.

//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder() {
   static bool FirstBuyTarget_hit = false, SecondBuyTarget_hit = false;
   int ct = 0;
   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);
        
   for(int c = OrdersTotal() - 1; c >= 0; c--)
      if (OrderSelect(c,SELECT_BY_POS))
         if (OrderSymbol() == Symbol() && OrderType() <= OP_SELL)
            if (OrderMagicNumber() == MagicNumber) {
               double half=MathFloor(OrderLots()/2/lotStep)*lotStep;
               double FirstTarget_Buy = OrderOpenPrice() + ((OrderTakeProfit() - OrderOpenPrice()) / 2);
               Print ("FirstTarget_Buy: ", DoubleToStr(FirstTarget_Buy,Digits));
                                        
               ct++;
                                        
               if (!FirstBuyTarget_hit)
                  if (OrderType() == OP_BUY && Bid >= FirstTarget_Buy + (Point/2)) {
                     if (OrderClose(OrderTicket(),half,Bid,5,Blue)) {
                        MoveToBreakEven();
                        FirstBuyTarget_hit = true;
                        break;
                     }
                     else
                        Print("Close_Half_Order_Buy Last Error = ", GetLastError());
                  }
                                        
               if (FirstBuyTarget_hit && !SecondBuyTarget_hit) 
                  if (OrderType() == OP_BUY && EMA_Bar < EMA_MA) {
                     if (OrderClose(OrderTicket(),half,Bid,5,CLR_NONE))
                        SecondBuyTarget_hit = true;
                     else
                        Print("EMA_Buy_Close Last Error = ",GetLastError());                    
                  }

               break;
            }
   if (ct == 0) {
      FirstBuyTarget_hit = false;
      SecondBuyTarget_hit = false;
   }
}

The above code assumes that only one order is placed per currency pair.

 

Omg thank you so much for your help! I really do appreciate it :)

Would you mind helping me with one last thing; at the moment I have a FIXED 1:2 take profit target (hard TP). I actually want to remove this out of the order. However, as you can see above, the relevant OrderClose() functions are using the OrderTakeProfit() to calculate where the OrderClose() functions should close at.

My question is, is there a way to store the OrderTakeProfit() price, but instead of having it fixed in the order, I want to close out a set amount of lots using OrderClose() @ the OrderTakeProfit() price? Reason why is because sometimes I'll have a trade that will run for huge gains, but because I have OrderTakeProfit() fixed with the open order, all the lots will close out there and I only want to partially close some lots at that exit price. I use a trailing stop for the big runners... Hope that makes sense?

That's it! I am then done and dusted! Finally!

UPDATE: The code above is a nice way of writing it. I've had a little play, but it's not quite closing in the correct order. It still just closes half on what ever comes first. It is kind of the same as what I had before using static datetime? Any thoughts?

 
(feeling like I've exhausted this thread)

I literally have those last 2 questions above and this is done? All I need to know is how can I store the OrderTakeProfit() price, so that when I modify it, I can still use that exact price to do an OrderClose()? Will a Static Double work?
 
DomGilberto:

UPDATE: The code above is a nice way of writing it. I've had a little play, but it's not quite closing in the correct order. It still just closes half on what ever comes first. It is kind of the same as what I had before using static datetime? Any thoughts?

Post any changes you've made to the above code and post log/journal entries (or screen shots).

 
DomGilberto:

Omg thank you so much for your help! I really do appreciate it :)

Would you mind helping me with one last thing; at the moment I have a FIXED 1:2 take profit target (hard TP). I actually want to remove this out of the order. However, as you can see above, the relevant OrderClose() functions are using the OrderTakeProfit() to calculate where the OrderClose() functions should close at.

My question is, is there a way to store the OrderTakeProfit() price, but instead of having it fixed in the order, I want to close out a set amount of lots using OrderClose() @ the OrderTakeProfit() price? Reason why is because sometimes I'll have a trade that will run for huge gains, but because I have OrderTakeProfit() fixed with the open order, all the lots will close out there and I only want to partially close some lots at that exit price. I use a trailing stop for the big runners... Hope that makes sense?

That's it! I am then done and dusted! Finally!

UPDATE: The code above is a nice way of writing it. I've had a little play, but it's not quite closing in the correct order. It still just closes half on what ever comes first. It is kind of the same as what I had before using static datetime? Any thoughts?


set your takeprofit at a huge level and you can still use your relevant OrderClose() functions that are using the OrderTakeProfit() to calculate where the OrderClose() functions should close at. only make a change on the calculation where the OrderClose() functions should close at
 
deVries - literally just figured that out this morning... Started putting another modify in... then realised I should just change it right from when the first order is placed...

Thirteen - I have not made any changes to it at all. It kind of worked the same way as the code I posted, but you're using a static bool as opposed to a static datetime? That's the only real difference I could see when I ran the back-test? Other than that, the second OrderClose() function with the moving average and the close below it; it still closes if that comes first before the other orderclose()?
 
DomGilberto:

Thirteen - I have not made any changes to it at all. It kind of worked the same way as the code I posted, but you're using a static bool as opposed to a static datetime? That's the only real difference I could see when I ran the back-test? Other than that, the second OrderClose() function with the moving average and the close below it; it still closes if that comes first before the other orderclose()?
With your use of the question mark, I'm not sure if you are asking me or telling me the highlighted portion. If the tester shows that the second partial close occurred before the first, please post your trade results from the log/journal that were generated by the strategy tester showing that the second partial close (the moving avg) occurred before the first partial close.
 
Thirteen:
With your use of the question mark, I'm not sure if you are asking me or telling me the highlighted portion. If the tester shows that the second partial close occurred before the first, please post your trade results from the log/journal that were generated by the strategy tester showing that the second partial close (the moving avg) occurred before the first partial close.

Thanks for getting back to me Thirteen - I will post up the log later today :)