Download SPY and use Pandas + Matlab to Plot Pricing by the Close

This shows how to download the SPY daily prices from Google Finance as a csv then load it using Pandas for plotting on the Close prices with Matlab.


In [ ]:
import os, sys, json

sys.path.insert(0, os.getenv('ENV_PYTHON_SRC_DIR', '/opt/work/src'))

from pycore import PyCore

print 'Initializing Python Core'
core = PyCore()
core.lg('')

# the downloader is hardcoded for now to download to this shared volume + file location:
csv_file = '/opt/work/data/src/spy.csv'

# removing previous csv file if it exists
if os.path.exists(csv_file):
    core.lg(' - Removing Previous(' + str(csv_file) + ')')
    os.system('rm -f ' + str(csv_file) + ')')
# end of removing previous

downloader_name    = 'download-spy-csv.py'
path_to_downloader = os.getenv('ENV_DATA_BIN_DIR', '/opt/work/src') + '/' + downloader_name

core.lg('Downloading latest SPY Pricing with Download(' + str(path_to_downloader) + ')')
os.system(path_to_downloader)

core.lg('')
core.lg('Checking CSV is Ready')
if os.path.exists(csv_file):
    core.lg('  SUCCESS - File Exists: ' + str(csv_file))
    
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    %matplotlib inline
    import numpy as np
    core.lg('Reading CSV with Pandas')
    # handle date formats and the special tab-character on the header row with utf-8-sig
    dateparse = lambda x: pd.datetime.strptime(x, '%d-%b-%y')
    data = pd.read_csv(csv_file, parse_dates=[0], date_parser=dateparse, encoding='utf-8-sig')
     
    core.lg('')
    core.lg('Going to the Head')
    print data.head()

    core.lg('')
    core.lg('Describing the Data', 2)
    core.lg(data.describe(), 6)
    
    core.lg('')
    
    # Set the size of the figure
    plt.rcParams['figure.figsize'] = (15.0, 10.0)
    
    all_dates   = data.columns.values[0]
    all_closes  = data.columns.values[4]
    core.lg('Plotting the Data X-axis(' + str(all_dates) + ') Y-axis(' + str(all_closes) + ')', 5)
    by_close_figure = data.plot(all_dates, all_closes)
    
else:
    core.lg('  ERROR: Failed to find CSV(' + str(csv_file) + ')', 0)
# end of if/else download was successful