Python for finance: Obtaining Stock Prices

Mohammod Rahman
MathematicalModelling
3 min readJan 28, 2019

Python is the most common coding language used in the world of finance. It’s flexibility allows python to be manipulated for finance. The SP500 is an american stock market index based on the market capitalization of 500 large companies. It is seen as a gauge of prosperity for businesses regulated by US law.

Step 1: List of Companies.

It is crucial to obtain accurate stock price information easily and quickly. You can obtain a list of all the companies in the SP500 on Wikipedia.

FIgure 1: Script for saving sp500 tickers

Python can be used to extract and save a list of tickers associated to each company using beautifulsoup; a python library for pulling data out of HTML and XML files, requests and pickle; a python module which implements binary protocols for serializing and de-serializing a python object structure (pickling object hierachy is converted into a byte stream). This process will save programmers hours of work in retrieving and saving information. Figure 1 is illustrates how within 20 lines of code, you can easily obtain a list of 500 companies from Wikipedia; from locating the table on the page to saving the key information.

FIgure 2: Output of SP500

Step 2: Stock data

Next, the stock price for each company must be retrieved. This process holds a lot of data which needs to be processed correctly.

Figure 4: Results in a folder of every company’s stock price.

Python can be used to extract stock information from websites such as Yahoo finance, Google finance using the data analysis library called Panda and module data reader. Pandas is great for data manipulation and analysis.

Figure 4: Results in a folder of every company’s stock price.

Figure 3 and figure 4 demonstrate how information from Yahoo finance for every SP500 company can be extracted and stored locally for future use, as continuously extracting information can become time consuming.

Step 3: Compile data

Figure 5: Compiling the data

Finally, as we are only looking at the adjusted close for each stock price, we can combine all the companies into a single csv file.

Figure 5 shows how the code will delete the columns deemed unnecessary and join each company’s adjusted close to the end of the new csv file.

Figure 6: combined data frame output

The data can now be manipulated and analyzed for modelling.

Github: https://github.com/MoRahmanWork/Py4Fin/blob/master/ObtainStockPrices

Disc: Self taught: For more information: pythonprogramming.net, sentdex.

--

--