Search code examples
apihtml-tableameritrade

Does anyone know how to get the hours for markets such as the NYSE on TD Ameritrade API?


I'm trying to request hour information from the TD Ameritrade API. Is there any way I could get the hours for the New York Stock Exchange?

Here is the link to the API: https://developer.tdameritrade.com/


Solution

  • TD Ameritrade's API provides an endpoint for market hours however you can't specify which exchange to check, only which market (BOND, EQUITY, ETF etc..).

    For some reason, it allows you to specify which date but it only accepts the current date. Providing an access token doesn't do anything so I won't be including a header with the token in my example.

    import json
    import urllib.request
    
    api_key = 'PRIVATE'
    
    
    def get_market_hours(market, date):
    
        params = f'?apikey={api_key}&date={date}'
        url = f'https://api.tdameritrade.com/v1/marketdata/{market}/hours' + params
    
        with urllib.request.urlopen(url) as response:
            text_bytes = response.read()
    
        text_str = text_bytes.decode('utf-8')
        text_json = json.loads(text_str)
    
        print(text_json)
    
    
    get_market_hours('EQUITY', '2020-12-23')
    

    According to their site, most of NYSE is open 06:30 - 20:00 EST, it might be easier to just check if the current time is within that timeframe and if the current day is not a holiday.