tradezero_api.account

 1from __future__ import annotations
 2
 3import warnings
 4from collections import namedtuple
 5
 6from selenium.webdriver.common.by import By
 7
 8
 9class Account:
10    def __init__(self, driver):
11        self.driver = driver
12        self.attribute_ids = [
13            "h-realized-value",
14            "h-unrealizd-pl-value",
15            "h-total-pl-value",
16            "p-bp",
17            "h-cash-value",
18            "h-exposure-value",
19            "h-equity-value",
20            "h-equity-ratio-value",
21            "h-used-lvg-value",
22            "p-allowed-lev",
23            "h-select-account",
24            "h-loginId",
25            "trading-order-label-account",
26        ]
27
28    def hide_attributes(self):
29        """
30        Hides all account attributes i.e, account username, equity-value, cash-value, realized-value...
31        """
32        for id_ in self.attribute_ids:
33            element = self.driver.find_element(By.ID, id_)
34            self.driver.execute_script("arguments[0].setAttribute('style', 'display: none;')", element)
35
36    @property
37    def attributes(self):
38        """
39        returns a namedtuple with the account following properties:
40        realized_pnl, unrealized_pnl, total_pnl, buying_power, cash,
41        exposure, equity, equity_ratio, used_lvg, allowed_lvg.
42        note that if one or more of the attributes have been hidden; either by
43        setting self.hide_attributes = True or by calling hide_attributes(),
44        a namedtuple will be returned with None values.
45
46        :return: namedtuple
47        """
48        Data = namedtuple('Data', ['realized_pnl', 'unrealized_pnl', 'total_pnl', 'buying_power', 'cash',
49                                   'exposure', 'equity', 'equity_ratio', 'used_lvg', 'allowed_lvg'])
50
51        attribute_ids = self.attribute_ids[:-3]
52        values = []
53        for id_ in attribute_ids:
54            element = self.driver.find_element(By.ID, id_)
55
56            if element.get_attribute('style') == 'display: none;':
57                warnings.warn('cannot fetch attribute that has been hidden')
58
59                empty_list = [None] * len(attribute_ids)
60                return Data._make(empty_list)
61
62            value = element.text.translate(str.maketrans('', '', '$%x,'))
63            values.append(float(value))
64        return Data._make(values)
class Account:
10class Account:
11    def __init__(self, driver):
12        self.driver = driver
13        self.attribute_ids = [
14            "h-realized-value",
15            "h-unrealizd-pl-value",
16            "h-total-pl-value",
17            "p-bp",
18            "h-cash-value",
19            "h-exposure-value",
20            "h-equity-value",
21            "h-equity-ratio-value",
22            "h-used-lvg-value",
23            "p-allowed-lev",
24            "h-select-account",
25            "h-loginId",
26            "trading-order-label-account",
27        ]
28
29    def hide_attributes(self):
30        """
31        Hides all account attributes i.e, account username, equity-value, cash-value, realized-value...
32        """
33        for id_ in self.attribute_ids:
34            element = self.driver.find_element(By.ID, id_)
35            self.driver.execute_script("arguments[0].setAttribute('style', 'display: none;')", element)
36
37    @property
38    def attributes(self):
39        """
40        returns a namedtuple with the account following properties:
41        realized_pnl, unrealized_pnl, total_pnl, buying_power, cash,
42        exposure, equity, equity_ratio, used_lvg, allowed_lvg.
43        note that if one or more of the attributes have been hidden; either by
44        setting self.hide_attributes = True or by calling hide_attributes(),
45        a namedtuple will be returned with None values.
46
47        :return: namedtuple
48        """
49        Data = namedtuple('Data', ['realized_pnl', 'unrealized_pnl', 'total_pnl', 'buying_power', 'cash',
50                                   'exposure', 'equity', 'equity_ratio', 'used_lvg', 'allowed_lvg'])
51
52        attribute_ids = self.attribute_ids[:-3]
53        values = []
54        for id_ in attribute_ids:
55            element = self.driver.find_element(By.ID, id_)
56
57            if element.get_attribute('style') == 'display: none;':
58                warnings.warn('cannot fetch attribute that has been hidden')
59
60                empty_list = [None] * len(attribute_ids)
61                return Data._make(empty_list)
62
63            value = element.text.translate(str.maketrans('', '', '$%x,'))
64            values.append(float(value))
65        return Data._make(values)
Account(driver)
11    def __init__(self, driver):
12        self.driver = driver
13        self.attribute_ids = [
14            "h-realized-value",
15            "h-unrealizd-pl-value",
16            "h-total-pl-value",
17            "p-bp",
18            "h-cash-value",
19            "h-exposure-value",
20            "h-equity-value",
21            "h-equity-ratio-value",
22            "h-used-lvg-value",
23            "p-allowed-lev",
24            "h-select-account",
25            "h-loginId",
26            "trading-order-label-account",
27        ]
driver
attribute_ids
def hide_attributes(self):
29    def hide_attributes(self):
30        """
31        Hides all account attributes i.e, account username, equity-value, cash-value, realized-value...
32        """
33        for id_ in self.attribute_ids:
34            element = self.driver.find_element(By.ID, id_)
35            self.driver.execute_script("arguments[0].setAttribute('style', 'display: none;')", element)

Hides all account attributes i.e, account username, equity-value, cash-value, realized-value...

attributes

returns a namedtuple with the account following properties: realized_pnl, unrealized_pnl, total_pnl, buying_power, cash, exposure, equity, equity_ratio, used_lvg, allowed_lvg. note that if one or more of the attributes have been hidden; either by setting self.hide_attributes = True or by calling hide_attributes(), a namedtuple will be returned with None values.

Returns

namedtuple