# Feed to matrix subDict = dict() for sym in instruments: f = feed.getBars(sym) adjCloseList = [] index = [] for bar in f: adjCloseList.append(bar.getAdjClose()) index.append(bar.getDateTime()) # print "{0} {1}".format(bar.getDateTime(), bar.getAdjClose()) subDict[sym] = pd.Series(adjCloseList, index=index) tickerDF = pd.DataFrame(subDict) # Then make a new data frame with just the adjusted closes. r = getPeriodicReturn(tickerDF) print "Annualized returns:\n{0}\n".format((1+r.mean())**252 - 1) print "Annualized StdDev:\n{0}\n".format(r.std()*np.sqrt(252)) print "Correlation Coeff:\n{0}\n".format(r.corr()) def MultiplyReturnsByFactor(r, slope, initAdj, initDate=None): # Now make a psuedo-TQQQ that goes back as far as QQQ, well to 2007 # fakeReturn = r["QQQ"]*slope fakeAdjList = [] fakeIndex = [] # Initialize fakeAdjList.append(initAdj) # fakeAdjList.append(tickerDF["TQQQ"][-1])initAdj # index.append(tickerDF["TQQQ"].index[-1])
# if (len(df.shape) == 1) or (df.shape[1] == 1): # periodicReturns = df.diff().iloc[1:].as_matrix() / df.iloc[:-1].as_matrix() # x = df.copy() # copy for answer # x.iloc[1:] = periodicReturns # x.iloc[0] = 0. # else: # periodicReturns = df.diff().iloc[1:,:].as_matrix() / df.iloc[:-1, :].as_matrix() # x = df.copy() # copy for answer # x.iloc[1:,:] = periodicReturns # x.iloc[0,:] = 0. # return x periodicReturnsDF = getPeriodicReturn(backTestDF) # Plot the stocks filename = "growth.html" plotTitle = "Portfolio Growth" plotTimeSeriesDF(filename, plotTitle, backTestDF / backTestDF.irow(0)) empRate = periodicReturnsDF.portVal.mean() empStd = periodicReturnsDF.portVal.std() annualReturnEmpirical = (empRate + 1)**52 annualSigmaEmpirical = e(empRate*52) * (e(52* empStd**2)-1)**0.5 print zip(tickerDF.keys(), targetWeight) print annualReturnEmpirical, annualSigmaEmpirical