forked from NelsonDane/auto-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtornadoAPI.py
544 lines (469 loc) · 19.1 KB
/
tornadoAPI.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
import datetime
import os
import traceback
from time import sleep
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from helperAPI import (
Brokerage,
check_if_page_loaded,
getDriver,
killSeleniumDriver,
printAndDiscord,
printHoldings,
stockOrder,
)
load_dotenv()
def tornado_error(driver, loop=None):
driver.save_screenshot(f"Tornado-error-{datetime.datetime.now()}.png")
printAndDiscord(f"Tornado Error: {traceback.format_exc()}", loop, embed=False)
def tornado_init(TORNADO_EXTERNAL=None, DOCKER=False, loop=None):
load_dotenv()
if not os.getenv("TORNADO") and TORNADO_EXTERNAL is None:
print("Tornado not found, skipping...")
return None
if TORNADO_EXTERNAL is None:
accounts = os.environ["TORNADO"].strip().split(",")
else:
accounts = TORNADO_EXTERNAL.strip().split(",")
TORNADO_obj = Brokerage("Tornado")
for index, account in enumerate(accounts):
account_name = f"Tornado {index + 1}"
try:
driver = getDriver(DOCKER)
if driver is None:
raise Exception("Driver not found.")
driver.get("https://tornado.com/app/login")
WebDriverWait(driver, 30).until(check_if_page_loaded)
# Log in with email and password
try:
email_field = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.ID, "email-field"))
)
email_field.send_keys(account.split(":")[0])
password_field = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.ID, "password-field"))
)
password_field.send_keys(account.split(":")[1])
login_button = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable(
(
By.CSS_SELECTOR,
"#root > div > div > div > div:nth-child(2) > div > div > div > div > form > div.sc-WZYut.ZaYjk > button",
)
)
)
login_button.click()
# Check for the element after logging in to ensure the page is fully loaded
WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(
By.XPATH,
"//*[@id='main-router']/div/div/div/div[1]/div/div/div/div[1]/div[1]/div/span",
)
)
)
TORNADO_obj.set_logged_in_object(account_name, driver)
# Set the account name
TORNADO_obj.set_account_number(account_name, account_name)
except TimeoutException:
printAndDiscord(
f"TimeoutException: Login failed for {account_name}.", loop
)
return False
except Exception:
tornado_error(driver, loop)
driver.close()
driver.quit()
return None
return TORNADO_obj
def tornado_extract_holdings(driver):
holdings_data = []
try:
# Locate all the individual stock holdings elements
holdings_elements = driver.find_elements(
By.XPATH, ".//div[@class='sc-jEWLvH evXkie']"
)
if len(holdings_elements) == 0:
return holdings_data
for holding_element in holdings_elements:
try:
# Extract the stock ticker
stock_ticker = holding_element.find_element(
By.XPATH, ".//a[1]/div[1]/span"
).text.strip()
# Extract the number of shares available
shares = holding_element.find_element(
By.XPATH, ".//a[4]/div/div/span/span"
).text.strip()
shares_float = float(shares.replace(" sh", ""))
# Corrected XPath to extract the actual stock price
price = holding_element.find_element(
By.XPATH, ".//a[1]/div[3]/span/div/div[1]/span"
).text.strip()
price_float = float(price.replace("$", "").replace(",", ""))
# Store the extracted data in a dictionary
holdings_data.append(
{
"stock_ticker": stock_ticker,
"shares": shares_float,
"price": price_float,
}
)
except Exception:
tornado_error(driver)
continue
except Exception:
tornado_error(driver)
return []
return holdings_data
def tornado_holdings(Tornado_o: Brokerage, loop=None):
try:
# Ensure we are using the correct account name
account_names = Tornado_o.get_account_numbers()
for account_name in account_names:
driver: webdriver = Tornado_o.get_logged_in_objects(account_name)
print(f"Processing holdings for {account_name}")
# Fetch the total account value
account_value_element = WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(
By.XPATH,
"//*[@id='main-router']/div/div/div/div[1]/div/div/div[1]/div[1]/div[1]/div/span",
)
)
)
account_value = account_value_element.text.strip()
account_value_float = float(account_value.replace("$", "").replace(",", ""))
# Extract holdings data
holdings_data = tornado_extract_holdings(driver)
if not holdings_data:
print(f"No holdings found for {account_name}. Skipping account.")
continue # Skip to the next account
for holding in holdings_data:
Tornado_o.set_holdings(
account_name,
account_name,
holding["stock_ticker"],
holding["shares"],
holding["price"],
)
# Set the account total using the fetched account value
Tornado_o.set_account_totals(
account_name, account_name, account_value_float
)
except Exception as e:
tornado_error(driver, loop)
printAndDiscord(f"Tornado Account: Error processing holdings: {e}", loop)
printHoldings(Tornado_o, loop) # Send the holdings to Discord
killSeleniumDriver(Tornado_o) # Close the browser after processing
def tornado_transaction(Tornado_o: Brokerage, orderObj: stockOrder, loop=None):
print("\n==============================")
print("Tornado")
print("==============================\n")
for s in orderObj.get_stocks():
for key in Tornado_o.get_account_numbers():
driver = Tornado_o.get_logged_in_objects(key)
# Ensure we are on the Tornado dashboard or navigate to it
try:
current_url = driver.current_url
if "app" not in current_url:
driver.get("https://tornado.com/app/")
WebDriverWait(driver, 30).until(check_if_page_loaded)
except Exception as e:
tornado_error(driver, loop)
printAndDiscord(f"Failed to navigate to dashboard for {key}: {e}", loop)
continue
try:
# Interact with the search bar
search_field = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "#nav_securities_search")
)
)
search_field.click()
sleep(1)
search_field.send_keys(s)
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado search field not found for {s}.", loop)
continue
try:
# Wait for and process search results
WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located(
(
By.XPATH,
'//*[@id="nav_securities_search_container"]/div[2]/ul/li',
)
)
)
dropdown_items = driver.find_elements(
By.XPATH, '//*[@id="nav_securities_search_container"]/div[2]/ul/li'
)
total_items = len(dropdown_items)
sleep(2)
if total_items == 0:
printAndDiscord(f"Tornado doesn't have {s}.", loop)
continue
found_stock = False
for item in dropdown_items:
ticker_name = item.find_element(By.CLASS_NAME, "bold").text.strip()
if ticker_name == s:
found_stock = True
sleep(1)
item.click()
break
if not found_stock:
printAndDiscord(f"Tornado doesn't have {s}.", loop)
continue
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado search results did not appear for {s}.", loop)
continue
# Proceed with the transaction based on the action (buy/sell)
if orderObj.get_action() == "buy":
handle_buy(driver, s, orderObj, loop)
elif orderObj.get_action() == "sell":
handle_sell(driver, s, orderObj, loop)
# Ensure to return to the dashboard after every transaction
try:
dashboard_link = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable(
(
By.XPATH,
'//*[@id="root"]/div/div/div[4]/div/div[1]/div/div/div[2]/div[1]/div[2]/span[1]/a/span',
)
)
)
dashboard_link.click()
WebDriverWait(driver, 60).until(
EC.presence_of_element_located(
(
By.XPATH,
'//*[@id="main-router"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div/span',
)
)
)
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(
f"Tornado failed to return to dashboard after processing {s}.", loop
)
print("Completed all transactions, Exiting...")
killSeleniumDriver(Tornado_o)
def handle_buy(driver, stock, orderObj, loop):
DRY = orderObj.get_dry()
QUANTITY = orderObj.get_amount()
print("DRY MODE:", DRY)
try:
buy_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="buy-button"]'))
)
driver.execute_script("arguments[0].click();", buy_button)
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado buy button not found for {stock}.", loop)
return
try:
quant = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[3]/input')
)
)
quant.clear()
quant.send_keys(str(QUANTITY))
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado failed to enter quantity for {stock}.", loop)
return
try:
current_shares_element = driver.find_element(
By.XPATH, '//*[@id="main-router"]/div[1]/div/div[4]/div'
)
has_current_shares = bool("sh" in current_shares_element.text.strip())
except NoSuchElementException:
has_current_shares = False
market_order_xpath = (
'//*[@id="main-router"]/div[1]/div/div[5]/select/option[1]'
if has_current_shares
else '//*[@id="main-router"]/div[1]/div/div[4]/select/option[1]'
)
current_price_xpath = (
'//*[@id="main-router"]/div[1]/div/div[6]/div[contains(text(), "$")]'
if has_current_shares
else '//*[@id="main-router"]/div[1]/div/div[5]/div[contains(text(), "$")]'
)
buy_power_xpath = (
'//*[@id="main-router"]/div[1]/div/div[8]/div[contains(text(), "$")]'
if has_current_shares
else '//*[@id="main-router"]/div[1]/div/div[7]/div[contains(text(), "$")]'
)
try:
market_order_option = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, market_order_xpath))
)
market_order_option.click()
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado failed to select market order for {stock}.", loop)
return
try:
sleep(3)
buy_power = driver.find_element(By.XPATH, buy_power_xpath).text.strip()
cost = driver.find_element(By.XPATH, current_price_xpath).text.strip()
# Validate and convert buy power
buy_power_float = float(buy_power.replace("$", "").replace(",", ""))
# Validate and convert cost, ensuring it's a valid price
if "$" in cost:
try:
cost_float = float(cost.replace("$", "").replace(",", ""))
except ValueError:
printAndDiscord(
f"Tornado: Invalid price format for {stock}: {cost}", loop
)
return
else:
printAndDiscord(
f"Tornado: Price not available or in an unexpected format for {stock}: {cost}",
loop,
)
return
# Check if the available buying power is enough
if buy_power_float < cost_float:
tornado_error(driver, loop)
printAndDiscord(
f"Tornado insufficient funds to buy {stock}. Required: ${cost_float}, Available: ${buy_power_float}",
loop,
)
return
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(
f"Tornado failed to fetch buying power or cost for {stock}.", loop
)
return
if not DRY:
try:
submit_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(
By.XPATH,
'//*[@id="main-router"]/div[1]/div/div[10]/div/button | //*[@id="main-router"]/div[1]/div/div[9]/div/button',
)
)
)
submit_button.click()
printAndDiscord(
f"Tornado account: buy {QUANTITY} shares of {stock} at {cost}", loop
)
# Click the "Continue" button after placing the order
continue_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[2]/div/button')
)
)
continue_button.click()
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(
f"Tornado failed to submit buy order for {stock} or click Continue.",
loop,
)
else:
sleep(5)
printAndDiscord(
f"DRY MODE: Simulated order BUY for {QUANTITY} shares of {stock} at {cost}",
loop,
)
def handle_sell(driver, stock, orderObj, loop):
DRY = orderObj.get_dry()
QUANTITY = orderObj.get_amount()
try:
sell_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="sell-button"]'))
)
driver.execute_script("arguments[0].click();", sell_button)
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado sell button not found for {stock}.", loop)
return
try:
current_shares_element = driver.find_element(
By.XPATH, '//*[@id="main-router"]/div[1]/div/div[4]/div'
)
current_shares = float(current_shares_element.text.strip().replace(" sh", ""))
except NoSuchElementException:
printAndDiscord(f"Tornado no current shares to sell for {stock}.", loop)
return
if QUANTITY > current_shares:
printAndDiscord(
f"Tornado not enough shares to sell {stock}. Available: {current_shares}",
loop,
)
return
try:
quant = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[3]/input')
)
)
quant.clear()
quant.send_keys(str(QUANTITY))
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado failed to enter quantity for {stock}.", loop)
return
try:
market_order_option = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[6]/select/option[1]')
)
)
market_order_option.click()
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado failed to select market order for {stock}.", loop)
return
try:
sell_price = driver.find_element(
By.XPATH, '//*[@id="main-router"]/div[1]/div/div[7]/div'
).text.strip()
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado failed to fetch sell price for {stock}.", loop)
return
if not DRY:
try:
submit_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[11]/div/button')
)
)
submit_button.click()
printAndDiscord(
f"Tornado account: sell {QUANTITY} shares of {stock} at {sell_price}",
loop,
)
# Click the "Continue" button after placing the order
continue_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(
(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[2]/div/button')
)
)
continue_button.click()
except TimeoutException:
tornado_error(driver, loop)
printAndDiscord(
f"Tornado failed to submit sell order for {stock} or click Continue.",
loop,
)
else:
printAndDiscord(
f"DRY MODE: Simulated order SELL for {QUANTITY} shares of {stock} at {sell_price}",
loop,
)