-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestrade.py
179 lines (134 loc) · 5.26 KB
/
questrade.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from params import *
import requests as req
'''
* Connector class for interfacing with Questrade API:
* https://www.questrade.com/api/documentation/
'''
class Questrade:
# self._access_token
# self._access_token_type
# self._account
# self._api_server
# self._authorized_headers <dict>
# self._refresh_token
# self._refresh_token_expires_in
# self._refresh_token_file
def __init__(self):
self._refresh_token_file = open(REFRESH_TOKEN_FILE, 'r+') # TODO = check file exists
self._refresh_token = self._refresh_token_file.read()
self._authorize()
self._account = self._update_account()
'''
* Executes get request, returns successful reply as JSON
* Handles logging and exception handling
'''
def _get_as_json(self, api_url, req_params=None, req_headers=None):
res = req.get(api_url, headers=req_headers, params=req_params)
res.raise_for_status() # raise exception if not 200 OK
return res.json()
'''
* Get access token and resource server information
'''
def _authorize(self):
req_params = {'grant_type': 'refresh_token', 'refresh_token': self._refresh_token}
res_json = self._get_as_json(QUESTRADE_OAUTH2_REFRESH_TOKEN_SERVER, req_params=req_params)
self._access_token = res_json['access_token']
self._access_token_type = res_json['token_type']
self._api_server = res_json['api_server']
self._refresh_token = res_json['refresh_token']
self._refresh_token_expires_in = res_json['expires_in']
self._authorized_headers = {
'Host': self._api_server,
'Authorization': self._access_token_type + ' ' + self._access_token
}
self._save_refresh_token()
return
'''
* Write new refresh_token to file
'''
def _save_refresh_token(self):
try:
self._refresh_token_file.seek(0)
self._refresh_token_file.write(self._refresh_token)
self._refresh_token_file.truncate()
self._refresh_token_file.close()
except:
print("error writing to refresh token file")
return
'''
* Uses API to get account number and populates _Account object
*
* For the purpose of this API's implementation, there is only
* a single account expected per user though this may be expanded
* in the future
'''
def _update_account(self):
num = self._get_account_number()
balances = self._get_account_balances(num)
positions = self._get_account_positions(num)
accnt = self._Account(num, balances['cash'], balances['total_equity'], positions)
return accnt
def _get_account_number(self):
res_json = self._get_as_json(self._api_server + 'v1/accounts',
req_headers=self._authorized_headers)
return res_json['accounts'][0]['number']
def _get_account_balances(self, acc_num):
res_json = self._get_as_json(self._api_server + 'v1/accounts/' + acc_num + '/balances',
req_headers=self._authorized_headers)
for balance in res_json['perCurrencyBalances']:
if balance['currency'] == 'CAD':
return {
'cash': balance['buyingPower'],
'total_equity': balance['totalEquity']
}
raise Exception #TODO - specific
def _get_account_positions(self, acc_num):
res_json = self._get_as_json(self._api_server + 'v1/accounts/' + acc_num + '/positions',
req_headers=self._authorized_headers)
positions = {}
for pos in res_json['positions']:
ticker = pos['symbol']
current_price = pos['currentPrice']
num_shares = pos['openQuantity']
positions[ticker] = Position(ticker, current_price, num_shares)
return positions
'''
* Data Object representing the account of the resource owner
* Comprising of data of balances and positions held
*
* This class is immutable, it holds data of an
* account at a particular instance in time to avoid calls to API
* In the future there may be several accounts per user
'''
class _Account:
# self.account_number
# self.cash
# self.total_equity
# self.positions <ticker:Position dict>
def __init__(self, account_number, cash, total_equity, positions):
self.account_number = account_number
self.cash = cash
self.total_equity = total_equity
self.positions = positions
# Interface Methods
# if considerable amount of time passed - update account, else use cached values
'''
* Get the amount of cash (buying power) in the account
'''
def get_cash(self):
return self._account.cash
def get_total_equity(self):
return self._account.total_equity
def get_positions(self):
return self._account.positions
'''
* Represents the most recent status of a position ie. VEE.TO
'''
class Position:
# self.ticker
# self.current_price
# self.num_shares
def __init__(self, ticker, current_price, num_shares):
self.ticker = ticker
self.current_price = current_price
self.num_shares = num_shares