Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:
out and about

Ahmad Khan akhan118

:octocat:
out and about
View GitHub Profile
View Get_Quote.py
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())
@akhan118
akhan118 / sinceEpoch.py
Created Jul 27, 2018
Converting time stamps to milliseconds since epoch.
View sinceEpoch.py
def unix_time_millis(self,dt):
epoch = datetime.utcfromtimestamp(0)
return int((dt - epoch).total_seconds() * 1000.0)
View Get_Price_History.py
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())
View get_access_token.py
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
View KikiResults.txt
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
View kiki_how_to_use_it.py
token='xxxxxxx'
apikey ='xxxxxx@AMER.OAUTHAP'
client_id = 'xxxxxxx@AMER.OAUTHAP'
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)))
@akhan118
akhan118 / kiki.py
Created Jul 27, 2018
A Python Client For TdAmeritrade API
View kiki.py
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