Yahoo Finance

Tutorial on how to get current/historical prices from yahoo finance using yahoo_finace

Import yahoo finance and other necessary packages


In [1]:
from yahoo_finance import Share
import numpy as np

First we need to create an instance of Share. Using that instance we will get prices, volumes, ratios and all other company information


In [2]:
#for this Example I will use google's finances

#create an instance of Share
google = Share('GOOG')

Price Information


In [3]:
#now that an instance of Share is created (google), we will call its functions to get the prices

#date and time of the trade
date = google.get_trade_datetime()

#opening price
opening_price = google.get_open()

#Price right now (Yahoo finance is delayed by 15 mins)
current_price = google.get_price()

#Day's high and low prices 
day_high = google.get_days_high()
day_low = google.get_days_low()

#price changes from opening price
price_change = google.get_change()

print "trading date: ", date
print "current price: ", current_price
print "opening price: $" , opening_price
print "day high: $", day_high
print "day low: $", day_low
print "print price change: $", price_change
#Refresh to get a new price
# Note that after the market closes @ 4PM EST, the price will stay the same
google.refresh()

date = google.get_trade_datetime()
current_price = google.get_price()
price_change = google.get_change()

print "\n########## After refreshing ####################"
print "trading date: ", date
print "current price: ", current_price
print "opening price: $" , opening_price
print "print price change: $", price_change


trading date:  2015-10-09 20:00:00 UTC+0000
current price:  643.61
opening price: $ 640.00
day high: $ 645.99
day low: $ 635.32
print price change: $ +4.45

########## After refreshing ####################
trading date:  2015-10-09 20:00:00 UTC+0000
current price:  643.61
opening price: $ 640.00
print price change: $ +4.45

Moving averages. Get a peek of what prices have been like in the past.


In [4]:
#If current prices are higher than 50 or 200 days moving average, that means prices are going up

#200 days moving average
th_moving_avg = google.get_200day_moving_avg()

#50 days moving average
fifty_moving_avg = google.get_50day_moving_avg()

print "200 days moving average: $", th_moving_avg
print "50 days moving average: $", fifty_moving_avg


200 days moving average: $ 576.96
50 days moving average: $ 620.83

Volume Information


In [5]:
#Volume speaks (If more people are trading, there's gotta be something good or bad happening)

volume = google.get_volume()
#compare this days volume with average volume
average_daily_volume = google.get_avg_daily_volume()

print "Today's volume: ", volume
print "Average volume: ", average_daily_volume


Today's volume:  1648726
Average volume:  2622860

Ratios are important for technical analysis. Price to Earning ratio is the most important of them all. Value investors like Warren Buffet use this to their analysis.


In [6]:
#PE ratio ---> price per share divided by earnings per share
#Lower PE the better 
PE = google.get_price_earnings_ratio()

#PEG ratio ---> pe ratio divided by 1-reinvestment (growth)
PEG = google.get_price_earnings_growth_ratio()

print "Price to earning (PE) ratio : ", PE
print "Price earning to growth (PEG) ratio: ", PEG


Price to earning (PE) ratio :  30.32
Price earning to growth (PEG) ratio:  1.22

Book Value


In [7]:
#book value -> what the numbers say this company is worth
print "book value",  google.get_book_value()


book value 163.07

Dividends: how is the company paying its investors


In [8]:
div_per_share = google.get_dividend_share()
div_yield = google.get_dividend_yield()

#for some reason Google's dividend information was not available
print "dividend per share: $", div_per_share
print "divident yield: ", div_yield


dividend per share: $ None
divident yield:  None

Historical Prices

*Data not available for Saturday/Sunday


In [10]:
historical = google.get_historical('2015-07-28', '2015-09-08')
print len(historical)


0

In [63]:
print len(historical)
#To get the closing price for first day
print historical[0]['Close']

#opening price for first day
print historical[0]['Open']


30
614.659973
612.48999

More on historical prices coming soon


In [65]:
#to get all opening prices together

opening = [] #is a dynamic array (list) for python

for i in range(len(historical)):
    x = historical[i]['Open']
    opening.append(x)

closing = [] #is a dynamic array (list) for python

for i in range(len(historical)):
    x = historical[i]['Close']
    closing.append(x)

x_axis = np.arange(0+1, len(historical)+1)

In [1]:
#print opening
#print closing
#print x_axis

In [67]:
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot(x_axis,opening, 'b', x_axis, closing, 'r')
plt.xlabel('Day')
plt.ylabel('Price ($)')
plt.show()



In [28]:


In [ ]: