def get_cost_basis(self, instrument): self.go_to_cost_basis() holding_table_xpath = "//table[starts-with(@id,'unrealizedTabForm')]" # We need to wait since the information is loaded after pageload self.browser.wait_until_visible(holding_table_xpath) holding_table = self.browser.find_elements_by_xpath( holding_table_xpath)[0] rows = holding_table.find_elements_by_tag_name("tr") instrument_id = None for row in rows: tds = row.find_elements_by_tag_name("td") # This is a header if len(tds) == 9: if instrument == tds[0].text: a = tds[8].find_elements_by_xpath( ".//span[contains(@class, 'noPipe firstLink')]/a") url = a[0].get_attribute('href') instrument_id = url.split('HldId=')[1] elif instrument_id and "Show details" in row.text: row.click() break lots_table_xpath = "//table[contains(@id, '%s')]" % instrument_id self.browser.wait_until_visible(lots_table_xpath) lots_table = self.browser.find_elements_by_xpath(lots_table_xpath)[0] lots_rows = lots_table.find_elements_by_tag_name("tr") lots_info = [] data_headers = [] data_headers.append(("date", TextType.plain)) data_headers.append(("quantity", TextType.plain)) data_headers.append(("cost_per_share", TextType.dollar)) data_headers.append(("total_cost", TextType.dollar)) data_headers.append(("market_value", TextType.dollar)) data_headers.append(("short_term", TextType.dollar)) data_headers.append(("long_term", TextType.dollar)) data_headers.append(("total_gain_lost", TextType.dollar)) for row in lots_rows: tds = row.find_elements_by_tag_name("td") if len(tds) == 8: lot = {} for index in xrange(len(tds)): headerName, textType = data_headers[index] lot[headerName] = Converters.convert( tds[index].text, textType) lots_info.append(lot) return lots_info
def get_current_holdings(self): self.go_to_balances_and_holdings() holdings_tables = self.browser.find_elements_by_xpath( "//table[starts-with(@id,'BHForm2:accountID:')]") accounts = {} account_num = 0 data_headers = [] data_headers.append(("symbol", TextType.plain)) data_headers.append(("name", TextType.plain)) data_headers.append(("expense_ratio", TextType.percent)) data_headers.append(("quantity", TextType.plain)) data_headers.append(("last_price", TextType.dollar)) data_headers.append(("change_amount", TextType.dollar)) data_headers.append(("change_percent", TextType.percent)) data_headers.append(("current_balance", TextType.dollar)) for holdings_table in holdings_tables: if len(holdings_table.find_elements_by_tag_name("th")) == 0: continue rows = holdings_table.find_elements_by_tag_name("tr") # First row is Tables Headers # Second row simply says "ETFs" rows = rows[2:] # Row before last simply says "Buy Sell" # Last row is Total Assets rows = rows[:-2] # Remaining rows are actual ETFs holdings_info = [] for row in rows: holding_info = {} els = row.find_elements_by_tag_name("td") if len(els) < 8: continue for el_num in xrange(len(els)): if el_num >= len(data_headers): continue headerName, textType = data_headers[el_num] holding_info[headerName] = Converters.convert( els[el_num].text, textType) holdings_info.append(holding_info) account_num += 1 accounts["Account %s" % account_num] = holdings_info return accounts
def test_can_convert_percentages(self): test_cases = [ (1.0, "100%"), (1.0, "100.00%"), (0.99, "99.00%"), (0.01, "1%"), (-0.01, "-1%"), (-0.1, "- 10%"), (-0.01, "-\u2013 1%"), (-0.1, "- \u2013 10%") ] for result, case in test_cases: self.assertEqual(result, Converters.percent(case), "Failed for {}".format(case))
def test_can_convert_dollar_amounts(self): test_cases = [ (1.0, "1"), (10, "10"), (10.1, "10.1"), (1000, "1,000"), (-1000000, "-1,000,000"), (1.0, "$1"), (10, "$10"), (10.1, "$10.1"), (1000, "$1,000"), (-1000000, "-$1,000,000"), (-1000000, "- $1,000,000"), (10, "$ \u2013 10"), (10.1, "\u2013 $10.1"), ] for result, case in test_cases: self.assertEqual(result, Converters.dollar_amount(case), "Failed for {}".format(case))
def get_current_holdings(self): self.go_to_balances_and_holdings() holdings_table = self.browser.find_element_by_id("BHForm2:accountID:0:_id245") rows = holdings_table.find_elements_by_tag_name("tr") # First row is Tables Headers # Second row simply says "ETFs" rows = rows[2:] # Row before last simply says "Buy Sell" # Last row is Total Assets rows = rows[:-2] # Remaining rows are actual ETFs holdings_info = [] for row in rows: holding_info = {} els = row.find_elements_by_tag_name("td") # Symbol holding_info["symbol"] = els[0].text # Name (needs to be trimmed of spaces) name = els[1].text holding_info["name"] = name.strip() # Expense Ratio (Convert to float ratio) holding_info["expense_ratio"] = Converters.percent(els[2].text) # Quantity (Convert to doller value) holding_info["quantity"] = Converters.dollar_amount(els[3].text) # Last Price (Convert to doller value) holding_info["last_price"] = Converters.dollar_amount(els[4].text) # Absolute Change Amount (Convert to doller value) holding_info["change_amount"] = Converters.dollar_amount(els[5].text) # Precentage Change (Convert to float ratio) holding_info["change_percent"] = Converters.percent(els[6].text) # Current Balance for this holding (Convert to dollar value) holding_info["current_balance"] = Converters.dollar_amount(els[7].text) holdings_info.append(holding_info) return holdings_info