kwess 0.0.5__tar.gz → 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kwess-0.0.6/PKG-INFO ADDED
@@ -0,0 +1,504 @@
1
+ Metadata-Version: 2.1
2
+ Name: kwess
3
+ Version: 0.0.6
4
+ Summary: Questrade API wrapper.
5
+ Home-page: https://github.com/kaiyoux/kwess
6
+ Author: Issa Lompo
7
+ Author-email: kaiyoux@gmail.com
8
+ Maintainer: Issa Lompo
9
+ Maintainer-email: kaiyoux@gmail.com
10
+ License: GNU General Public License v3 (GPLv3)
11
+ Project-URL: Bug Tracker, https://github.com/kaiyoux/kwess/issues
12
+ Keywords: Questrade,api,REST,wrapper
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Development Status :: 5 - Production/Stable
17
+ Classifier: Intended Audience :: Developers
18
+ Requires-Python: >=3.6
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.txt
21
+ Requires-Dist: requests>=2.28.1
22
+
23
+ # Introduction
24
+
25
+ Yet another Questrade API wrapper.
26
+
27
+ For details about the input parameters and output results, please visit
28
+ the [Questrade API documentation](https://www.questrade.com/api/home).
29
+
30
+ ### To install:
31
+ **python -m pip install kwess**
32
+
33
+
34
+ # Usage Example
35
+
36
+ ```
37
+ import kwess
38
+ from datetime import datetime as dt
39
+ from pprint import pprint
40
+
41
+ # It is assumed that your manually generated token has been saved in local file
42
+ # my_token.txt
43
+ qs = kwess.Trader(rt_file="my_token.txt", verbose="v")
44
+
45
+ accs = qs.get_accounts()
46
+ for acc in accs:
47
+ print(acc, "\n")
48
+
49
+ accn = qs.find_account_number("tfsa")
50
+ print(accn)
51
+
52
+ # Get account activities from 1/12/1999 to 28/9/2022
53
+ accs = qs.get_account_activities(startdatetime=dt(year=1999, month=12, day=1), \
54
+ enddatetime=dt(year=2022, month=9, day=28), verbose="xxx")
55
+ for acc in accs:
56
+ print(acc, "\n")
57
+
58
+ # Get (all types of) TFSA account orders from 17/8/2022 to now
59
+ accs = qs.get_account_orders(startdatetime=dt(year=2022, month=8, day=17), \
60
+ verbose="vv")
61
+ for acc in accs:
62
+ print(acc, "\n")
63
+
64
+ # Get margin account orders that are still open
65
+ accs = qs.get_account_orders(accounttype="margin", startdatetime=dt(year=2022, \
66
+ month=7, day=28), enddatetime=dt.now(), statefilter="opened", verbose="333")
67
+ for acc in accs:
68
+ print(acc, "\n")
69
+
70
+ ords = qs.get_account_orders_by_ids(accounttype="tfsa", orderid="1088713788", \
71
+ verbose="b")
72
+ pprint(ords)
73
+
74
+ ords = qs.get_account_orders_by_ids(orderid="1098747429,1088752426,1088713788", \
75
+ verbose="aa")
76
+ pprint(ords)
77
+
78
+ ords = qs.get_account_orders_by_ids(orderid=1098747429)
79
+ pprint(ords)
80
+
81
+ # Get the current time from the API server
82
+ dto = qs.get_server_time()
83
+ print(dto[0])
84
+ print(dto[1])
85
+
86
+ # Questrade does not seem to keep old account executions - only the most recent
87
+ excs = qs.get_account_executions(startdatetime=dt(year=2022, month=1, day=28), \
88
+ enddatetime=dt(year=2022, month=9, day=30), verbose="o")
89
+ for exc in excs:
90
+ print(exc, "\n")
91
+
92
+ accs = qs.get_account_balances()
93
+ print(accs)
94
+
95
+ accs = qs.get_account_positions(verbose="d")
96
+ print(accs)
97
+
98
+ sim = qs.search_symbols("vfv", verbose="88")
99
+ print(sim)
100
+
101
+ sim = qs.get_symbols_by_names("xdiv.to,xuu.to,cve.to", verbose="**")
102
+ print(sim)
103
+
104
+ sim = qs.get_symbols_by_names("hom.un.to")
105
+ print(sim)
106
+
107
+ sim = qs.get_symbols_by_ids(26070347, verbose="e")
108
+ print(sim)
109
+
110
+ sim = qs.get_symbols_by_ids("26070347,12890,8953192,18070692", verbose="ee")
111
+ print(sim)
112
+
113
+ sim = qs.get_symbol_options(12890, verbose="s")
114
+ pprint(sim)
115
+
116
+ mks = qs.get_markets()
117
+ pprint(mks)
118
+
119
+ mks = qs.get_market_quotes(12890,verbose="zz")
120
+ print(mks)
121
+
122
+ mks = qs.get_market_quotes("26070347,12890,8953192,18070692", verbose="h")
123
+ print(mks)
124
+
125
+ ops = qs.get_market_quotes_options(option_ids=[9907637,9907638])
126
+ pprint(ops)
127
+
128
+ ```
129
+
130
+
131
+ # API Class And Methods
132
+
133
+ class Trader
134
+ ```
135
+ __init__(self, rt_file='refreshToken', server_type='live', timeout=15, verbose='')
136
+ Description:
137
+ Initializer of a Trader object. Before creating a Trader object (for the very
138
+ first time or when the present token has expired), you must generate a new
139
+ token for manual authorization from your Questrade APP HUB, and save that
140
+ manually generated token in a local file. That local file's filename
141
+ (or pathname) is passed to rt_file.
142
+ When Trader creates a Trader object, it exchanges that manually obtained token
143
+ for an access token and a refresh token. The access token expires in 30 minutes
144
+ and the refresh token expires in three days.
145
+ As long as the refresh token has not expired, creating Trader objects or
146
+ calling method get_new_refresh_token will obtain a new access token (if the
147
+ current access token has expired) and obtain a new replacement refresh token
148
+ that will last for another 3 days.
149
+ Technically, as long as the current refresh token has not expired, it is
150
+ possible to keep exchanging the current refresh token for a new access token and
151
+ new refresh token pair indefinitely (by creating Trader objects or calling
152
+ method get_new_refresh_token).
153
+ If the refresh token ever expires, you must log back into your Questrade account,
154
+ generate a new token for manual authorization under APP HUB, and save that token
155
+ in the local file referred to by rt_file.
156
+ Parameters:
157
+ - rt_file name of your local file containing your refresh token.
158
+ Defaults to "refreshToken".
159
+ - server_type could be 2 possible values: "live" or "test".
160
+ "live" will allow you to interact with your real Questrade account.
161
+ "test" is for interacting with your test account.
162
+ - timeout number of seconds to wait for the API server to respond before
163
+ giving up.
164
+ Defaults to 15 seconds. Set timeout to None if you wish to wait forever
165
+ for a response.
166
+ - verbose level of verbosity represented by the number of characters in a string.
167
+ Defaults to empty string. Maximum verbosity is 1 or "v".
168
+ Returns:
169
+ Trader object.
170
+
171
+
172
+ build_datetime_string(self, adatetime=None, gmt=False)
173
+ Description:
174
+ Higher level helper method used to build a Questrade datetime string.
175
+ Parameters:
176
+ - adatetime a datetime object.
177
+ - gmt optional boolean indicating if datetime is Greenwich Mean Time.
178
+ Default value is False.
179
+ Returns:
180
+ A Questrade datetime string.
181
+
182
+
183
+ find_account_number(self, accounttype)
184
+ Description:
185
+ Finds the account number corresponding to account type.
186
+ Parameters:
187
+ - accounttype the type of account. Example "tfsa", "margin", etc.
188
+ Returns:
189
+ An account number in string format.
190
+
191
+
192
+ get_account_activities(self, startdatetime, enddatetime=None, accounttype='TFSA',
193
+ verbose='')
194
+ Description:
195
+ Generator that returns the account activities from the account related to account
196
+ type accounttype, between the range specified by startdatetime and enddatetime.
197
+ Both objects are datetime objects.
198
+ Parameters:
199
+ - startdatetime datetime object specifying the start of a range.
200
+ - enddatetime optional datetime object specifying the end of a range.
201
+ Defaults to now (datetime.datetime.now()) if not specified.
202
+ - accounttype type of Questrade account.
203
+ Defaults to "tfsa".
204
+ - verbose level of verbosity represented by the number of characters in a string.
205
+ Defaults to empty string. Maximum verbosity is 3 or "vvv".
206
+ Yields:
207
+ The account activities as a Python object representation of the returned json,
208
+ between the range startdatetime and enddatetime, in chunks of 30 days.
209
+
210
+
211
+ get_account_balances(self, accounttype='TFSA', verbose='')
212
+ Definition:
213
+ Provides the account balances for the account related to account type
214
+ accounttype.
215
+ Paramaters:
216
+ - accounttype type of Questrade account.
217
+ Defaults to "tfsa".
218
+ - verbose level of verbosity represented by the number of characters in
219
+ a string.
220
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
221
+ Returns:
222
+ Account balances as a Python object representation of the returned json.
223
+
224
+
225
+ get_account_executions(self, accounttype='TFSA', startdatetime=None,
226
+ enddatetime=None, verbose='')
227
+ Description:
228
+ Generator that provides account executions from the account related to
229
+ account type accounttype, between the range specified by startdatetime and
230
+ enddatetime. Both objects are datetime objects.
231
+ Parameters:
232
+ - startdatetime datetime object representing the beginning of a range.
233
+ - enddatetime datetime object representing the end of a range.
234
+ Defaults to now (datetime.datetime.now()) if not specified.
235
+ - accounttype type of Questrade account.
236
+ Defaults to "tfsa".
237
+ - verbose level of verbosity represented by the number of characters in
238
+ a string.
239
+ Defaults to empty string. Maximum verbosity is 3 or "vvv".
240
+ Yields:
241
+ Account executions as a Python object representation of the returned json,
242
+ between the range startdatetime and enddatetime, in chunks of 30 days.
243
+
244
+
245
+ get_account_orders(self, startdatetime, enddatetime=None, accounttype='TFSA',
246
+ statefilter='All', verbose='')
247
+ Description:
248
+ Generator that provides the account orders from the account related to
249
+ account type accounttype, between the range specified by startdatetime and
250
+ enddatetime. Both objects are datetime objects.
251
+ Parameters:
252
+ - startdatetime datetime object representing the beginning of a range.
253
+ - enddatetime optional datetime object representing the end of a range.
254
+ Defaults to now (datetime.datetime.now()) if not specified.
255
+ - accounttype type of Questrade account.
256
+ Defaults to "tfsa".
257
+ - statefilter can be used to specify the state of orders.
258
+ It has 3 possible values: Opened, Closed, or All.
259
+ Defaults to "All".
260
+ - verbose level of verbosity represented by the number of characters in a string.
261
+ Defaults to empty string. Maximum verbosity is 3 or "vvv".
262
+ Yields:
263
+ Account orders as a Python object representation of the returned json,
264
+ between the range startdatetime and enddatetime, in chunks of 30 days.
265
+
266
+
267
+ get_account_orders_by_ids(self, orderid, accounttype='TFSA', verbose='')
268
+ Description:
269
+ Provides the account orders, specified by orderid, from the account related to
270
+ account type accounttype.
271
+ Parameters:
272
+ - orderid is a string of one or many orderid numbers. Several orderid numbers are
273
+ seperated by commas (with no spaces). A single orderid could be passed as
274
+ a numeral instead of a string.
275
+ - accounttype type of Questrade account.
276
+ Defaults to "tfsa".
277
+ - verbose level of verbosity represented by the number of characters in a string.
278
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
279
+ Returns:
280
+ Account orders as a Python object representation of the returned json.
281
+
282
+
283
+ get_account_positions(self, accounttype='TFSA', verbose='')
284
+ Definition:
285
+ Provides the account positions for the account related to account type
286
+ accounttype.
287
+ Paramaters:
288
+ - accounttype type of Questrade account.
289
+ Defaults to "tfsa".
290
+ - verbose level of verbosity represented by the number of characters in a string.
291
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
292
+ Returns:
293
+ Account positions as a Python object representation of the returned json.
294
+
295
+
296
+ get_accounts(self, verbose='')
297
+ Description:
298
+ Generator that provides the accounts.
299
+ Parameters:
300
+ - verbose level of verbosity represented by the number of characters in a string.
301
+ Defaults to empty string. Maximum verbosity is 1 or "v".
302
+ Yields:
303
+ All your Questrade accounts as a Python object representation of the returned
304
+ json.
305
+
306
+
307
+ get_market_candles(self, sid, interval, startdatetime=None, enddatetime=None,
308
+ verbose='')
309
+ Description:
310
+ Provides a list of json formatted market candles.
311
+ Parameters:
312
+ - sid symbol id as a string or numeral.
313
+ - interval is the Historical Data Granularity.
314
+ Examples: "OneMinute", "HalfHour", "OneYear".
315
+ - startdatetime datetime object representing the beginning of a range.
316
+ - enddatetime datetime object representing the end of a range.
317
+ Defaults to now if not specified.
318
+ - verbose level of verbosity represented by the number of characters in a string.
319
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
320
+ Returns:
321
+ Historical market data in the form of OHLC candlesticks for a specified symbol
322
+ as a Python object representation of the returned json.
323
+ This call is limited to returning 2,000 candlesticks in a single response.
324
+ sid is a symbol id.
325
+
326
+
327
+ get_market_quotes(self, ids, verbose='')
328
+ Definition:
329
+ Provides market quotes data.
330
+ Parameter:
331
+ - ids Internal symbol identifier. Could be a single value or a string of values.
332
+ - verbose level of verbosity represented by the number of characters in a string.
333
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
334
+ Returns:
335
+ A single Level 1 market data quote for one or more symbols in json string format
336
+ as a Python object representation of the returned json.
337
+ IMPORTANT NOTE: Questrade user needs to be subscribed to a real-time data
338
+ package, to receive market quotes in real-time, otherwise call to get quote
339
+ is considered snap quote and limit per market can be quickly reached.
340
+ Without real-time data package, once limit is reached, the response will return
341
+ delayed data. (Please check "delay" parameter in response always).
342
+
343
+
344
+ get_market_quotes_options(self, option_ids, filters=None, verbose='')
345
+ Definition:
346
+ Provides market quotes options.
347
+ Parameters:
348
+ - option_ids is a list of stock option ids.
349
+ - filters optional list of dictionary items.
350
+ - verbose level of verbosity represented by the number of characters in a string.
351
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
352
+ Returns:
353
+ A single Level 1 market data quote and Greek data for one or more option symbols
354
+ as a Python object representation of the returned json.
355
+
356
+
357
+ get_market_quotes_strategies(self, variants, verbose='')
358
+ Definition:
359
+ Provides a calculated L1 market data quote for a single or many multi-leg
360
+ strategies.
361
+ Parameter:
362
+ - variants is a list of dictionary items.
363
+ - verbose level of verbosity represented by the number of characters in a string.
364
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
365
+ Returns:
366
+ A calculated L1 market data quote for a single or many multi-leg strategies
367
+ as a Python object representation of the returned json.
368
+
369
+
370
+ get_markets(self, verbose='')
371
+ Description:
372
+ Provides market data.
373
+ Parameters:
374
+ - verbose level of verbosity represented by the number of characters in a string.
375
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
376
+ Returns:
377
+ Information about supported markets as a Python object representation of
378
+ the returned json.
379
+
380
+
381
+ get_new_refresh_token(self, token, server_type, verbose='')
382
+ Description:
383
+ Obtains a new refresh token (and new access token) from the API server.
384
+ You generally would not need to call this method, as it is potentially called by
385
+ Trader during initialization.
386
+ Trader will only call this method if the access token has expired.
387
+ Parameters:
388
+ - token the refresh token that is stored in the local file pointed to by rt_file.
389
+ - server_type should be "live" or "test" for your live Questrade account or
390
+ your test Questrade account, respectively.
391
+ - verbose level of verbosity represented by the number of characters in a string.
392
+ Defaults to empty string. Maximum verbosity is 1 or "v".
393
+
394
+
395
+ get_server_time(self, verbose='')
396
+ Description:
397
+ Provides the time from the Questrade API server.
398
+ Parameters:
399
+ - verbose level of verbosity represented by the number of characters in a string.
400
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
401
+ Returns:
402
+ The time on the server as a tuple made of a simple datetime object,
403
+ as well as in the expected Python object representation of the returned json.
404
+
405
+
406
+ get_symbol_options(self, sid, verbose='')
407
+ Definition:
408
+ Provides symbol options data.
409
+ Parameter:
410
+ - sid Internal symbol identifier.
411
+ - verbose level of verbosity represented by the number of characters in a string.
412
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
413
+ Returns:
414
+ An option chain for a particular underlying symbol as a Python object
415
+ representation of the returned json.
416
+
417
+
418
+ get_symbols_by_ids(self, ids, verbose='')
419
+ Definition:
420
+ Provides symbols data from symbol id(s).
421
+ Parameter:
422
+ - ids Internal symbol identifier(s). Could be a single numeric value or a string
423
+ of comma-seperated values (with no spaces).
424
+ - verbose level of verbosity represented by the number of characters in a string.
425
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
426
+ Returns:
427
+ Detailed information about one or more symbol as a Python object representation
428
+ of the returned json.
429
+
430
+
431
+ get_symbols_by_names(self, names, verbose='')
432
+ Definition:
433
+ Provides symbols data from name(s).
434
+ Parameter:
435
+ - names is a string of names seperated by commas (with no spaces).
436
+ - verbose level of verbosity represented by the number of characters in a string.
437
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
438
+ Returns:
439
+ Detailed information about one or more symbol as a Python object representation
440
+ of the returned json.
441
+
442
+
443
+ object_to_qdstr(self, dto, gmt=False)
444
+ Description:
445
+ Converts a datetime object to a Questrade datetime string.
446
+ Parameters:
447
+ - dto datetime object.
448
+ - gmt optional boolean indicating if datetime is Greenwich Mean Time.
449
+ Default value is False.
450
+ Returns:
451
+ The provided datetime object in Questrade API compatible string format.
452
+ Example: "2011-02-01T00:00:00-05:00".
453
+ If gmt is set to False, time will be in local time.
454
+ If gmt is True, the returned time will be considered as gmt time.
455
+
456
+
457
+ search_symbols(self, prefix, offset=0, verbose='')
458
+ Definition:
459
+ Provides symbol(s) data using several search criteria.
460
+ Parameters:
461
+ - prefix Prefix of a symbol or any word in the description.
462
+ - offset Offset in number of records from the beginning of a result set.
463
+ Default is not to offset.
464
+ - verbose level of verbosity represented by the number of characters in a string.
465
+ Defaults to empty string. Maximum verbosity is 2 or "vv".
466
+ Returns:
467
+ Symbol(s) data as a Python object representation of the returned json.
468
+
469
+
470
+ values_to_dobj(self, y, m, d, h=0, mi=0, s=0)
471
+ Description:
472
+ Helper method that builds a datetime object.
473
+ Parameters:
474
+ - y integer to be set as the year.
475
+ - m intiger to be set as the month.
476
+ - d integer to be set as the day.
477
+ - h optional integer to be set as the hour. Default value is 0.
478
+ - mi optional integer to be set as the minutes. Default value is 0.
479
+ - s optional integer to be set as the seconds. Default value is 0.
480
+ Returns:
481
+ A datetime object from the values provided as parameters.
482
+
483
+
484
+ values_to_qdstr(self, y, m, d, h=0, mi=0, s=0, gmt=False)
485
+ Description:
486
+ Helper method that builds a Questrade datetime string.
487
+ - y integer to be set as the year.
488
+ - m intiger to be set as the month.
489
+ - d integer to be set as the day.
490
+ - h optional integer to be set as the hour. Default value is 0.
491
+ - mi optional integer to be set as the minutes. Default value is 0.
492
+ - s optional integer to be set as the seconds. Default value is 0.
493
+ - gmt optional boolean indicating if datetime is Greenwich Mean Time.
494
+ Default value is False.
495
+ Returns:
496
+ The date time and timezone values provided as parameters in Questrade API
497
+ compatible string format.
498
+ Example: "2011-02-01T00:00:00-05:00".
499
+ If gmt is set to False, time will be in local time.
500
+ If gmt is True, the returned time will be considered as gmt time.
501
+ ```
502
+
503
+
504
+ Let me know if you have any questions: <kaiyoux@gmail.com>