February 12, 2023; Updated on

Python Scripting to Serialize WebDriver Commands to Export Hyper SBI 2 Watchlists

Occasionally, I check the watchlists of the Hyper SBI 2 trading software on a separate terminal. Selenium WebDriver can automatically export these watchlists to a specific website. In this post, I will discuss how to serialize the WebDriver commands. This serialization allows the Python configparser module to store it in a configuration file.

Unconditional Procedure: Replace Watchlists on SBI Securities with Hyper SBI 2 Watchlists

Hyper SBI 2 sends its watchlists to the SBI Securities website periodically. First, replace the watchlists displayed on the website with the Hyper SBI 2 watchlists.

This procedure is the sequential selection of a source, a destination, and a manipulation. You can represent it as a list as follows, where each element is a tuple of a WebDriver command and XPath to find the corresponding HTML element:

action_0 = [('click', '//*[@id="id_0"]'),
            ('click', '//*[@id="id_1"]')]

Incidentally, the ast.literal_eval() function can evaluate such a string value of a configparser option as a list of tuples. Then, the following function executes this list sequentially:

def execute_action(driver, action):
    for index in range(len(action)):
        command = action[index][0]
        xpath = action[index][1]

        if command == 'click':
            driver.find_element(By.XPATH, xpath).click()

Conditional Procedure: Export Hyper SBI 2 Watchlists to Yahoo Finance

Next, export Hyper SBI 2 watchlists to My Portfolio on Yahoo Finance, which supports various technical indicators. Convert the following Hyper SBI 2 watchlist file to supported CSV watchlist files beforehand and export them to My Portfolio:

%APPDATA%\SBI Securities\HYPERSBI2\IDENTIFIER\portfolio.json

In this post, I delete a previously exported watchlist with the same name if it exists. The representation in the previous section will not work because this procedure has a condition of whether the element of the watchlist exists.

Accordingly, I append a nested list as the third argument of the command tuple. This list is an alternative action if the element exists:

action_1 = [('click', '//*[@id="id_0"]'),
            ('exist', '//*[@id="id_1"]', [('click', '//*[@id="id_1"]'),
                                          ('click', '//*[@id="id_2"]')]),
            ('click', '//*[@id="id_3"]')]

Then, the following extended function recursively executes the appended action above:

def execute_action(driver, action):
    for index in range(len(action)):
        command = action[index][0]
        xpath = action[index][1]

        if command == 'click':
            driver.find_element(By.XPATH, xpath).click()
        elif command == 'exist':
            if driver.find_elements(By.XPATH, xpath):
                execute_action(driver, action[index][2])

Python Script Example

configparser has the flexibility to represent a nested list as a value and can store it in a configuration file. In a real-world application, I have published the trading_peripheral.py Python script on GitHub.

No comments:

Post a Comment