from yahoo_finance import Share
import numpy as np


"""
Explanation: Yahoo Finance
Tutorial on how to get current/historical prices from yahoo finance using yahoo_finace
Import yahoo finance and other necessary packages
End of explanation
"""


#for this Example I will use google's finances

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

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


#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

"""
Explanation: Price Information
End of explanation
"""


#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

"""
Explanation: Moving averages. Get a peek of what prices have been like in the past.
End of explanation
"""


#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

"""
Explanation: Volume Information
End of explanation
"""


#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

"""
Explanation: 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.
End of explanation
"""


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

"""
Explanation: Book Value
End of explanation
"""


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

"""
Explanation: Dividends: how is the company paying its investors
End of explanation
"""


historical = google.get_historical('2015-07-28', '2015-09-08')
print len(historical)

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

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


"""
Explanation: Historical Prices
*Data not available for Saturday/Sunday
End of explanation
"""


#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)


#print opening
#print closing
#print x_axis

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()



"""
Explanation: More on historical prices coming soon
End of explanation
"""


