Python for Finance

Ronit Malik
CARRE4

--

Completing my undergraduate degree in finance, investment and risk and then switching career and completing masters in business analytics, I always use to think about the application of data analytics that may have in the financial industry and how various software can make the process faster.

I was introduced to Python programming language by one of my friends in my undergraduate degree, it was only till I reached master when I understood the true potential of the programming language and how it can make the process so much quicker.

A little disclaimer I am not a coder by any means nor am I a software engineer but I have discovered the possibility of python and the various uses cases it has, I guess my curiosity led me to discovering python even further.

Over the last few months I have been investing in the stock market and I thought why not use Python for scrapping data from yahoo finance.

This is when I came across this documentation http://theautomatic.net/yahoo_fin-documentation/ and I guess this made my life easier. Yahoo_fin is a package within Python 3 which scrapes data from yahoo finance. It is a powerful tool as it can load data, show analyst evaluation and various other metrics.

After reading this I thought of giving it a shot and used ‘APPLE’ as an example to work around the package:-

The first step is to download the library itself, I prefer using google collab https://colab.research.google.com/ but you can also use anaconda, spyder or pycharm any IDE that you may like.

!pip install yahoo_fin

!pip install request_html

On google collab you can directly used ‘! pip’ command to download the packages as seen above, then we import all the dependencies for this program.

from yahoo_fin import stock_info as si

import pandas as pd

import numpy as np

from datetime import datetime

Once you load all the library we can go to the next step, however if you get an error here make sure you have the package loaded by used the ‘!pip’ command.

I have created a function to load the tickers.

def stock(ticker):

stock_ticker = si.get_data(ticker, start_date = '1-1-2010', end_date = none, index_as_date=True, interval='1mo')

print(stock_ticker)

This function picks the ‘ticker’ from yahoo finance, you can adjust the dates as you like I have it from 2010 to 2020, the end date will be defaulted to today with 1 month intervals.

I can call this function to get the historical data for Apple

stock('AAPL')

This will output the historical data for apple from 2010 to 2020, now we can further examine this stock by using the various metrics that the package provides, I will use few as an example for this article but feel free to read through the documentation and use others too.

I will create another function to display the metrics, I am interested to get information about the shares: analyst info, valuations, and live price.

def stock_info(ticker):

info = si.get_analysts_info(ticker)

val = si.get_stats_valuation(ticker)

live_price = si.get_live_price(ticker)

return(info, val, live_price)

The function above will display the various metrics, the package does have a lot of other metrics so do check them out.

I hope this will get you guys started with the initial web scrapping from yahoo finance using this package, make sure to try this code out yourself.

Also, if you have read till here then do check out my github — https://github.com/RonitMalik

Check out the full tutorial on youtube — https://www.youtube.com/watch?v=AsxpHMq2auc

--

--

Ronit Malik
CARRE4
Writer for

Tech, Finance & Productivity Content Creator On Youtube