Closing out half lots. - page 12

 
DomGilberto:
Ah yea - got it! I think I know how to do it it - back to playing now :)
 
int start()
   {
   if(IsNewCandle())
      {
      CheckForMaTrade();
      if(UseCandleTrail)AdjustTrail();
      }

   if(OpenOrdersThisPair(Symbol())>0)
      {
      if(UseMoveToBreakEven)MoveToBreakEven();
      if(UseTrailingStop)AdjustTrail();
      if(Use_MA_Trail)MA_Trail();
      CloseHalfOrder(); // If there is an order call this void - loop inside the void - "if(OrderType()!=OP_BUY)continue;"
      }
   }
Would that work? Would it check every tick from there? (CloseHalfOrder() )

UPDATE BELOW - it does work :)
 

Sorry - I should stop asking 101 questions before I try things out :P

Yea - that does work. I now have it working where-by it closes as soon as the tick comes in and the bid is >= the OrderClose price specified :D

Nice large step in the right direction! Now to put a clamp on stopping this from constantly closing halves every time price revisits that same price specified in the OrderClose function :)

(Got rid of that annoying OrderModify error 1 too!) Hmmm, I cannot think of the correct logic for it to then WAIT after the first target (1:1) has done its close order... Any idea's? The OrderTicket() number is the same... so it can't be that way? I'm stuck on that part now :) after I have this bit, it's cracked pretty much :)

Man I am so grateful for your help!

 
DomGilberto:


Man I am so grateful for your help!

You are welcome, I have no problem helping people who listen and are happy to try and help themselves first
 
unbelievable 1 2 3 4 5 6 7 8 9 10 11 12

what a huge topic it is

Closing out half lots.

hope you can explain someone else how to do the moment you know

101 questions... who wants to read them all...

For (Got rid of that annoying OrderModify error 1 too!)

this happens most of the time if you try modify the trade with setting it already has

 

Man it's so rewarding once you figure it out too! Obviously thanks to your input as well!

The one last bit I am stuck on is telling the loop to stop halving the open order at that OrderClose() exit price, continously? I honestly cannot figure out the logic? It is spot on with WHEN and WHAT price to partially close at. I just need to tell it to stop ONCE it has halved the order one time?

The problem is, is that once the OrderClose() has closed half off on the open position, it will still be called again and again because the "int start" is calling "CloseHalfOrder()" every time there is an "OP_BUY" open... Any thoughts?

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if(OrderType()==OP_BUY)
      {
      CloseHalfOrder();
      } 
} // ---// <<<<<<<<<<<<< I haven't bothered copying and pasting all of in start() functions. Just showing you how the "CloseHalfOrder" is being called...


//+--------------------------------------------------------------------------------------------------+
//| Close OP_BUY Half lots @ 1:1 Function                                                            |
//+--------------------------------------------------------------------------------------------------+
void CloseHalfOrder()
{   
   
   double minLot=MarketInfo(Symbol(),MODE_MINLOT);
   //Print("The minimum lots are: ",DoubleToStr(minLot,Digits));

   double lotStep=MarketInfo(Symbol(),MODE_LOTSTEP);
   //Print("The Lotstep is: ",DoubleToStr(lotStep,Digits));

   double half=MathFloor(OrderLots()/2/lotStep)*lotStep;
   Print("The Lots to close is: ",DoubleToStr(half,Digits));

for(int c=OrdersTotal()-1; c>=0; c--)
      {
      if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES)==true)
         {
         Print("Order Ticker Number = ",OrderTicket());
         Print("Order Lots Open = ",OrderLots());
         }
      else
          {
          Print("Order Select returned the error of ",GetLastError()); // Order Select does not seem to want to work?
          continue;
          }

      double FirstTarget_Buy=OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice())/2);
      Print("FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
      
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol()==Symbol())
            if(OrderLots()>minLot)
            {
            Print("Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
            int HalfCloseTicket = OrderTicket();
            
            if(OrderType()==OP_BUY && Bid>=FirstTarget_Buy+(Point/2)) // Doesn't seem to even go further than this as I do not get an error from OrderClose?
               {
               Print("Bid >= FirstTarget_Buy - Current Bid: ",DoubleToStr(Bid,Digits)," FirstTarget_Buy: ",DoubleToStr(FirstTarget_Buy,Digits));
               bool Close_Half_Order_Buy=OrderClose(HalfCloseTicket,half,Bid,3,CLR_NONE);
               if(Close_Half_Order_Buy!=TRUE)Print("Close_Half_Order_Buy Last Error = ",GetLastError());
               if(Close_Half_Order_Buy==True)Print("First Target Closed: ", OrderLots());
               }
            }
         if(Close_Half_Order_Buy==True)
         {
         MoveToBreakEven(); // This is a void.
         EMA_Exit(); // This is another void being called on the basis I can get this loop above to just close the OP_BUY once...
         }    
     }
}
 
deVries:
unbelievable 1 2 3 4 5 6 7 8 9 10 11 12

what a huge topic it is

Closing out half lots.

hope you can explain someone else how to do the moment you know

101 questions... who wants to read them all...

For (Got rid of that annoying OrderModify error 1 too!)

this happens most of the time if you try modify the trade with setting it already has


Mate I will be more than happy to write a thread explaining a simple and effective way to partially close an open order - I am stuck with just one last bit, and then it's working mint! I will collate it all in one concise topic / thread for others to use? (not sure how I'd go about doing this?)

See the last post above - I have nearly done it, just cannot figure out the logic to tell the loop to STOP closing halves at the same OrderClose() exit price continuously?

Thanks for the tip with regards to OrderModify error 1 too :)
 
DomGilberto:

Mate I will be more than happy to write a thread explaining a simple and effective way to partially close an open order - I am stuck with just one last bit, and then it's working mint! I will collate it all in one concise topic / thread for others to use? (not sure how I'd go about doing this?)

See the last post above - I have nearly done it, just cannot figure out the logic to tell the loop to STOP closing halves at the same OrderClose() exit price continuously?

Thanks for the tip with regards to OrderModify error 1 too :)

Why not first move your ticket to breakeven this is told you before and then close a part of the trade .... if OrderLots() >= .......
 
deVries:

Why not first move your ticket to breakeven this is told you before and then close a part of the trade .... if OrderLots() >= .......

Because of this? It doesn't matter if I move to break even first... The first obstacle is this part I cannot understand? Even if I move to break even first, then close half, this will call ANY open positions again and OrderClose() at the same price again by calling "CloseHalfOrder();"?

int start()
   {
    
   if(OrderType()==OP_BUY)
      {
      CloseHalfOrder();
      } 
      
   }
 
DomGilberto:

Because of this? It doesn't matter if I move to break even first... The first obstacle is this part I cannot understand? Even if I move to break even first, then close half, this will call ANY open positions again and OrderClose() at the same price again by calling "CloseHalfOrder();"?


You only move to BE and close half if the SL is not already at BE
Reason: