Submitting an order to the TWS requires three things are required:
Perhaps the most important event received after successfully connecting to the TWS is the nextValidId, which is also triggered after invoking the reqIds method. As its name indicates, the nextValidId event provides the next valid identifier needed to place an order. This identifier is nothing more than the next number in the sequence. This means the client application does not have to obtain a new valid identifier every time it needs to submit a new order. It is enough to increase the last value received from the nextValidId method by one. For example, if the valid identifier for your first API order is 1, the next valid identifier would be 2, and so on. You can always use the reqIds method in the event that your client application accidentally loses track of this sequence.
The next valid identifier is persistent between TWS sessions and completely independent from one client to another. A client application connecting with client ID 123 will always have its own sequence starting where it left it regardless of a second client connecting with client ID 987. An example further ahead will make this easier to understand.
If necessary, you can reset the order ID sequence in TWS in the API Settings in the Configuration window.
Important: For the sequence to be correctly reset, there cannot be any API-submitted active orders.