Last active
July 28, 2020 02:34
-
-
Save mistermichaelll/a41a6020a43a4c865d449d0f53073825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ================================================ | |
# Export portfolio information using Selenium & R | |
# ================================================ | |
# note that apex_information.py is a separate script that you need | |
# to run this one. I've uploaded that (sans account information) here: | |
# https://gist.github.com/mistermichaelll/7d6cb085000adf8c33087df3ae1b3a40 | |
# libraries utilized | |
# & APEX information script | |
# ------------------------- | |
from selenium import webdriver | |
from time import sleep | |
from shutil import move | |
from subprocess import call | |
import apex_information | |
import os | |
# preliminary information | |
# ----------------------- | |
home_directory = os.path.expanduser('~') | |
destination_directory = home_directory + "/Documents/M1 Portfolio/" | |
download_directory = home_directory + "/Downloads/" | |
apex_user = apex_information.user | |
apex_pass = apex_information.password | |
file_names = ["positions-export.csv", "activity-export.csv", "realized.csv", "unrealized.csv"] | |
# info to call the R script from this one | |
# --------------------------------------- | |
R_location = "/Library/Frameworks/R.framework/Versions/3.6/Resources/Rscript" | |
R_script = destination_directory + "portfolio_analyze.R" | |
# function that logs into APEX | |
# ---------------------------- | |
def apex_login(apex_username, apex_password): | |
print("Logging in...") | |
# had to adjust for this first step that APEX added... | |
# basically they ask for your username first. | |
initial_login = driver.find_element_by_name("username") | |
initial_login.clear() | |
initial_login.send_keys(apex_username) | |
initial_login.find_element_by_xpath(apex_information.initial_login_xpath).click() | |
sleep(4) | |
# main login | |
user_login = driver.find_element_by_xpath(apex_information.username_xpath) | |
user_login.clear() | |
user_login.send_keys(apex_username) | |
sleep(3) | |
# put in password | |
user_pass = driver.find_element_by_xpath(apex_information.password_xpath) | |
user_pass.clear() | |
user_pass.send_keys(apex_password) | |
# actually log in | |
sleep(3) | |
driver.find_element_by_xpath(apex_information.login_button_xpath).click() | |
# function to grab csv positions | |
# ------------------------------ | |
def positions_export(): | |
print("Getting current positions...") | |
# open the positions page | |
driver.get(apex_information.positions_export_url) | |
# wait a sec so button is available | |
sleep(4) | |
# press button | |
driver.find_element_by_xpath('/html/body/div[2]/div/div/section/div[2]/div[2]/div/ui-grid-export-tools/div/export-ui-grid-csv/button').click() | |
sleep(1) | |
# export activities | |
# ----------------- | |
def activity_export(): | |
print("Exporting activites...") | |
# open the positions page | |
driver.get(apex_information.activity_export_url) | |
# wait a sec so button is available | |
sleep(4) | |
# press button | |
driver.find_element_by_xpath('/html/body/div[2]/div/div/section/div[2]/div[2]/div/ui-grid-export-tools/div/export-ui-grid-csv/button').click() | |
sleep(1) | |
# get cost basis | |
# -------------- | |
def get_cost_basis(): | |
print("Obtaining cost basis information...") | |
# unrealized cost | |
driver.get(apex_information.cost_basis_open_url) | |
sleep(5) | |
driver.get(apex_information.cost_basis_closed_url) | |
sleep(5) | |
# function to move files | |
# ---------------------- | |
def move_downloaded_files(apex_file_names): | |
for file in apex_file_names: | |
print("Moving", file, "...") | |
move(download_directory + file, destination_directory + file) | |
print("Done.") | |
# this is the main | |
# thing that gets done | |
# -------------------- | |
driver = webdriver.Chrome(home_directory + '/Downloads/chromedriver') | |
def main_part(): | |
driver.get(apex_information.apex_login_url) | |
sleep(2) | |
apex_login(apex_user, apex_pass) | |
sleep(4) | |
positions_export() | |
sleep(5) | |
activity_export() | |
sleep(5) | |
get_cost_basis() | |
driver.quit() | |
move_downloaded_files(file_names) | |
print("Running R script...") | |
# run the R script that updates my | |
# portfolio in Google Sheets | |
# -------------------------------- | |
call([R_location, '--vanilla', R_script]) | |
print("Done. Information updated.") | |
main_part() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment