Lost magic number and comment after partially closing

 

Does anyone here have a solution about this problem? Let's say you use OrderSend to open 0.5 lots. If you call orderClose to close 0.1 lot. The system would generate another new 0.4 lots order, but your original magic number and comment were lost. Without them, your EA couldn't work together with others on the same symbol. I have to open multiple orders for this problem, wondering if there is anyway to keep them.

Thanks!

 

Lost magic number

No warranty to this code but it has worked for a very long time

int orderToClose = (ticket number of the smaller ticket);

int closingTicket = (ticket number of the larger ticket);

if (orderToClose != 0 && closingTicket != 0)

{

bFlag1 = OrderCloseBy(orderToClose, closingTicket);

if (bFlag1 == true)

{

newTicket = 0;

// Check for a new ticket

for (idx2 = OrdersTotal() - 1; idx2 >= 0; idx2--)

{

if (OrderSelect(idx2, SELECT_BY_POS, MODE_TRADES))

{

s = StringConcatenate("split from #", orderToClose);

splitIndex = StringFind(OrderComment(), s, 0);

if (splitIndex != -1)

{

// We found the new ticket number

newTicket = OrderTicket();

// Now update the old ticket to the new ticket

closingTicket = newTicket;

break;

}

}

}

}

In my opinion MQL4 has it completely backwards on this point. They change the ticket number making it very difficult to find.

I hope this helps.

Scott

 

lost magic number

I assure you I can format code; for some reason all my formatting went out the window when I posted my reply .

 
ScottB:
I assure you I can format code; for some reason all my formatting went out the window when I posted my reply .

Try using the code tags when posting. It looks like a "#" on the toolbar.

 

Thanks, so it will use "Split from xxx" as the comment of the new orders. I will try it.

ScottB:
No warranty to this code but it has worked for a very long time

int orderToClose = (ticket number of the smaller ticket);

int closingTicket = (ticket number of the larger ticket);

if (orderToClose != 0 && closingTicket != 0)

{

bFlag1 = OrderCloseBy(orderToClose, closingTicket);

if (bFlag1 == true)

{

newTicket = 0;

// Check for a new ticket

for (idx2 = OrdersTotal() - 1; idx2 >= 0; idx2--)

{

if (OrderSelect(idx2, SELECT_BY_POS, MODE_TRADES))

{

s = StringConcatenate("split from #", orderToClose);

splitIndex = StringFind(OrderComment(), s, 0);

if (splitIndex != -1)

{

// We found the new ticket number

newTicket = OrderTicket();

// Now update the old ticket to the new ticket

closingTicket = newTicket;

break;

}

}

}

}

In my opinion MQL4 has it completely backwards on this point. They change the ticket number making it very difficult to find.

I hope this helps.

Scott