Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View akhan118's full-sized avatar
:octocat:
out and about

Ahmad Khan akhan118

:octocat:
out and about
  • Detroit , Michigan
View GitHub Profile
@akhan118
akhan118 / kiki.py
Created July 27, 2018 18:32
A Python Client For TdAmeritrade API
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
@akhan118
akhan118 / Get_Quote.py
Created July 27, 2018 21:24
Get Quote
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 / get_access_token.py
Created July 27, 2018 21:01
Get Access Token
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
@akhan118
akhan118 / Get_Price_History.py
Created July 27, 2018 21:11
Get Price History
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())
@akhan118
akhan118 / sinceEpoch.py
Created July 27, 2018 21:20
Converting time stamps to milliseconds since epoch.
def unix_time_millis(self,dt):
epoch = datetime.utcfromtimestamp(0)
return int((dt - epoch).total_seconds() * 1000.0)
@akhan118
akhan118 / KikiResults.txt
Last active July 27, 2018 20:53
Kiki results
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
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)))