Partial OrderClose()

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
7
nunonuk 2013.04.22 11:05 #
 

Hi Thirteen,


Your code seems to work, but much more resource consuming than the one I have created.

I think that the last part is not correct, please correct me, if I am wrong, I think it should be like this:


for (j = 0; j < ArrayRange(before_close, 0); j++) {
    if (before_close[j] != OrderTicket())
        new_order = false;
    else
        { new_order = true; break; }
}
if (new_order)
    return (OrderTicket());


Anyway, I got i better solution, from a fellow from another forum, which is an optimized version of the first one I create, here it is:


int getNewTicketNumber(double openPrice, datetime openTime) 
{    
    for(i=1; i<=OrdersTotal(); i++)       
    {          
        if ( OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES) == true )          
        {             
           if ( OrderOpenTime() == openTime && OrderOpenPrice()== openPrice )              
                return(OrderTicket());                                       
        }       
     }    
      return(-1);  // if no ticket has been found 
}


DeVries, my system open trandes like this:

         // Caso tenha subido
         if ( SOMETHING )
         {
            ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-stopLoss*Point,Ask+takeProfitFinal*Point,"...", 12345, 0, Green);
            fase[ticket] = 0;
         }
      
         // Caso tenha descido
         if ( SOMETHING )
         {
            ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+stopLoss*Point,Bid-takeProfitFinal*Point,"...", 12345, 0, Red);
            fase[ticket] = 0;
         }

For the lot size, for the testing moment I am using fixed lot size, but I already have a system to auto choose the lot size dynamically.

I change the Stoploss level before the partial close in order to secure my position, and after the partial close I have a trailing system that updates the Stoploss everytime the diference is incremented by 5pips/50points.

avatar
3849
ubzen 2013.04.22 13:15 #
 
nunonuk: if ( OrderOpenTime() == openTime && OrderOpenPrice()== openPrice )

What if there's another order with matching open_time && open_price, how would it know the difference?

DeVries asked the what question because the complexity of what you need depends upon the system's implementation and environment. Example: will there be other ea's trading? Would you also be trading manually? Do you need to link partial_closes to the originals? Do you open orders based upon the # of partial_tickets opened?

Now, I'm no expert on partial_closing and I don't employ partial_closing within my experts. The codes provided by others are usually samples and may-not be customized to fit all your needs.

If you believe you'll never have another order with matching time&&price, then use your function. As usual, its upto the programmer and their comfort level. I taught about the ticket# first because I believed it was unique.

avatar
2408
deVries 2013.04.22 14:21 #
 

the moment you have to startup again your pc you loose value openPrice and openTime

how do you want to use your solution in that case 

you have to find all the trades of your EA by selecting MagicNumber  and Symbol

if you find a trade in the loop of your EA you can check if the partial close has be done already

with fixed lots you can say 

if OrderLots() + LotStep <= Lots  you know the partial close is done 

or you have to check the stoplosslevel if you don't work with fixed lots 

in this loop count down you can close partial and you can manage the trailingstop

I think that i don't care about ticketnumber 

 

avatar
13009
WHRoeder 2013.04.22 14:51 #
 
Assuming you're not using a fixed lot size, modify the SL from initial (below break even to BE or above) and then do the partial close. The next time your orderSelect loop will find the new ticket and you will know that you already did a partial close.
avatar
3849
ubzen 2013.04.22 15:49 #
 
WHRoeder:
Assuming you're not using a fixed lot size, modify the SL from initial (below break even to BE or above) and then do the partial close. The next time your orderSelect loop will find the new ticket and you will know that you already did a partial close.

I see some obvious problems with this solution.

  1. The OrderModify() could work but OrderClose() fail.
  2. His OrderStopLoss() is now being used as Stamp.
  3. Gives no indication of the Parent Order.

In the end, the best solution comes down to a file_write IMO, mostly because GlobalVariableSet cannot handle arrays efficiently. 

1> Even if he goes through an extensive error_handling and cannot close the order, he'll need to result to saving this information to disk. file_write || globalvariableset.

2> He may need to re-adjust his OrderStopLoss(), this assumes that he wants to partial close only when the order is profitable. What happens to strategies that partial_close as position loses. Along the lines of this solution, I taught, why not just tell him to set the OrderTakeProfit() to very_high value like 999999999 || Point(for sell positions).

3> How does a strategy which checks for details from the original position get this information? Example: Say he's 3 partial_closes deep. What was the original OrderOpenPrice()?

avatar
352
GreenMoney 2013.04.22 19:45 #
 

nunonuk:

Hi Thirteen,

... 

I think that the last part is not correct, please correct me, if I am wrong, I think it should be like this:

for (j = 0; j < ArrayRange(before_close, 0); j++) {
    if (before_close[j] != OrderTicket())
        new_order = false;
    else
        { new_order = true; break; }
}
if (new_order)
    return (OrderTicket());

 ...


No...if it finds a matching ticket number, than that ticket number cannot be the new ticket for the market order that was opened as a result of the OrderClose().  Therefore, to be the new ticket number which resulted from the OrderClose() function, new_order must equal true (no match) for all iterations of the nearest For loop.  Once the loop ends, new_order is evaluated to determine if a match was made.  That part of my code (reproduced below for clarification) is correct, as I intended:

for (j = 0; j < ArrayRange(before_close, 0); j++) {
   if (before_close[j] != OrderTicket())
      new_order = true;
   else
      { new_order = false; break; }
}
if (new_order)
   return (OrderTicket());

Also, just as ubzen stated, your code presumes that no two orders will share the same OrderOpenPrice() and OrderOpenTime().  If that is true for you, then you have found your answer.  For me, I can envision several instances where two or more orders might share the same OrderOpenTime() and OrderOpenPrice().  While I generally don't partially close orders at this time, I can see where, in certain circumstances, it can be useful.  But if I did (or when I do), I cannot make the presumption that the OrderOpenPrice() and OrderOpenTime() describe one (and only one) order.

avatar
352
GreenMoney 2013.04.22 19:57 #
 
WHRoeder:
Assuming you're not using a fixed lot size, modify the SL from initial (below break even to BE or above) and then do the partial close. The next time your orderSelect loop will find the new ticket and you will know that you already did a partial close.
This probably won't work as intended for those (like me) who use a broker that enforces FIFO rules by forcing all orders for the same symbol to reflect the most recent stoploss and takeprofit changes.  See here.

avatar
15
assassin 2015.08.05 18:25 #
 

Hi all,

I solve this kind of problem with magic number

But now, I have different problem in OrderOpenTime

I need to keep original orderopen time with me but partial close change also order open time

is there a way to keep these time ?


Thanks

avatar
13009
WHRoeder 2015.08.05 21:34 #
 
nunonuk:

If there aren't any other positions created, between the creation of theorder and the partial close, I can use

OrderTicket()+1

tocatch the following position ticket number.

Only in the tester is that true.
Back to topics list   | 1 2  

To add comments, please log in or register