Skip to content

Commit

Permalink
Merge pull request #256 from NelsonDane/deepsource-transform-49061c92
Browse files Browse the repository at this point in the history
style: format code with Black and isort
  • Loading branch information
NelsonDane authored Jun 10, 2024
2 parents de11c7e + b64e288 commit 23162e7
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions vanguardAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import pprint
import traceback

from dotenv import load_dotenv
from vanguard import account as vg_account
from vanguard import order, session
from dotenv import load_dotenv

from helperAPI import (
Brokerage,
Expand All @@ -20,18 +20,14 @@
)


def vanguard_run(
orderObj: stockOrder, command=None, botObj=None, loop=None
):
def vanguard_run(orderObj: stockOrder, command=None, botObj=None, loop=None):
# Initialize .env file
load_dotenv()
# Import Vanguard account
if not os.getenv("VANGUARD"):
print("Vanguard not found, skipping...")
return None
accounts = (
os.environ["VANGUARD"].strip().split(",")
)
accounts = os.environ["VANGUARD"].strip().split(",")
# Set the functions to be run
_, second_command = command

Expand Down Expand Up @@ -64,7 +60,7 @@ def vanguard_init(account, index, botObj=None, loop=None):
title=f"Vanguard_{index}",
headless=True,
profile_path="./creds",
debug=debug
debug=debug,
)
need_second = vg_session.login(account[0], account[1], account[2])
if need_second:
Expand All @@ -75,7 +71,9 @@ def vanguard_init(account, index, botObj=None, loop=None):
getOTPCodeDiscord(botObj, name, timeout=120, loop=loop), loop
).result()
if sms_code is None:
raise Exception(f"Vanguard {index} code not received in time...", loop)
raise Exception(
f"Vanguard {index} code not received in time...", loop
)
vg_session.login_two(sms_code)
all_accounts = vg_account.AllAccount(vg_session)
success = all_accounts.get_account_ids()
Expand All @@ -84,9 +82,11 @@ def vanguard_init(account, index, botObj=None, loop=None):
print("Logged in to Vanguard!")
vanguard_obj.set_logged_in_object(name, vg_session)
print_accounts = []
for acct in all_accounts.account_totals.keys():
for acct in all_accounts.account_totals:
vanguard_obj.set_account_number(name, acct)
vanguard_obj.set_account_totals(name, acct, all_accounts.account_totals[acct])
vanguard_obj.set_account_totals(
name, acct, all_accounts.account_totals[acct]
)
print_accounts.append(acct)
print(f"The following Vanguard accounts were found: {print_accounts}")
except Exception as e:
Expand All @@ -107,11 +107,19 @@ def vanguard_holdings(vanguard_o: Brokerage, loop=None):
raise Exception("Error getting account details")
success = all_accounts.get_holdings()
if success:
for account in all_accounts.accounts_positions.keys():
for type in all_accounts.accounts_positions[account].keys():
for stock in all_accounts.accounts_positions[account][type]:
for account in all_accounts.accounts_positions:
for account_type in all_accounts.accounts_positions[account].keys():
for stock in all_accounts.accounts_positions[account][
account_type
]:
if float(stock["quantity"]) != 0:
vanguard_o.set_holdings(key, account, stock["symbol"], stock["quantity"], stock["price"])
vanguard_o.set_holdings(
key,
account,
stock["symbol"],
stock["quantity"],
stock["price"],
)
else:
raise Exception("Vanguard-api failed to retrieve holdings.")
except Exception as e:
Expand Down Expand Up @@ -161,16 +169,21 @@ def vanguard_transaction(vanguard_o: Brokerage, orderObj: stockOrder, loop=None)
dry_run=orderObj.get_dry(),
)
print("The order verification produced the following messages: ")
if messages["ORDER CONFIRMATION"] == "No order confirmation page found. Order Failed.":
printAndDiscord("Market order failed placing limit order.", loop)
if (
messages["ORDER CONFIRMATION"]
== "No order confirmation page found. Order Failed."
):
printAndDiscord(
"Market order failed placing limit order.", loop
)
price_type = order.PriceType.LIMIT
price = vg_order.get_quote(s) + 0.01
messages = vg_order.place_order(
account_id=account,
quantity=int(orderObj.get_amount()),
price_type=price_type,
symbol=s,
duration=order.Duration.DAY,
quantity=int(orderObj.get_amount()),
price_type=price_type,
symbol=s,
duration=order.Duration.DAY,
order_type=order_type,
limit_price=price,
dry_run=orderObj.get_dry(),
Expand All @@ -191,8 +204,8 @@ def vanguard_transaction(vanguard_o: Brokerage, orderObj: stockOrder, loop=None)
loop,
)
if (
not messages["ORDER INVALID"]
== "No invalid order message found."
messages["ORDER INVALID"]
!= "No invalid order message found."
):
printAndDiscord(
f"{key} account {print_account}: The order verification produced the following messages: {messages['ORDER INVALID']}",
Expand All @@ -217,19 +230,21 @@ def vanguard_transaction(vanguard_o: Brokerage, orderObj: stockOrder, loop=None)
loop,
)
if (
not messages["ORDER INVALID"]
== "No invalid order message found."
messages["ORDER INVALID"]
!= "No invalid order message found."
):
printAndDiscord(
f"{key} account {print_account}: The order verification produced the following messages: {messages['ORDER INVALID']}",
loop,
)
except Exception as e:
printAndDiscord(f"{key} {print_account}: Error submitting order: {e}", loop)
printAndDiscord(
f"{key} {print_account}: Error submitting order: {e}", loop
)
print(traceback.format_exc())
continue
obj.close_browser()
printAndDiscord(
"All Vanguard transactions complete",
loop,
)
)

0 comments on commit 23162e7

Please sign in to comment.