Obtaining Stock Prices
Last updated
Last updated
Jan 29, 2019 · 3 min read
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.
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.
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.
Next, the stock price for each company must be retrieved. This process holds a lot of data which needs to be processed correctly.
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 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.
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.
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.