In [56]:
#importing necessary libraries
import json
import ystockquote
import pprint
import numpy
import operator
import numpy as np
from datetime import datetime
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
#custom function
def price_series(symbol, start_date, end_date):
series = {}
data = ystockquote.get_historical_prices(symbol, start_date, end_date)
data
for k,v in data.iteritems():
series.update({k:v['Adj Close']})
return series
##### start main program #######
#print 'Please enter symbol:'
symbol = 'MSFT'
start_date = '1999-01-04'
end_date = '1999-06-18'
prices = price_series(symbol, start_date, end_date)
date = []
price = []
dates = []
date = sorted(prices.keys())
n = 0
for i in date:
price.append(prices[date[n]])
n = 1 + n
#print date
#print price
#dates = plt.dates.datestr2num(date)
dates = range(len(date))
#print dates
plt.plot(dates,price,color='g')
plt.show()
In [59]:
#
# ystockquote : Python module - retrieve stock quote data from Yahoo Finance
#
# Copyright (c) 2007,2008,2013 Corey Goldberg (cgoldberg@gmail.com)
#
# license: GNU LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Requires: Python 2.7/3.2+
__version__ = '0.2.5dev'
try:
# py3
from urllib.request import Request, urlopen
from urllib.parse import urlencode
except ImportError:
# py2
from urllib2 import Request, urlopen
from urllib import urlencode
import pprint
def _request(symbol, stat):
url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (symbol, stat)
req = Request(url)
resp = urlopen(req)
return str(resp.read().decode('utf-8').strip())
def get_historical_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
Date format is 'YYYY-MM-DD'
Returns a nested dictionary (dict of dicts).
outer dict keys are dates ('YYYY-MM-DD')
"""
params = urlencode({
's': symbol,
'a': int(start_date[5:7]) - 1,
'b': int(start_date[8:10]),
'c': int(start_date[0:4]),
'd': int(end_date[5:7]) - 1,
'e': int(end_date[8:10]),
'f': int(end_date[0:4]),
'g': 'd',
'ignore': '.csv',
})
url = 'http://ichart.yahoo.com/table.csv?%s' % params
req = Request(url)
resp = urlopen(req)
content = str(resp.read().decode('utf-8').strip())
daily_data = content.splitlines()
#print daily_data
hist_dict = dict()
keys = daily_data[0].split(',')
#print keys
for day in daily_data[1:]:
day_data = day.split(',')
date = day_data[0]
hist_dict[date] = \
{keys[1]: day_data[1],
keys[2]: day_data[2],
keys[3]: day_data[3],
keys[4]: day_data[4],
keys[5]: day_data[5],
keys[6]: day_data[6]}
return hist_dict
def get_dividend_yield(symbol):
return _request(symbol, 'y')
hist = get_historical_prices('IBM', '2013-12-04', '2013-12-06')
pprint.pprint(hist)
In [33]:
import urllib
import urllib2
from urllib2 import Request, urlopen
from urllib import urlencode
hello = salutation('Joel')
def salutation(name):
print "Bonjour %s" %name
In [60]:
def get_historical_prices(symbol, start_date, end_date):
"""
Get historical prices for the given ticker symbol.
Date format is 'YYYY-MM-DD'
Returns a nested dictionary (dict of dicts).
outer dict keys are dates ('YYYY-MM-DD')
"""
params = urlencode({
's': symbol,
'a': int(start_date[5:7]) - 1,
'b': int(start_date[8:10]),
'c': int(start_date[0:4]),
'd': int(end_date[5:7]) - 1,
'e': int(end_date[8:10]),
'f': int(end_date[0:4]),
'g': 'd',
'ignore': '.csv',
})
url = 'http://ichart.yahoo.com/table.csv?%s' % params
req = Request(url)
resp = urlopen(req)
content = str(resp.read().decode('utf-8').strip())
daily_data = content.splitlines()
#print daily_data
hist_dict = dict()
keys = daily_data[0].split(',')
#print keys
for day in daily_data[1:]:
day_data = day.split(',')
date = day_data[0]
hist_dict[date] = \
{keys[1]: day_data[1],
keys[2]: day_data[2],
keys[3]: day_data[3],
keys[4]: day_data[4],
keys[5]: day_data[5],
keys[6]: day_data[6]}
return hist_dict
hist = get_historical_prices('IBM', '2013-12-04', '2013-12-06')
hist
Out[60]:
In [93]:
import json
import ystockquote
import pprint
import numpy
import operator
import numpy as np
from datetime import datetime
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
result = []
def price_series(symbol, start_date, end_date):
series = {}
data = ystockquote.get_historical_prices(symbol, start_date, end_date)
for k,v in data.iteritems():
#print k,v
#print k,v['Adj Close']
series.update({k:v['Adj Close']})
return series
prices = price_series('GOOG', '2013-09-09', '2013-12-06')
delta = []
for key, value in prices.iteritems():
date = sorted(prices.keys())
delta.append([key,value])
print sorted(delta)
print date
date = []
price = []
dates = []
change = []
date = sorted(prices.keys())
n = 0
for i in date:
#dates.append(int(i))
quote = float(prices[date[n]])
pquote = float(prices[date[n-1]])
price.append(quote)
change.append((quote-pquote)/pquote)
#price.append(float(prices[date[n]]))
#change.append((prices[date[n]] - prices[date[n-1]])/(prices[date[n-1]]))
n = 1 + n
#print price
#print change
dates = range(len(date))
#print dates
#plt.plot(dates,price,color='g')
#plt.show()
In [ ]: