2015/12/13

Automatically stream and download the Yahoo! Finance stock price using Python

Thanks to the Pandas package in Python, now we can stream the stock price from Yahoo! automatically within 1 second. And of course it's free! 

I am going to show you the example of downloading stock price of US Oil, Facebook, Best Buy, and Expedia from Jan 1st, 2014 to Dec 1st, 2015 and saving the data into a CSV file on your local drive. 

======== Code Start======== 
import pandas.io.data as pdweb
import datetime
#import the library you need from pandas

stockprice = pdweb.get_data_yahoo(['USO','FB','BBY','EXPE'],start = datetime.datetime(2014,1,1),end=datetime.datetime(2015,12,1))['Adj Close']
#set the stocks you would like to download and the time frame

stockprice.to_csv('stockprice.csv')
#save it to your local drive. It's in the same folder where you run the python.
======== Code End======== 



Result from jupyter Notebook (AKA iPython Notebook) 

The CSV file opened in Excel will look like as below. You can import this excel file to Tableau and R Studio to do more analysis. If you know how to conduct the analysis in Python, there is no need to save it as an Excel file. :

Correlation of the stocks:

I made a plot of four stocks to see if they have correlation in Python. Facebook and Expedia have very strong correlation. When the stock price of Facebook increases, the stock price of Expedia increases as well. The simply answer is that they are both technology and internet stock. 
When the price of Best Buy decreased, the US Oil stock price kept increasing.


1 comment:

  1. Thanks for such a great information and to provide us. We have the finest I mcafee certified technicians and If you're facing trouble in your mcafee antivirus software then just go through the link here. Click Here:- http://mcafeetechsupport.com/

    ReplyDelete