Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 ClientOrderId propagation 🚧 #617

Open
Slacquer opened this issue May 8, 2023 · 3 comments
Open

🚧 ClientOrderId propagation 🚧 #617

Slacquer opened this issue May 8, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@Slacquer
Copy link

Slacquer commented May 8, 2023

Hello, I am using the ClientOrderId for propagation to help me determine bracket success and failures. I have noticed that when i set the ClientOrderId in a BracketOrder.WithClientOrderId, this value DOES NOT end up anywhere in the streamingClient.OnTradeUpdate. I either just simply do not understand its purpose or there is a bug. The ITradeUpdate.Order object does not contain the value that i specified, it does have a random Guid, but not the value i added., For example the value i specified was like this "TSLA-[SOME GUID]-T1" this allowed me to know that my target 1 of a bracket order had been hit. Upon further investigation, it should be noted this is ONLY a problem when a target or stoploss is hit, that is when a seemingly random id is generated for the ClientOrderId, and not MY specified ClientOrderId. my code looks at this if (tu.PositionQuantity == 0) (tu being ITradeUpdate)

@OlegRa OlegRa self-assigned this May 17, 2023
@OlegRa OlegRa added enhancement New feature or request question Further information is requested labels May 17, 2023
@OlegRa
Copy link
Collaborator

OlegRa commented May 17, 2023

@Slacquer, I have one question about this issue. We're speaking about client order identifiers for the leg orders (take profit and stop loss) of the bracket order, right? Right now you can specify only one client order ID per order request and this order ID will be assigned to the main (market) order of the compound bracket order.

The workaround is to get the main order with both legs and change the auto-assigned client order ID for each leg using the order modification request. It will require 3 additional API calls but if you want to track the execution of your orders via streaming API this is only the option. If you'll confirm that I understand the problem correctly I can prepare a code snippet that will do all the needed work for you and maybe I'll even include this code in the Extensions library.

@Slacquer
Copy link
Author

Yes correct, the leg orders have a random guid, not the one I assigned, in fact all clientOrderIds are random when a target or stoploss is hit, NOT my id. I have since reworked my code to only create a single bracket order, i would ideally like to have 2 targets. Any suggestions would be very helpful.

@OlegRa
Copy link
Collaborator

OlegRa commented May 20, 2023

After some research and discussing the problem with Alpaca technical support, I can confirm that it's impossible to solve this problem right now. There are two possible options to specify the client_order_id field for an order: on order creation or by patching the existing order.

The current API for order placement supports only one client_order_id per order and this ID will be used for the main (market) order in the bracket compound order. Order placement API returns full order information so we can read the Legs property and replace auto-generated IDs with our custom IDs. Sounds good but, unfortunately, this will not work because these leg orders are created in the HELD state and API rejects any attempts to change orders in this state.

According to Aplaca API developers, the rule about preventing orders change in the HELD state is more generic and probably will never be changed. So the only proper way to provide client_order_id is on order placement but it requires changes on the server side first. As soon as this support will be implemented I'll add the appropriate code to this SDK. Unfortunately @Slacquer, I'm unable to provide you with any ETA for this feature right now.

As a temporary workaround, you can maintain the map between your expected client_order_id values and auto-generated values returned from the PostOrderAsync method. The orders in the IOrder.Legs collection are in fact take profit and stop loss orders and you can get their auto-generated ClientOrderId properties and save them into a hash table as keys with values equal to your custom IDs and use this hash table in stream event processing logic.

@OlegRa OlegRa added this to the SDK 7.0.x Experimental milestone May 20, 2023
@OlegRa OlegRa removed the question Further information is requested label May 20, 2023
@OlegRa OlegRa changed the title ClientOrderId propagation 🚧 ClientOrderId propagation 🚧 May 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants