Closing out half lots. - page 16

 
Ok I've made a little progress. I have an issue though. If I place an order for the first time on any given pair, it will NOT close half of the order UNLESS I have a trade to compare against in the history... In which case, if the first trade went 1:1, it will not close out half of the position as it has nothing to compare against in the history pool... Likewise, I am not entirely sure what I am doing wrong, but even when there is a trade in the history to compare with, (OrderOpenTime()), it is still closing it out continuously at the same price?

int start()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
if(OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES)==True)
{  
   OrderEntryTime            = OrderOpenTime();
   datetime OrderEntryClosed = OrderCloseTime(); 
    CurrentSymbol            = OrderSymbol();
     if(OrderType() <= OP_SELL && CurrentSymbol == Symbol())
         {   
         Print(" The Selected Order ", CurrentSymbol, " matches this pair: ", Symbol());
         Print(" The Open Time of this order was: ", TimeToStr(OrderEntryTime, TIME_MINUTES | TIME_DATE));  
         }
         
  if(OpenOrdersThisPair(Symbol())>0 && CurrentSymbol==Symbol())
     {
      // Need a for loop to compare historically closed trades that match with Symbol() and the "datetime OrderEntryTime = OrderOpenTime();" above.
     CheckHistoricalTrades(); 
     }
   }
}
}  
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  

void CheckHistoricalTrades()
{
for(int Pos=OrdersHistoryTotal()-1; Pos >= 0; Pos--) 
    {
     if (OrderSelect(Pos, SELECT_BY_POS, MODE_HISTORY)  
        &&  OrderMagicNumber()  == MagicNumber            
        &&  CurrentSymbol       == Symbol()            
        &&  OrderType()         <= OP_SELL
        &&  OrderEntryTime    >  OrderOpenTime() && OrderType()==OP_BUY)
          {
          Print(" Last Order Open Time: ", TimeToStr(OrderOpenTime(), TIME_MINUTES | TIME_DATE), 
                  " Was not the same as current order: ", TimeToStr(OrderEntryTime, TIME_MINUTES | TIME_DATE));
          CloseHalfOrder();
          }
                if(OrderEntryTime > OrderOpenTime() && OrderType()==OP_SELL)
                  {
                  Print("Last Order Open Time: ", TimeToStr(OrderOpenTime(), TIME_MINUTES | TIME_DATE), 
                     " Was not the same as current order: ", TimeToStr(OrderEntryTime, TIME_MINUTES | TIME_DATE));
                  CloseHalfOrder1(); // This just closes half of the position at 1:1 - then if OrderStopLoss > OrderOpenPrice() = MoveToBreakEven() void kicks in. 
                  }
                      
     }
}  
I know the problem is staring me right in then face, but I can't see it...
 
This is the prints from the journal - some prints are not shown above - but all of the above are.
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: Last Order Open Time: 2010.02.04 09:22 Was not the same as current order: 2010.02.25 01:16 //>>> "Last Order Open time" here doesn't make sense?
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: SELL First Target Closed: 0.03 OP_SELL First Closed - Open Time: 2010.02.05 15:07 //>>> This is the same as "The Last Order Open Time"?
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: close #16 sell 0.03 GBPJPY at 138.270 sl: 138.240 tp: 135.082 at price 136.637  //>>> Therefore, it closed it here.
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: Ask >= FirstTarget_Sell - Current Ask: 136.637 FirstTarget_Sell: 136.676     //>>> The ask is at the OrderClose() price.
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: FirstTarget_Sell: 136.676
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: Order Ticker Number = 10
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: The Lots to close is: 0.030
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1: Last Order Open Time: 2010.02.05 15:07 Was not the same as current order: 2010.02.25 01:16
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1:  The Open Time of this order was: 2010.02.25 01:16
2013.08.09 13:56:20     2010.02.25 09:16  Trend Fishing 3 Exits GBPJPY,H1:  The Selected Order GBPJPY matches this pair: GBPJPY
 
DomGilberto:

The problem I have though; I am trying to partial close the same "OP_BUY" or "OP_SELL" up to 4 times at different prices... I think the question I should be asking is, can I get a way, where by I have a rule that ALL partial closes (of any lots and price on ONE given trade) will only partially close ONCE at their predefined "OrderClose()" parameters set...

This way I am looking at doing it now with comparing the OrderOpenTime() will essentially only work once, and will restrict any other type of OrderClose() function from happening at all... I am wanting to find a way where I can have one rule applied to 4 OrderClose() functions... (if that makes sense?)

I know people are suggesting open 4 orders, but without going too deep, it's less efficient for me doing it that way.

Why not simply have a two-dimensional array that stores the ticket number and the number of available partial closes left to perform.

Outline/Pseudocode:

1. define static two-dimensional array: cOrders[][2].

2. for each new order entered: resize cOrders' first dimension to size+1, put new order's ticket number in [x][0] and number of partial closes left to perform (in this case, 4) in [x][1].

3. at whatever time interval (e.g., each start()), loop through the array, select each order using the stored ticket number, and determine if a partial close needs to be performed.

