DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Extracting Financial Data Using Python
WaburiBK
WaburiBK

Posted on

Extracting Financial Data Using Python

Struggling with Virtual Environments

So before I was even able to pull financial data off Yahoo Finance using Python, I really struggled with remembering how to create a virtual environment (venv). I’ve always known that it is important to work on a venv so as to not interfere with the global Python environment. Additionally, it makes it easier to share my work with other developers.

Libraries and Dictionaries

Regardless, after being saved by tech Indian gurus on YouTube, I immediately got started on reviewing an article by Ran Aroussi , who developed the yfinance library. This library is able to reliably extract data from Yahoo Finance.
After installation of the pip command (pip install yfinance), I had to pass an argument of the ticker symbol that I wish to extract information from. A ticker represents each stock/security uniquely for trading purposes. In this illustration, I wanted to extract Facebook’s financial data using the ticker β€œFB”.

Extracting Financial Data Using Python

Extracting keys from the dictionary

Company information is contained within the info dictionary. I was able to extract certain elements of the dictionary by printing the keys of the info dictionary. Examples of the keys I extracted include: β€˜sector’, β€˜address1’ and β€˜fiftyTwoWeekHigh’.

Being Specific
Additionally, it is possible to extract daily prices over a specified period of time. This is possible through specifying the period. Valid entries include 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y and ytd(year to date).
Furthermore, I was able to extract daily prices within a range of time. First, I had to import the datetime library. According to Python documentation, the datetime module supplies classes for manipulating dates and times. Specifically, the class datetime.datetime contains attributes of year, month, day, hour, minute, second, microsecond and tzinfo.
I proceeded to define my start and end dates and got an output of daily prices between the specified dates.

Disclaimer: Here to share my thoughts in this self-taught journey.

Top comments (0)

Classic DEV Post

brain computer interface

Experimenting with brain-computer interfaces in JavaScript