tastyware / tastytrade

An unofficial, sync/async Python SDK for Tastytrade!
https://tastyworks-api.rtfd.io
MIT License
127 stars 43 forks source link

Performance to get candles #45

Closed pingzh closed 1 year ago

pingzh commented 1 year ago

Hi @Graeme22 wondering if you have tested the performance of oneshot_candle?, i noticed that it could take like 10 seconds to get the SPX 1-minute candle for just a day.

Graeme22 commented 1 year ago

Hey! I noticed the same thing. I did some tests and profiling and came to some conclusions:

Conclusion: once the handshake completes, although setup is completed locally, remotely there is still some kind of setup process that hasn't happened that takes about 8-9 seconds, and afterwards you're good to go. Unfortunately, there's no way to know when that process concludes remotely, as there's no kind of confirmation message sent. So there's no way to solve this problem really, I could insert a wait into the create function, but that's no guarantee and it makes more sense to just await the results anyways.

Solutions: just use await, unfortunately there's nothing else that can be done. The good news is that after the setup time at the beginning, it's no longer a problem and everything executes relatively quickly.

(Side note: this is probably why the Tastytrade clients always take so long to start up hahaha)

Graeme22 commented 1 year ago

I suppose one hacky solution would be to put a oneshot into your initialization code:

async def safe_create(session: Session) -> DataStreamer:
    streamer = await DataStreamer.create(session)
    _ = await streamer.oneshot(EventType.QUOTE, ['SPY'])

    return streamer
Graeme22 commented 1 year ago

I suppose one hacky solution would be to put a oneshot into your initialization code:

async def safe_create(session: Session) -> DataStreamer:
    streamer = await DataStreamer.create(session)
    _ = await streamer.oneshot(EventType.QUOTE, ['SPY'])

    return streamer

Something like this could go into the create function, maybe that would be wise, although total setup time would be around 14 seconds for the streamer...

Graeme22 commented 1 year ago

I went ahead and fixed this by inserting a oneshot into the create function. Setup takes longer now but once you have the streamer object it's guaranteed to be initialized.

pingzh commented 1 year ago

@Graeme22 i am wondering if you have support fo sync function instead of await/async?

Graeme22 commented 1 year ago

Not sure what your use case is, but the streamer is async to allow for concurrency; for example, it'd be common to have one task listening for quotes and another handling subscriptions. Of course, if you don't need those features you don't have to use them; just do everything in a single task.