4. if partial close needs to be performed (see step 3), partially close order using OrderClose() and update cOrders[x][0] to reflect new ticket number and reduce cOrders[x][1] by 1.

5. remove from cOrders any orders that have been closed or that their number of partial closes left to perform is 0.

The only issue is what to do upon platform/computer restarts. You could store that information in a file and read the file in init() upon platform restart.

 
Thirteen:

Why not simply have a two-dimensional array that stores the ticket number and the number of available partial closes left to perform.

Outline/Pseudocode:

1. define static two-dimensional array: cOrders[][2].

2. for each new order entered: resize cOrders' first dimension to size+1, put new order's ticket number in [x][0] and number of partial closes left to perform (in this case, 4) in [x][1].

3. at whatever time interval (e.g., each start()), loop through the array, select each order using the stored ticket number, and determine if a partial close needs to be performed.

4. if partial close needs to be performed (see step 3), partially close order using OrderClose() and update cOrders[x][0] to reflect new ticket number and reduce cOrders[x][1] by 1.

5. remove from cOrders any orders that have been closed or that their number of partial closes left to perform is 0.

The only issue is what to do upon platform/computer restarts. You could store that information in a file and read the file in init() upon platform restart.

I think the easiest way of doing this is with Magic Numbers . . . use parts of the number to define different parameters, for example: number of parts, order number, day, month, EA number . . . all parts would have the same Magic Number and could be traced easily in the History. I assume that the closed part and open part left both have the same Magic Number . . .
 
Yea I think so. The magic number is always the same "1234"?

I have not a clue how to use parts of the magic number to define different parameters? I think using OrderOpenTime() is not going to be a logical route... I can't believe how long this thread is getting. Lol.

I swear to god I am going to do a thread on this once I (with a lot of help from everyone else!) have cracked it.
 
RaptorUK:
I think the easiest way of doing this is with Magic Numbers . . . use parts of the number to define different parameters, for example: number of parts, order number, day, month, EA number . . . all parts would have the same Magic Number and could be traced easily in the History. I assume that the closed part and open part left both have the same Magic Number . . .

The use of the Magic Number to encode pieces of information is quite possible, but I see a couple of possible limitations. First, the Magic Number is an int and therefore is 10 digits long where the left-most digit can only be a 1 or 2 (and if the left-most digit is a 2, the second most-left digit must be 7 or less). Second, it might take more time per start() to review all of the history to find all the parts of each order just to determine if the current order needs (or can be) partially closed. Just not sure if that more time would be trivial or not.

 

Wait, can't I just have several magicnumbers? Use one for the initial OrderSend(), one for the OrderModify(), after half of the position is closed at the first target of 1:1, modify the order to change the MagicNumber? Therefore, when I am first looking to close half off at 1:1, it will only close off 1:1 as long as the current open "OP_BUY" corresponds to the magic number I first gave it? After it has then partially closed, modify and change that magic number?!

Lol? Is that not really simple?!

UPDATE: I obviously need to stop looking at this today - you can't modify a MagicNumber - wish you fucking could though... (facepalm) - Could you imagine how much easier this would all be...

 
DomGilberto:
Wait, can't I just have several magicnumbers? Use one for the initial OrderSend(), one for the OrderModify(), after half of the position is closed at the first target of 1:1, modify the order to change the MagicNumber? Therefore, when I am first looking to close half off at 1:1, it will only close off 1:1 as long as the current open "OP_BUY" corresponds to the magic number I first gave it? After it has then partially closed, modify and change that magic number?!

Lol? Is that not really simple?!

No. First, after the initial OrderSend(), you can't add/modify the Magic Number using OrderModify(). Second, when you do a partial order close using OrderClose(), the new order (I assume) gets the same Magic Number as the old order.

 
Thirteen:

No. First, after the initial OrderSend(), you can't add/modify the Magic Number using OrderModify(). Second, when you do a partial order close using OrderClose(), the new order (I assume) gets the same Magic Number as the old order.


Yes. I realised how retarded I am being. Long day!

Yes, after the first partial close, the remaining position is still assigned the same magic number it first started with.

It's really frustrating me now... If only I could modify the magicnumber, it would be too easy!? There has to be a logical way to partially close a position ONCE - and do it multiple times at any given predefined price specified within the OrderClose() - There needs to be an update to allow us to modify the MagicNumber()!
 

You are not thinking logically at all.

1. How would modifying the magic number help you more than knowing the OrderOpenTime() ? It amounts to the exact same thing as an identifier to that order.

2. why are you converting it to day/hour/minutes format with timetostr ? OrderOpenTime() is already in the timestamp format it looks like this: 1375356521 it represents the exact time to the second when the order was opened.

if you could change the magic number you would compare the magic number of the order to make sure it does not have the magic number that means it was already part closed right ?

If you could do that, why cant you compare the OrderOpenTime of the order with the timestamp that you know means it is one that was already part closed ?

static datetime alreadypartclosed;

if part close order is successful

alreadypartclosed = OrderOpenTime();

when order meets criteria for part closing

if(OrderOpenTime() != alreadypartclosed )

If you are managing multiple open orders at the same time use an array[] to hold each timestamp of part closed orders.

Reason: