Skip to content

Commit

Permalink
Fixed the buy and sell function
Browse files Browse the repository at this point in the history
buy function should now be able to buy when and when no shares a present and select market order

sell order should not spit out a HUGE error to the discord when no shares are present to sell
  • Loading branch information
ImNotOssy committed Sep 5, 2024
1 parent 8cd8107 commit aa0c1d6
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions tornadoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,27 +337,29 @@ def handle_buy(driver, stock, orderObj, loop):
printAndDiscord(f"Tornado failed to enter quantity for {stock}.", loop)
return

# Now check for current shares and adjust XPaths accordingly
try:
has_current_shares = True
current_shares_element = driver.find_element(By.XPATH, '//*[@id="main-router"]/div[1]/div/div[4]/div')
if "sh" in current_shares_element.text.strip():
has_current_shares = True
else:
has_current_shares = False
except NoSuchElementException:
tornado_error(driver, loop)
has_current_shares = False

market_order_xpath = (
'//*[@id="main-router"]/div[1]/div/div[5]/select'
'//*[@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'
else '//*[@id="main-router"]/div[1]/div/div[4]/select/option[1]'
)
current_price_xpath = (
'//*[@id="main-router"]/div[1]/div/div[6]/div'
'//*[@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'
else '//*[@id="main-router"]/div[1]/div/div[5]/div[contains(text(), "$")]'
)
buy_power_xpath = (
'//*[@id="main-router"]/div[1]/div/div[8]/div'
'//*[@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'
else '//*[@id="main-router"]/div[1]/div/div[7]/div[contains(text(), "$")]'
)

try:
Expand All @@ -374,9 +376,21 @@ def handle_buy(driver, stock, orderObj, loop):
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(",", ""))
cost_float = float(cost.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(
Expand Down Expand Up @@ -448,7 +462,6 @@ def handle_sell(driver, stock, orderObj, loop):
)
current_shares = float(current_shares_element.text.strip().replace(" sh", ""))
except NoSuchElementException:
tornado_error(driver, loop)
printAndDiscord(f"Tornado no current shares to sell for {stock}.", loop)
return

Expand Down

0 comments on commit aa0c1d6

Please sign in to comment.