How to preserve comments after partially closing orders

Hello.

In my EA I use comments to identify my orders. However when I partially close an order, it seems, that it closes the old order and opens new order with the remaining lot size, but the comment is no longer there. Is there a way how to carry on the comment when partially closing an order?

Not a good idea to use comments, brokers can change comments including complete replacement

And do you know about any alternative? I tried magic number, but my IDs are too big for integer variable.

Never use comments to track the orders.

The following solution may not be the 100% optimal but it works decent for me.

Use global variables to track the partial closure.
Assign a value to the global variable at the time of order execution. If the value is unchanged, do the partial close first time and update the variable value.

You can do the multiple partial closure subsequently. And, don’t forget to reset the value once you are done with the order.

As the global variables are saved in the memory, terminal restart will not affect the ea.

That is not very optimal indeed. My EA runs a lot of orders simultaneously, so I would have to keep track which variable is paired with which order. Also some orders can be partially closed and some will be closed fully.

Yes, works good for the single active order but it would be a complete mess in case of multiple active orders.

  1. Define a structure to describe open position with all your custom extra attributes.

  2. Define an array to store such structures.

  3. Refresh array on every tick - add new trades, delete old ones etc.

This way you can store as many extra attributes for an open position as you like.

  1. Optional - save array to file to prevent data loss so you can load it on init if EA was closed in the meantime.

And what if I shorten my IDs. Does magic number carry on when I partially close an order?

  1. what do you mean by shorten the ID / magic number?
  2. yes, magic number will remain the same.

Well my own ID that I use now for order identification are too long/big for integer variable (in OrderSend function the magic number parameter is integer). But if I can make them shorter/smaller, so they actually fit into integer variable without overflowing, I can put my ID into magic number and identify my orders that way. And when you said, that they actually stay the same even after partial close, then this seems as a perfectly fine solution.

Always use a magic number for the order identification. As said whroeder1 broker can change comments not only at a partial close. The new order (after a partial close) will inherit the magic number.