Making finance bots using python and bs4

I recently discovered something about myself, that I prefer asking a bot to get some quick info than searching for and opening a website.

So as a trial I decided to make financial bots for telegram using python and bs4.

bot_1 : StonkBoi

This bot scrapes the live stock data of a company from yahoo finance and reports back on telegram. It uses bs4 and requests to search and scrape the data and telegram python library to get the commands and send the info.

Bot is divided into two files the first file dataFetch.py has all the scraping goodness organised into different functions which are called in the tradeBot.py file.

Here’s some sample code for the dataFetch.py :

def lastTradedPrice(ticker):
    page = requests.get("https://in.finance.yahoo.com/quote/"+ticker)
    soup = BeautifulSoup(page.content, 'html.parser')
    val = soup.findAll("span", {"class": "Trsdu(0.3s) Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(b)"})
    if len(val) >=1 :
        return val[0].get_text()
    else:
        return "Invalid Request"

Here function lastTradedPrice request the whole yfinance page for the given ticker and then beautiful soup fetches the price element using its findAll function to filter for specific class( in this case : Trsdu(0.3s) Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(b)). The function returns “Invalid request” it it detects the value is null . We cannot use the response code of the requests function as the page still returns the not found page and send the 200 status code.

Here’s the complete source code for the bot.

Here is the list of commands :

Enter '$<ticker> x' where x can be 
sum => summary
ltp => Last Traded Price
mktcap => Market Cap
chg => Day's price change
prevclose => Previous Close Price
openprice => Opening Price
dayrange => Day's price range
52range => 52 Week Range
vol => volume
pe => P/E Ratio
div => Dividend Yields
Example => $SPY sum
Example-2 => $HDFCBANK.NS ltp

bot_2 : MarginsBoi

This bot fetches the latest margins info provided by my stonk broker – Zerodha. It can find latest margins on commodities, equities, equity futures and currency futures.

The scraping part of the bot is also accessible through command line/terminal.

Here’s some sample code of the scraping part :

def equityFutures(ticker):
    j =0
    page = requests.get("https://zerodha.com/margin-calculator/Futures/")
    soup = BeautifulSoup(page.content, 'html.parser')
    table = soup.find("table", {"class": "data futures"})
    table_rows = table.find_all('tr')
    head = 'Contract\tExpiry\tLot Size\tPrice\tNRML Margin\tNRML Margin Rate \tMWPS\n\n'
    data = ''
    for tr in table_rows:
        if j>=1:
            td = tr.find_all('td')
            row = [i.text for i in td]
            completeRow = ''
            if ticker.lower() == row[1].lower().replace('\n', '').replace('\t', ''):
                for index, cell in enumerate(row, start=0): 
                    if index <= 7 and index >=1 :
                        completeRow = completeRow + cell.replace('\n', '').replace('\t', '') + '\t\t'
                data = data + completeRow + '\n'
        j = j+1
    return head + data

Here the bot scrapes the whole margins page for equity futures using requests and finds the desired table using bs4. Then a for loop parses every row of the table and a conditional checks if the second element of row (stonk name) matches the input ticker. It then goes on to return all the futures derivatives of the given ticker after cleaning the data for ‘\n\ & ‘\t’ characters.

Here’s the complete source for the python script.

Here is the list of terminal commands :

Enter 'python3 marginBoi.py <TICKER> <x>' where x can be :  
'com' : Commodity
'eq' : Equities
'eqfut' : Equity Futures
'fxfut' : Currency Futures
Example : 'python3 marginBoi.py BHEL eqfut'

I plan of adding more robust features to stonk-boi like maybe trade execution and stonk alerts…. Stay tuned !