forked from Himangshu4Das/tradestation-v3-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTradeStationClient.py
722 lines (569 loc) · 26 KB
/
TradeStationClient.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import requests
import http.server
import socketserver
import requests
import webbrowser
from datetime import datetime, timedelta
import json
"""
=== IMPLEMENTATION OF TRADESTATION'S VERSION3 ENDPOINTS USING AUTH0 KEYS ===
Only for HTTP requests.
It would be better to use the HTTP streams directly from main scripts.
Generate Auth0 Code using the Generate_AuthCode.py file first if refresh code has never been created.
Auth0 code and refresh code should only be generated once since post refresh token generation, the expiry of
refresh token is set infinite. Refresh code expiry time can be changed by contacting TradeStation support.
In case the expiry time of refresh is set ot a certain limit. You will have to generate auth code and refresh code post expiries."""
class CreateTSClient:
def __init__(self, key: str, secret: str, redirect_uri='http://localhost:3000/'):
"""
:param key: Your API client ID or key.
:param secret: Your API secret key.
:param redirect_uri: Specify a different port on localhost or custom redirect_uri if requested from TradeStation. Default port is 3000.
"""
self.key = key
self.secret = secret
self.redirect_uri = redirect_uri
self.token_url = 'https://signin.tradestation.com/oauth/token'
# load access token
with open('access_token.txt', 'r') as f:
self.access_token = f.readline().strip()
self.access_token_expiry = datetime.strptime(f.readline().strip(), '%Y-%m-%d %H:%M:%S')
# ===================================== LOGINs ==================================================
def fetch_refresh_token(self):
"""
Fetches the refresh token.
:return: refresh token and access token. Access token is ignored and recreated for simplicity.
"""
print('=' * 25)
# Read the authorization code from the text file
with open('auth_code.txt', 'r') as f:
auth_code = f.read()
print(auth_code)
# Send a POST request to the token endpoint to obtain a refresh token
response = requests.post(
self.token_url,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data={
'grant_type': 'authorization_code',
'code': auth_code,
'redirect_uri': self.redirect_uri,
'client_id': self.key,
'client_secret': self.secret
}
)
try:
ref_token = response.json()['refresh_token']
ref_ac_token = response.json()['access_token']
# Write the refresh token to a text file
with open('refresh_token.txt', 'w') as f:
f.write(ref_token)
# Write the access token to a text file
with open('refresh_access_token.txt', 'w') as f:
f.write(ref_ac_token)
# Print a success message
print(f'Refresh token saved to refresh_token.txt')
print(f'Refresh access token saved to refresh_access_token.txt')
# Return refresh token and access token along with it
return ref_token, ref_ac_token
except Exception as e:
print(e)
print('Unable to fetch refresh token!')
print('=' * 25)
def fetch_access_token(self):
"""
Get access token and expiry time of the same. Return it and save it in a text file.
:return: Access token and expiry time in datetime format.
"""
print('=' * 25)
# Read the authorization code from the text file
with open('refresh_token.txt', 'r') as f:
ref_token = f.read()
print(ref_token)
# Send a POST request to the token endpoint to obtain a refresh token
response = requests.post(
self.token_url,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data={
'grant_type': 'refresh_token',
'redirect_uri': self.redirect_uri,
'client_id': self.key,
'client_secret': self.secret,
'refresh_token': ref_token
}
)
self.access_token = response.json()['access_token']
self.access_token_expiry = datetime.now() + timedelta(seconds=response.json()['expires_in'] - 60)
self.access_token_expiry = self.access_token_expiry.strftime('%Y-%m-%d %H:%M:%S')
# Write the access token and expiry time to a text file
with open('access_token.txt', 'w') as f:
f.write(self.access_token)
f.write('\n')
f.write(self.access_token_expiry)
# Print a success message
print(f'Access token saved to access_token.txt')
print(self.access_token_expiry)
self.access_token_expiry = datetime.strptime(self.access_token_expiry, '%Y-%m-%d %H:%M:%S')
print('=' * 25)
return self.access_token, self.access_token_expiry
def get_saved_access_token(self):
"""
Get previously stored access token and expiry datetime
:return:
"""
with open('access_token.txt', 'r') as f:
self.access_token = f.readline().strip()
self.access_token_expiry = f.readline().strip()
return self.access_token, self.access_token_expiry
# ======================================= MARKET DATA =======================================================
def fetch_bars(self, symbol):
"""
Get market data bars for specified ticker/ symbol
eg. AAPL
"""
bar_url = "https://api.tradestation.com/v3/marketdata/barcharts/{}".format(symbol)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", bar_url, headers=headers)
return response.text
def fetch_symbol_details(self, symbols):
"""
Fetch details for the specified symbol or symbols.
The symbol or symbols should be in a list.
You can get details for a maximum of 50 symbols at a time.
:param symbols: list or string of symbol or symbols.
:return: Symbol details.
"""
if isinstance(symbols, str):
symbols = [symbols]
sd_url = "https://api.tradestation.com/v3/marketdata/symbols/{}"
symbol_str = ",".join(symbols)
sd_url = sd_url.format(symbol_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", sd_url, headers=headers)
return response.text
def fetch_interests(self):
"""
:return: Return interest rates of cryptocurrencies
"""
url = "https://api.tradestation.com/v3/marketdata/crypto/interestrates"
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", url, headers=headers)
return response.text
def fetch_opt_expirations(self, symbol):
"""
Get expiration dates for specified underlying symbol.
"""
bar_url = "https://api.tradestation.com/v3/marketdata/options/expirations/{}".format(symbol)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", bar_url, headers=headers)
return response.text
def fetch_opt_risk_reward(self, payload):
"""
:param payload: Spread details with same strictly expirations.
:return: Risk reward information such as:
MaxGainIsInfinite, AdjustedMaxGain, MaxLossIsInfinite, AdjustedMaxLoss, BreakevenPoints
payload example:
payload = {
"SpreadPrice": 0,
"Legs": [
{
"Symbol": "string",
"Quantity": 0,
"TradeAction": "BUY"
}
]
}
"""
risk_reward_url = "https://api.tradestation.com/v3/marketdata/options/riskreward"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("POST", risk_reward_url, json=payload, headers=headers)
return response.text
def fetch_opt_strikes(self, symbol):
"""
Fetch strike prices for an underlying.
:return: collection of strikes.
"""
bar_url = "https://api.tradestation.com/v3/marketdata/options/strikes/{}".format(symbol)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", bar_url, headers=headers)
return response.text
def fetch_spread_types(self):
"""
Fetch spread types.
"""
st_url = "https://api.tradestation.com/v3/marketdata/options/spreadtypes"
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", st_url, headers=headers)
return response.text
def fetch_quotes(self, symbols):
"""
Get quotes for specified symbols.
The symbol or symbols should be in a list or string(allowed for single symbol).
You can get details for a maximum of 50 symbols at a time.
:param symbols: list or string of symbol or symbols.
:return: Symbol details.
"""
if isinstance(symbols, str):
symbols = [symbols]
q_url = "https://api.tradestation.com/v3/marketdata/quotes/{}"
symbol_str = ",".join(symbols)
q_url = q_url.format(symbol_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", q_url, headers=headers)
return response.text
# =========================================== BROKERAGE ===================================================
def fetch_accounts(self, sim=True):
"""
Fetch listed accounts.
"""
if sim:
acc_url = "https://sim-api.tradestation.com/v3/brokerage/accounts"
else:
acc_url = "https://api.tradestation.com/v3/brokerage/accounts"
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", acc_url, headers=headers)
return response.text
def fetch_balances(self, accounts, sim=True):
"""
Fetch account balances for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param sim: Sim set to True will access the simulator endpoints.
:return: Account balances.
"""
if sim:
bal_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/balances"
else:
bal_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/balances"
if isinstance(accounts, str):
accounts = [accounts]
accounts_str = ", ".join(accounts)
bal_url = bal_url.format(accounts_str)
print(bal_url)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", bal_url, headers=headers, timeout=20)
return response.text
def fetch_bod_balances(self, accounts, sim=True):
"""
Fetch beginning of day balances for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param sim: Sim set to True will access the simulator endpoints.
:return: Account balances.
"""
if sim:
bod_bal_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/bodbalances"
else:
bod_bal_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/bodbalances"
if isinstance(accounts, str):
accounts = [accounts]
accounts_str = ", ".join(accounts)
bod_bal_url = bod_bal_url.format(accounts_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", bod_bal_url, headers=headers, timeout=20)
return response.text
def fetch_hist_orders(self, accounts, since_date, sim=True):
"""
Fetch orders for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param since_date: starting date for historical orders(string only with format YYYY-mm-dd).
:param sim: Sim set to True will access the simulator endpoints.
:return: Historical orders for given accounts and time range.
"""
if sim:
hist_orders_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/historicalorders"
else:
hist_orders_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/historicalorders"
if isinstance(accounts, str):
accounts = [accounts]
query = {"since": since_date}
accounts_str = ", ".join(accounts)
hist_orders_url = hist_orders_url.format(accounts_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", hist_orders_url, headers=headers, params=query)
return response.text
def fetch_hist_orders_by_oid(self, accounts, since_date, o_id, sim=True):
"""
Fetch order details for specified order IDs for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param since_date: starting date for historical orders(string only with format YYYY-mm-dd).
:param sim: Sim set to True will access the simulator endpoints.
:param o_id: list of order ID/s.
:return: Historical orders for given accounts and time range.
"""
if sim:
hist_orders_oid_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/historicalorders/{}"
else:
hist_orders_oid_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/historicalorders/{}"
if isinstance(accounts, str):
accounts = [accounts]
if isinstance(o_id, str):
o_id = [o_id]
query = {"since": since_date}
accounts_str = ", ".join(accounts)
o_id_str = ", ".join(o_id)
hist_orders_oid_url = hist_orders_oid_url.format(accounts_str, o_id_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", hist_orders_oid_url, headers=headers, params=query)
return response.text
def fetch_positions(self, account, sim=True):
"""
Fetch placed positions of the account
:param account: Account number
:param sim: Set to True to use the simulation account
:return: Placed Positions
"""
if sim:
pos_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/positions".format(account)
else:
pos_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/positions".format(account)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", pos_url, headers=headers, timeout=20)
return response.text
def fetch_orders(self, accounts, sim=True):
"""
Fetch account orders for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param sim: Sim set to True will access the simulator endpoints.
:return: Orders for given accounts.
"""
if sim:
orders_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/orders"
else:
orders_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/orders"
if isinstance(accounts, str):
accounts = [accounts]
accounts_str = ", ".join(accounts)
orders_url = orders_url.format(accounts_str)
print(orders_url)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", orders_url, headers=headers, timeout=20)
return response.text
def fetch_orders_by_oid(self, accounts, o_id, sim=True):
"""
Fetch order details for specified order IDs for one or multiple accounts if passed as list.
:param accounts: account in string or accounts in list.
:param sim: Sim set to True will access the simulator endpoints.
:param o_id: list of order ID/s.
:return: Orders for given accounts and order IDs.
"""
if sim:
orders_oid_url = "https://sim-api.tradestation.com/v3/brokerage/accounts/{}/orders/{}"
else:
orders_oid_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/orders/{}"
if isinstance(accounts, str):
accounts = [accounts]
if isinstance(o_id, str):
o_id = [o_id]
accounts_str = ", ".join(accounts)
o_id_str = ", ".join(o_id)
orders_oid_url = orders_oid_url.format(accounts_str, o_id_str)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", orders_oid_url, headers=headers)
return response.text
def get_crypto_wallets(self, crypto_account):
"""
Fetch information for specified cryptocurrency wallet.
:param crypto_account: A Cryptocurrency wallet account ID.
:return: Wallet information.
"""
wallet_url = "https://api.tradestation.com/v3/brokerage/accounts/{}/wallets".format(crypto_account)
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", wallet_url, headers=headers)
return response.text
# =========================================== EXECUTION ===================================================
def confirm_order(self, payload):
"""
IMPORTANT : confirm_order only returns the estimated information for the intended order without it being placed.
payload example:
payload = {
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "Market",
"TradeAction": "BUY",
"TimeInForce": {"Duration": "DAY"},
"Route": "Intelligent"
}
:param payload: Pass a dictionary imitating the order you wish to place. This will only estimate pricing and
info. and not place any order. :return: Order estimated pricing, commision and other information.
"""
confirm_url = "https://api.tradestation.com/v3/orderexecution/orderconfirm"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("POST", confirm_url, json=payload, headers=headers)
return response.text
def confirm_group_order(self, payload):
"""
IMPORTANT : confirm_group_order only returns the estimated information for the intended order without it
being placed. Fetch estimated information for a group of orders.
payload example:
payload = {
"Type": "OCO",
"Orders": [
{
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "Limit",
"TradeAction": "BUY",
"LimitPrice": "230.00",
"Route": "Intelligent",
"TimeInForce": {"Duration": "DAY"}
},
{
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "StopMarket",
"TradeAction": "BUY",
"Route": "Intelligent",
"TimeInForce": {"Duration": "DAY"},
"AdvancedOptions": {"TrailingStop": {"Percent": "5.0"}}
}
]
}
Type : Order types can be BRK, OCO and NORMAL
Order Cancels Order (OCO) An OCO order is a group of orders whereby if one of the orders is filled or
partially-filled, then all the other orders in the group are cancelled.
Bracket OCO Orders A bracket order is a special instance of an OCO (Order Cancel Order). Bracket orders are
used to exit an existing position. They are designed to limit loss and lock in profit by “bracketing” an
order with a simultaneous stop and limit order.
:param payload: Pass a dictionary imitating the group of orders you wish to place. This will only estimate
pricing and information and won't place any order. :return: Order estimated pricing, commision and other
information.
"""
confirm_group_url = "https://api.tradestation.com/v3/orderexecution/orderconfirm"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("POST", confirm_group_url, json=payload, headers=headers)
return response.text
def place_orders(self, payload, sim=True):
"""
payload example:
payload = {
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "Market",
"TradeAction": "BUY",
"TimeInForce": {"Duration": "DAY"},
"Route": "Intelligent"
}
:param sim: Set to true if the orders are to be placed in simulation account.
:param payload: Order details to be placed.
:return: Response.
"""
if sim:
place_order_url = "https://sim-api.tradestation.com/v3/orderexecution/orders"
else:
place_order_url = "https://api.tradestation.com/v3/orderexecution/orders"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("POST", place_order_url, json=payload, headers=headers)
return response.text
def place_group_orders(self, payload, sim=True):
"""
payload example:
payload = {
"Type": "OCO",
"Orders": [
{
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "Limit",
"TradeAction": "BUY",
"LimitPrice": "230.00",
"Route": "Intelligent",
"TimeInForce": {"Duration": "DAY"}
},
{
"AccountID": "123456782",
"Symbol": "MSFT",
"Quantity": "10",
"OrderType": "StopMarket",
"TradeAction": "BUY",
"Route": "Intelligent",
"TimeInForce": {"Duration": "DAY"},
"AdvancedOptions": {"TrailingStop": {"Percent": "5.0"}}
}
]
}
:param sim: Set to true if the orders are to be placed in simulation account.
:param payload: Group order details to be placed.
:return: Response.
"""
if sim:
place_group_order_url = "https://sim-api.tradestation.com/v3/orderexecution/ordergroups"
else:
place_group_order_url = "https://api.tradestation.com/v3/orderexecution/ordergroups"
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("POST", place_group_order_url, json=payload, headers=headers)
return response.text
def replace_order(self, o_id, payload, sim=True):
"""
Modify an active order that is not yet filled.
payload example:
payload = {
"Quantity": "10",
"OrderType": "Limit",
"LimitPrice": "132.52"
}
:param o_id: OrderID you wish to update.
:param payload: Changes for update ( Quantity, OrderType and LimitPrice)
:param sim: Set to true if you need to work on simulation account.
:return: Response.
"""
if sim:
replace_url = "https://sim-api.tradestation.com/v3/orderexecution/orders/{}".format(o_id)
else:
replace_url = "https://api.tradestation.com/v3/orderexecution/orders/{}".format(o_id)
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("PUT", replace_url, json=payload, headers=headers)
return response.text
def cancel_order(self, o_id, sim=True):
"""
Cancel an active order that is not yet filled.
:param o_id: OrderID you wish to cancel.
:param sim: Set to true if you need to work on simulation account.
:return: Response.
"""
if sim:
cancel_url = "https://sim-api.tradestation.com/v3/orderexecution/orders/{}".format(o_id)
else:
cancel_url = "https://api.tradestation.com/v3/orderexecution/orders/{}".format(o_id)
headers = {
"content-type": "application/json",
"Authorization": f"Bearer {self.access_token}"
}
response = requests.request("DELETE", cancel_url, headers=headers)
return response.text
def fetch_activation_triggers(self):
"""
:return: List of valid activation trigger methods.
"""
act_url = "https://api.tradestation.com/v3/orderexecution/activationtriggers"
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", act_url, headers=headers)
return response.text
def fetch_routes(self):
"""
:return: Fetch a list of available routes for placing an order.
"""
routes_url = "https://api.tradestation.com/v3/orderexecution/routes"
headers = {"Authorization": f"Bearer {self.access_token}"}
response = requests.request("GET", routes_url, headers=headers)
return response.text