I used to use TD Ameritrade API, but then they switched to Schwab, and Schwab has this retarded token scheme which I can't be bothered to figure out. It was simple, I did response.get('<url containing data parameters>') with python and I got the data I needed. But now, no. You have to login and do stuff that I don't understand. All I really need is (US stock and etf) minute data(o,h,l,c,v) from the past 20 trading days. I'd prefer more than that, but w/e. That much is enough. Ideally, data since 2000.
import requests # Set up your OAuth2 credentials and endpoint auth_url = "https://api.schwab.com/oauth2/token" client_id = "your_client_id" client_secret = "your_client_secret" # Make the token request response = requests.post(auth_url, data={ "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret }) # Extract the access token token_data = response.json() access_token = token_data.get("access_token") import datetime # Define the historical data endpoint and the symbol historical_data_url = "https://api.schwab.com/markets/historical" symbol = "SPY" # Set the date range for the past 30 days end_date = datetime.date.today() start_date = end_date - datetime.timedelta(days=30) # Set up the headers with the access token headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } # Make the request params = { "symbol": symbol, "start_date": start_date.strftime("%Y-%m-%d"), "end_date": end_date.strftime("%Y-%m-%d") } response = requests.get(historical_data_url, headers=headers, params=params) # Extract data from the response historical_data = response.json()
thanks but i do: Code: import requests # Set up your OAuth2 credentials and endpoint auth_url = "https://api.schwabapi.com/v1/oauth/token" client_id = "____" client_secret = "____" # Make the token request response = requests.post(auth_url, data={ "grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret }) # Extract the access token token_data = response.json() access_token = token_data.get("access_token") print ( access_token ) headers = { 'Authorization' : f'Bearer {access_token}' , 'Content-Type' : 'application/json' } url = 'https://api.schwabapi.com/marketdata/v1/pricehistory' stk = "RACE" params = { 'symbol' : stk, 'periodType' : 'day', 'period' : 10, 'frequencyType' : 'minute', 'frequency' : 30, 'needExtendedHoursData' : 'false' } response = requests.get( url, headers=headers, params=params ) data = response print( data ) I get: Code: None <Response [400]> ----------------------- program exited with code 0 If I do it with .json() I get jsondecoder error expecting value line 3 column 9 Fooling around with the code, I print( token_data ) results is ['error','invalid client'] I really feel like I'm on some wild goose chase with this. I had some code which would open the browser and do something like register my token or something, I would get a url that I would paste into the terminal, and then I'd have authorization. But for some reason I lost that code, and can't find it anywhere on my computer or on the internet.
Alpha Vantage provides intraday data of multiple resolution (1,5,30,…) for the last 30 days but we’re limited to 25 requests per day (free)
You may find this resource useful - https://quantjourney.substack.com/p/60-market-data-integrations-to-power 60+ Market Data Integrations to Power Your Trading Strategies