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)


{'2013-12-04': {'Adj Close': '175.74',
                'Close': '175.74',
                'High': '177.50',
                'Low': '175.16',
                'Open': '175.37',
                'Volume': '5267400'},
 '2013-12-05': {'Adj Close': '176.08',
                'Close': '176.08',
                'High': '176.86',
                'Low': '175.28',
                'Open': '176.15',
                'Volume': '4384900'},
 '2013-12-06': {'Adj Close': '177.67',
                'Close': '177.67',
                'High': '178.00',
                'Low': '176.01',
                'Open': '176.70',
                'Volume': '4739800'}}

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


Bonjour Joel

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]:
{'2013-12-04': {'Adj Close': '175.74',
  'Close': '175.74',
  'High': '177.50',
  'Low': '175.16',
  'Open': '175.37',
  'Volume': '5267400'},
 '2013-12-05': {'Adj Close': '176.08',
  'Close': '176.08',
  'High': '176.86',
  'Low': '175.28',
  'Open': '176.15',
  'Volume': '4384900'},
 '2013-12-06': {'Adj Close': '177.67',
  'Close': '177.67',
  'High': '178.00',
  'Low': '176.01',
  'Open': '176.70',
  'Volume': '4739800'}}

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


[['2013-09-09', '888.05'], ['2013-09-10', '888.67'], ['2013-09-11', '896.19'], ['2013-09-12', '893.06'], ['2013-09-13', '889.07'], ['2013-09-16', '887.76'], ['2013-09-17', '886.11'], ['2013-09-18', '903.32'], ['2013-09-19', '898.39'], ['2013-09-20', '903.11'], ['2013-09-23', '886.50'], ['2013-09-24', '886.84'], ['2013-09-25', '877.23'], ['2013-09-26', '878.17'], ['2013-09-27', '876.39'], ['2013-09-30', '875.91'], ['2013-10-01', '887.00'], ['2013-10-02', '887.99'], ['2013-10-03', '876.09'], ['2013-10-04', '872.35'], ['2013-10-07', '865.74'], ['2013-10-08', '853.67'], ['2013-10-09', '855.86'], ['2013-10-10', '868.24'], ['2013-10-11', '871.99'], ['2013-10-14', '876.11'], ['2013-10-15', '882.01'], ['2013-10-16', '898.03'], ['2013-10-17', '888.79'], ['2013-10-18', '1011.41'], ['2013-10-21', '1003.30'], ['2013-10-22', '1007.00'], ['2013-10-23', '1031.41'], ['2013-10-24', '1025.55'], ['2013-10-25', '1015.20'], ['2013-10-28', '1015.00'], ['2013-10-29', '1036.24'], ['2013-10-30', '1030.42'], ['2013-10-31', '1030.58'], ['2013-11-01', '1027.04'], ['2013-11-04', '1026.11'], ['2013-11-05', '1021.52'], ['2013-11-06', '1022.75'], ['2013-11-07', '1007.95'], ['2013-11-08', '1016.03'], ['2013-11-11', '1010.59'], ['2013-11-12', '1011.78'], ['2013-11-13', '1032.47'], ['2013-11-14', '1035.23'], ['2013-11-15', '1033.56'], ['2013-11-18', '1031.55'], ['2013-11-19', '1025.20'], ['2013-11-20', '1022.31'], ['2013-11-21', '1034.07'], ['2013-11-22', '1031.89'], ['2013-11-25', '1045.93'], ['2013-11-26', '1058.41'], ['2013-11-27', '1063.11'], ['2013-11-29', '1059.59'], ['2013-12-02', '1054.48'], ['2013-12-03', '1053.26'], ['2013-12-04', '1058.18'], ['2013-12-05', '1057.34'], ['2013-12-06', '1069.87']]
['2013-09-09', '2013-09-10', '2013-09-11', '2013-09-12', '2013-09-13', '2013-09-16', '2013-09-17', '2013-09-18', '2013-09-19', '2013-09-20', '2013-09-23', '2013-09-24', '2013-09-25', '2013-09-26', '2013-09-27', '2013-09-30', '2013-10-01', '2013-10-02', '2013-10-03', '2013-10-04', '2013-10-07', '2013-10-08', '2013-10-09', '2013-10-10', '2013-10-11', '2013-10-14', '2013-10-15', '2013-10-16', '2013-10-17', '2013-10-18', '2013-10-21', '2013-10-22', '2013-10-23', '2013-10-24', '2013-10-25', '2013-10-28', '2013-10-29', '2013-10-30', '2013-10-31', '2013-11-01', '2013-11-04', '2013-11-05', '2013-11-06', '2013-11-07', '2013-11-08', '2013-11-11', '2013-11-12', '2013-11-13', '2013-11-14', '2013-11-15', '2013-11-18', '2013-11-19', '2013-11-20', '2013-11-21', '2013-11-22', '2013-11-25', '2013-11-26', '2013-11-27', '2013-11-29', '2013-12-02', '2013-12-03', '2013-12-04', '2013-12-05', '2013-12-06']

In [ ]: