This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_quotes(self,symbol): | |
| access_token = self.get_access_token().json() | |
| access_token = access_token['access_token'] | |
| headers={'Content-Type': 'application/x-www-form-urlencoded', | |
| 'Authorization': 'Bearer {}'.format(access_token)} | |
| data = { 'symbol': symbol, 'apikey': self.apikey} | |
| authReply = requests.get('https://api.tdameritrade.com/v1/marketdata/quotes', | |
| headers=headers, params=data) | |
| # print(authReply) | |
| return (authReply.json()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def unix_time_millis(self,dt): | |
| epoch = datetime.utcfromtimestamp(0) | |
| return int((dt - epoch).total_seconds() * 1000.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_price_history(self,symbol,startDate=None,endDate=None): | |
| access_token = self.get_access_token().json() | |
| access_token = access_token['access_token'] | |
| headers={'Content-Type': 'application/x-www-form-urlencoded', | |
| 'Authorization': 'Bearer {}'.format(access_token)} | |
| data = { 'periodType': 'year','frequencyType':'daily', | |
| 'startDate':startDate,'endDate':endDate} | |
| authReply = requests.get('https://api.tdameritrade.com/v1/marketdata/'+symbol+'/pricehistory', | |
| headers=headers, params=data) | |
| # print(authReply.json()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_access_token(self): | |
| #Post Access Token Request | |
| headers = { 'Content-Type': 'application/x-www-form-urlencoded' } | |
| data = { 'grant_type': 'refresh_token', 'refresh_token': self.refresh_token, | |
| 'client_id': self.client_id, 'redirect_uri': 'http://localhost:8080'} | |
| authReply = requests.post('https://api.tdameritrade.com/v1/oauth2/token', headers=headers, data=data) | |
| return authReply |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| close datetime high low open volume | |
| 0 14.08 1522731600000 14.9000 13.800 14.80 33231754 | |
| 1 14.59 1522818000000 14.7800 13.620 13.69 20131765 | |
| 2 14.39 1522904400000 14.9600 14.195 14.70 17015408 | |
| 3 14.25 1522990800000 14.5700 13.980 14.35 13533877 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| token='xxxxxxx' | |
| apikey ='[email protected]' | |
| client_id = '[email protected]' | |
| p = Td(token,client_id,apikey) | |
| start_date = datetime.strptime('04 3 2018 1:33PM', '%m %d %Y %I:%M%p') | |
| end_date = datetime.strptime('05 3 2018 1:33PM', '%m %d %Y %I:%M%p') | |
| print(p.get_price_history('SNAP',p.unix_time_millis(start_date), | |
| p.unix_time_millis(end_date))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import requests | |
| from pandas.io.json import json_normalize | |
| from datetime import datetime | |
| import time | |
| class Td: | |
| def __init__(self,refresh_token,client_id,apikey): | |
| # URL decoded refresh token | |
| self.refresh_token= refresh_token |