-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkiki.py
152 lines (135 loc) · 4.85 KB
/
kiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pandas as pd
import requests
from pandas.io.json import json_normalize
from datetime import datetime
import time
from urllib.parse import unquote
from pathlib import Path
class Td:
def __init__(self, refresh_token, client_id, apikey):
# URL decoded refresh token
self.refresh_token = unquote(refresh_token)
self.code = unquote(refresh_token)
self.apikey = apikey
self.client_id = client_id
self.main()
# Checks if token file is available
# token file : refresh-token.txt
#
def main(self):
refresh_token_file = Path("refresh-token.txt")
if refresh_token_file.is_file():
self.get_access_token()
else:
self.auth_code()
# Save a refresh token
# token file : refresh-token.txt
#
def auth_code(self):
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'grant_type': 'authorization_code',
'access_type': 'offline',
'code': self.code,
'client_id': self.client_id,
'redirect_uri': 'http://localhost:8080'
}
authReply = requests.post(
'https://api.tdameritrade.com/v1/oauth2/token',
headers=headers,
data=data)
if authReply.status_code == 200:
refresh_token = authReply.json()
f = open("refresh-token.txt", "w+")
f.write(refresh_token['refresh_token'])
f.close()
else:
print('Failed to obtain a refresh token: auth_code(): Status',
authReply.status_code)
print('Obtain a new Code from TDAmeritrade')
# Gets Access Token
#
#
def get_access_token(self):
#Post Access Token Request
my_file = Path("refresh-token.txt")
if my_file.is_file():
f = open("refresh-token.txt", "r")
if f.mode == 'r':
token = f.read()
f.close()
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'grant_type': 'refresh_token',
'refresh_token': token,
'access_type': 'offline',
'client_id': self.client_id,
'redirect_uri': 'http://localhost:8080'
}
authReply = requests.post(
'https://api.tdameritrade.com/v1/oauth2/token',
headers=headers,
data=data)
if authReply.status_code == 200:
refresh_token = authReply.json()
f = open("refresh-token.txt", "w+")
f.write(refresh_token['refresh_token'])
f.close()
else:
print(authReply.json())
return authReply
# Get Qoute
# Param : Symbol
#
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)
return (authReply.json())
# Convert time to Unix Time Stamp
# Param : Time
#
def unix_time_millis(self, dt):
epoch = datetime.utcfromtimestamp(0)
return int((dt - epoch).total_seconds() * 1000.0)
# Get price History
# Param : Symbol, Start date , End date
#
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)
candles = authReply.json()
df = json_normalize(authReply.json())
df = pd.DataFrame(candles['candles'])
return df
code=''
apikey ='[email protected]'
client_id = '[email protected]'
p = Td(code, 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)))