''' Created on Dec 16, 2014 @author: Brett Paufler Copyright Brett Paufler ''' import pandas as pd import os import sqlite3 from pandas.io import sql pd.set_option('expand_frame_repr',False) tdays = {0:"Monday", 1:"Tuesday", 2:"Wednesday", 3:"Thursday", 4:"Friday"} prefix = {"corn":"CZ", "soybeans":"SX"} weekdayList = ["Monday","Tuesday","Wednesday","Thursday","Friday"] cM = {"corn": "./corn/", "soybeans":"./soybean/", } graphList = [["corn",2014], ["corn",range(1960,2015)], ["soybeans",2014], ["soybeans",range(1970,2015)] ] def report(dF): ''' Quick Debugging Screen Output to visualize current status of dataFrame ''' print "Quick Report %s" % str(dF.shape) print dF.head() def get_trades(commodity): ''' returns all trades of given commodity, no filter for year if only want trades for year in which issue expires, see by_issue_year ''' if commodity not in cM: raise Exception ("%s Not a Valid Commodity Type (check spelling, mapping)") tA = [] for f in os.listdir(cM[commodity]): print "Extracting Data from %s" % f if commodity == "corn": issueName = f[f.find("CZ"):] issueName = issueName[:6] if commodity == "soybeans": issueName = f[f.find("SX"):] issueName = issueName[:6] if not issueName: raise Exception("Error defining issueName for %s" % commodity) t = pd.read_csv(cM[commodity] + f, parse_dates=['Date']) t["Issue"] = issueName tA.append(t) dF = pd.concat(tA) print "\n\n" print "%s returned from get_trades: shape %s" % (commodity,str(dF.shape)) print dF.head(2) print "..." print dF.tail(2) return dF def by_issue_year(commodity="corn", years=range(1960,2016)): ''' returns a dataFrame in which the Issue and Year are the same by_year("corn", 2015) returns all dates in 2015 for CZ2015 by_year("soybeans", [2013,2014]) returns all dates in 2013 for SX2013 and in 2014 for SX2014 by_year(commodity="corn", years=range(1960,2016)) returns all corn trades from 1960 to 2015 for the year in which the issue comes due ''' if isinstance(years, int): years = [years] dF = get_trades(commodity) temp = [] for year in years: t = dF[dF.Issue == "%s%d" % (prefix[commodity],year)] #trades.report(t) t = t[t.Date > "%d-1-1" % year] #trades.report(t) t = t[t.Date < "%d-12-31" % year] #trades.report(t) temp.append(t) dF = pd.concat(temp) dF = dF.set_index(dF["Date"], verify_integrity=True) #report(dF) return dF if __name__ == '__main__': dF = get_trades('soybeans') print dF.head() ''' #Find Specific Issue dF = dF[dF["Issue"].isin(["SX2015"])] #Find Series of Issues dF = dF[dF["Issue"].isin(["SX2014","SX2015"])] #Twenty Day Moving Average dF['Moving Average'] = pd.stats.moments.rolling_mean(dF['Settle'], window=20) print dF.head() print dF.tail() print dF.shape #simplest graph dF.plot() plt.show() plt.savefig(outputName) '''