API of TD Ameritrade

Discussion in 'Automated Trading' started by earth_imperator, Jul 8, 2022.

  1. Ahhh! Very cool.
     
    #21     Jul 9, 2022
  2. ok, here is how it works.
    The access token is good for 30 minutes and does need to be renewed.
    The REFRESH token is good for 90 days.

    You can login either with the full login process with the credentials etc, OR you can do so with the REFRESH TOKEN. So to renew the access token every 30 minutes, use the REFRESH token

    In order to have the REFRESH Token returned along with the access token, you need to add "access_type=offline" parameter to your authorization request

    So, you ALWAYS want to add the "access_type=offline" when doing full login, but only do it when the refresh token nears the 90 day expiration for the request where you login with the refresh token.

    NOTE: DO NOT do the offline request every time when renewing the access token. Only do it once every 90 days. Doing it every time causes problems.

    you would want to store the refresh token you receive along with its expiration, so it can be reused during subsequent app sessions.

    As for the complaints about why use this, it is actually pretty standard login procedure for 3rd party API access. Same type of thing is used by E*Trade, TradeStation, Tradier, Ally Invest, Questrade, Facebook, Twitter, Stocktwits and others. But again, if you don't want to go through the headaches, you have other options with our API.
     
    #22     Jul 9, 2022
    earth_imperator likes this.
  3. the API knows which account(s) you have access to by your login. Plus you have to include the account ID in the various requests, so if you have multiple accounts tied to your login, you can access all of them with the single login
     
    #23     Jul 9, 2022
  4. @j_medved & all, thank you very much.

    Now all of the auth stuff works fine.
    I also quickly tested to get stock quotes: it too worked fine (using "Bearer <access_token>")
    As next will try to get options quotes, and then get account data and positions, and orders etc. Much work ahead...
     
    #24     Jul 9, 2022
    j_medved likes this.
  5. #25     Jul 10, 2022
    earth_imperator likes this.
  6. An advantage of the TD API is that it does not require a local application of the broker to connect thru.
    OTOH the API of IB does require running "TWS" or "IB Gateway" application (big Java applications).

    This is important when running own application on a remote Internet server. The TD code can run on a server without a desktop GUI (ie. console system, a "headless server") whereas for the IB API the server must have a GUI desktop with Java installed.
     
    #26     Jul 13, 2022