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

Timing/latency issues when using alpacaTradingClient.PostOrderAsync #747

Open
1 task done
rshahrabani opened this issue Aug 8, 2024 · 1 comment
Open
1 task done
Assignees
Labels
question Further information is requested
Milestone

Comments

@rshahrabani
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

What is your question?

Using either sync or async options for PostOrderAsync:

var order = alpacaTradingClient.PostOrderAsync(LimitOrder.Buy(symbol, 1, limitPrice));
var order = await alpacaTradingClient.PostOrderAsync(LimitOrder.Buy(symbol, 1, limitPrice));

We notice the first order placed in a batch of orders takes much longer than subsequent orders. See below for timing results for both methods. Why does this happen and is there any way we can reduce latency on the first order?

Async timing

symbol: AAPL, limitPrice: 212.00, Elapsed time: 70
symbol: MSFT, limitPrice: 400.00, Elapsed time: 0
symbol: TSLA, limitPrice: 200.00, Elapsed time: 1
symbol: T, limitPrice: 20.00, Elapsed time: 2
symbol: F, limitPrice: 10.00, Elapsed time: 0
symbol: GOOG, limitPrice: 170.00, Elapsed time: 7
symbol: META, limitPrice: 500.00, Elapsed time: 6
symbol: NVDA, limitPrice: 110.00, Elapsed time: 0
symbol: AMZN, limitPrice: 170.00, Elapsed time: 1
symbol: GM, limitPrice: 50.00, Elapsed time: 0

Sync timing

symbol: AAPL, limitPrice: 212, Elapsed time: 262
symbol: MSFT, limitPrice: 400, Elapsed time: 41
symbol: TSLA, limitPrice: 200, Elapsed time: 29
symbol: T, limitPrice: 20, Elapsed time: 30
symbol: F, limitPrice: 10, Elapsed time: 30
symbol: GOOG, limitPrice: 170, Elapsed time: 29
symbol: META, limitPrice: 500, Elapsed time: 25
symbol: NVDA, limitPrice: 110, Elapsed time: 28
symbol: AMZN, limitPrice: 170, Elapsed time: 35
symbol: GM, limitPrice: 50, Elapsed time: 31

Anything else?

No response

@OlegRa OlegRa self-assigned this Sep 16, 2024
@OlegRa OlegRa added the question Further information is requested label Sep 16, 2024
@OlegRa OlegRa added this to the SDK 7.x LTS milestone Sep 16, 2024
@OlegRa
Copy link
Collaborator

OlegRa commented Sep 29, 2024

Hi @rshahrabani, I'm sorry for the delayed response. The JSON serialization library is the cause of this significant delay in the first-order sending request. Currently, the library uses the Newtonsoft.JSON library for JSON handling. This library is reflection-based, so any attempt to serialize/deserialize any type requires type inspection and code generation.
We plan to replace this library with the more modern System.Text.Json library. This library supports code generation, and it should solve a problem. However, I don't have any estimations for this upgrade for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants