areed1192 / td-ameritrade-python-api

Unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard API and allows users to build data pipelines for the Streaming API.
MIT License
684 stars 252 forks source link

Enhancement - Persistent requests.Session Object in TDClient #180

Closed Jmarks199 closed 3 years ago

Jmarks199 commented 3 years ago

Starting on 5/5 I started noticing response times from the server about 10x longer than usual. I reached out to TDA but they said there were no issues with the server. The issue is persisting for me today. Running curl on a GET endpoint of the server seems to show much longer than usual handshake process.

I began investigating looking for solutions. That lead me to the td.client module TDClient._make_request() method. I see a new Session object is initialized for every request and closed after the response is received. I was wondering if making the session an instance variable had been considered.

The reason is that running some diagnostics, my response times for the request_session.send() inside _make_request() are ~1.3 seconds lately. However, re-running the request with the same Session object only takes about 100ms, as expected. I'd like to reconfigure this method such that we re-use the Session across requests. Does anyone see an issue with this? Additionally, is anyone else experiencing the long request times?

Jmarks199 commented 3 years ago

Also confirmed that calling the same function twice results in the long response time both times, i.e. the increase seems related to re-using the Session object.

Jmarks199 commented 3 years ago

As a follow up, I have been running with the persistent Session object for the last few days, but it has not helped my situation. I have also noticed that some endpoints are much faster than others e.g. get_accounts(). It is with requesting quotes that i am seeing the longest latency right now and I have not been able to improve this. I'm guessing it is just a server side limitation, although I was not seeing latency this high in the past.

Using a persistent Session may still be a slight enhancement, but it's hard to tell.

Jmarks199 commented 3 years ago

Is anyone else dealing with erratic, high latency particularly involving positions, orders and quote requests? I am still seeing some requests take 1.3 seconds + regularly. I'd estimate 20-40% of all api calls take this long. I've reached out to TD but they have not responded.

Jmarks199 commented 3 years ago

As a follow up - I guess maybe TD was doing same maintenance to the servers or something. Eventually the response times came down and are now lower than ever before, and I have not seen any issues for at least a month.