def compilereturnsdataframe(self , showresults = 0): #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI # if self.Period == 'daily': # import pullreturnsdaily as pr # else: # import pullreturnsmonthly as pr import pullreturnsbyperiod as pr dict_of_df_pricehistory = {} dict_of_df_returns = {} # Build the annualized returns series mysymbolslist = self.SymbolsList # list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] ser_annual = pd.Series() for symbol in mysymbolslist: o = pr.perform(symbol,self.StartDateString,self.Period) #print o.ReturnsDataframe[['e_'+ self.PctChangeOrLogReturn]] #df_pricehistorydataframe = o.PriceHistoryDataframe #dfcompilereturnsdataframe = o.ReturnsDataframe dict_of_df_pricehistory[symbol] = o.PriceHistoryDataframe dict_of_df_returns[symbol] = o.ReturnsDataframe annualizedreturn = o.annualizedreturn() ser_annual = ser_annual.set_value(symbol, annualizedreturn) self.AnnualizedReturnsSeries = ser_annual #mysymbolslist = self.SymbolsList # list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] #ser_annual = pd.Series() # for symbol in mysymbolslist: # df_pricehistory = dict_of_df_pricehistory[symbol] # df_returns = dict_of_df_returns[symbol] # o = pr.perform(symbol,self.StartDateString) # df = o.ReturnsDataframe # dict_of_dfs[symbol] = df index = ['X'] columns = ['A','B', 'C'] df_largestofreturns = pd.DataFrame(index=index, columns=columns) df_largestofreturns = df_largestofreturns.fillna(0) # with 0s rather than NaNs #print df_largestofreturns #while len(dict_of_dfs_bysize) < len(dict_of_dfs): keyoflargestreturnsdf = '' for k,v in dict_of_df_returns.items(): if len(v) > len(df_largestofreturns): df_largestofreturns = v keyoflargestreturnsdf = k #break #wwwwww # print df_largestofreturns # print keyoflargestreturnsdf df_alignedreturns = df_largestofreturns[['e_'+ self.PctChangeOrLogReturn]] #df_alignedreturns = df_alignedreturns.set_index('b_periodend') df_alignedreturns.columns = [keyoflargestreturnsdf] df_alignedreturns.sort_index #print df_alignedreturns #print df_largestofreturns # # if passed == 0: # df_alignedreturns = v[['b_periodend','e_pctchange']] # df_alignedreturns = df_alignedreturns.set_index('b_periodend') # df_alignedreturns.columns = [k] # df_alignedreturns.sort_index # #sLength = len(df_alignedreturns[k]) # #originalid = k # # else: for k,v in dict_of_df_returns.items(): if not k == keyoflargestreturnsdf: df_new = v[['e_' + self.PctChangeOrLogReturn]] #df_new = df_new.set_index('b_periodend') df_new.columns = [k] df_new.sort_index #print df_new #df_alignedreturns[k] = df_new.loc[k].shape[0] #print df_new #df_alignedreturns[k] = pd.Series(df_new, index=df_alignedreturns.index) #df_alignedreturns[k] = df_alignedreturns[originalid].map(lambda x: df_new[k]) df_alignedreturns[k] = df_new[k] if showresults == 1: print '----------------------------------------------------' print ' returns' print '----------------------------------------------------' print df_alignedreturns #print df_alignedreturns #self.MonthlyReturnsDataframe = df_alignedreturns return df_alignedreturns
def compilehistoricaldataframes(self , showresults = 0): #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI # if self.Period == 'daily': # import pullreturnsdaily as pr # else: # import pullreturnsmonthly as pr import pullreturnsbyperiod as pr dict_of_df_pricehistory = {} dict_of_df_returns = {} # Build the annualized returns series mysymbolslist = self.SymbolsList # list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] ser_annual = pd.Series() for symbol in mysymbolslist: o = pr.perform(symbol,self.StartDateString,self.Period) dict_of_df_pricehistory[symbol] = o.PriceHistoryDataframe dict_of_df_returns[symbol] = o.ReturnsDataframe annualizedreturn = o.annualizedreturn() ser_annual = ser_annual.set_value(symbol, annualizedreturn) self.AnnualizedReturnsSeries = ser_annual index = ['X'] columns = ['A','B', 'C'] # ---------------------------------------------------------------------- df_largestofreturns = pd.DataFrame(index=index, columns=columns) df_largestofreturns = df_largestofreturns.fillna(0) # with 0s rather than NaNs keyoflargestreturnsdf = '' for k,v in dict_of_df_returns.items(): if len(v) > len(df_largestofreturns): df_largestofreturns = v keyoflargestreturnsdf = k df_alignedreturns = df_largestofreturns[['e_'+ self.PctChangeOrLogReturn]] df_alignedreturns.columns = [keyoflargestreturnsdf] df_alignedreturns.sort_index for k,v in dict_of_df_returns.items(): if not k == keyoflargestreturnsdf: df_new = v[['e_' + self.PctChangeOrLogReturn]] df_new.columns = [k] df_new.sort_index df_alignedreturns[k] = df_new[k] self.ReturnsDataframe = df_alignedreturns # ---------------------------------------------------------------------- df_largestofpricehistory = pd.DataFrame(index=index, columns=columns) df_largestofpricehistory = df_largestofpricehistory.fillna(0) # with 0s rather than NaNs keyoflargestpricehistorydf = '' for k,v in dict_of_df_pricehistory.items(): if len(v) > len(df_largestofpricehistory): df_largestofpricehistory = v keyoflargestpricehistorydf = k df_alignedpricehistory = df_largestofpricehistory[['e_adjclose']] df_alignedpricehistory.columns = [keyoflargestpricehistorydf] df_alignedpricehistory.sort_index for k,v in dict_of_df_pricehistory.items(): if not k == keyoflargestpricehistorydf: df_new = v[['e_adjclose']] df_new.columns = [k] df_new.sort_index df_alignedpricehistory[k] = df_new[k] self.PriceHistoryDataframe = df_alignedpricehistory
def pricehistorydataframe(self , period = 'monthly' # or daily , showresults = 0): #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI import pullreturnsbyperiod as pr # if self.Period == 'daily': # import pullreturnsdaily as pr # else: # import pullreturnsmonthly as pr dict_of_dfs = {} print 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Build the annualized returns series mysymbolslist = self.SymbolsList # list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] #ser_annual = pd.Series() for symbol in mysymbolslist: o = pr.perform(symbol,self.StartDateString,period) df = o.PriceHistoryDataframe dict_of_dfs[symbol] = df #annualizedreturn = o.annualizedreturn() #ser_annual = ser_annual.set_value(symbol, annualizedreturn) # if not showresults == 0: # print '----- ser_annual -----' # print ser_annual #print 'returns dataframe ready' #self.AnnualizedReturnsSeries = ser_annual # mysymbolslist = self.SymbolsList # list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] # #ser_annual = pd.Series() # for symbol in mysymbolslist: # o = pr.perform(symbol,self.StartDateString) # df = o.ReturnsDataframe # dict_of_dfs[symbol] = df index = ['X'] columns = ['A','B', 'C'] df_largestofreturns = pd.DataFrame(index=index, columns=columns) df_largestofreturns = df_largestofreturns.fillna(0) # with 0s rather than NaNs #print df_largestofreturns #while len(dict_of_dfs_bysize) < len(dict_of_dfs): keyoflargestreturnsdf = '' for k,v in dict_of_dfs.items(): if len(v) > len(df_largestofreturns): df_largestofreturns = v keyoflargestreturnsdf = k break print '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^', keyoflargestreturnsdf df_alignedreturns = df_largestofreturns[['e_adjclose']] #print df_alignedreturns df_alignedreturns.columns = [keyoflargestreturnsdf] #df_alignedreturns = df_alignedreturns.set_index('b_periodend') #print df_alignedreturns df_alignedreturns.sort_index #print df_alignedreturns # if passed == 0: # df_alignedreturns = v[['b_periodend','e_pctchange']] # df_alignedreturns = df_alignedreturns.set_index('b_periodend') # df_alignedreturns.columns = [k] # df_alignedreturns.sort_index # #sLength = len(df_alignedreturns[k]) # #originalid = k # # else: for k,v in dict_of_dfs.items(): if not k == keyoflargestreturnsdf: df_new = v[['e_adjclose']] #print '----------------v--------------------' #print v #print '----------------df_new--------------------' #print df_new #df_new = df_new.set_index('b_periodend') df_new.columns = [k] #print df_new df_new.sort_index #print 'made it here' #print df_new #df_alignedreturns[k] = df_new.loc[k].shape[0] #print df_new #df_alignedreturns[k] = pd.Series(df_new, index=df_alignedreturns.index) #df_alignedreturns[k] = df_alignedreturns[originalid].map(lambda x: df_new[k]) df_alignedreturns[k] = df_new#[k] if showresults == 1: print '----------------------------------------------------' print ' stockhistory' print '----------------------------------------------------' print '------------------------------df_alignedreturns -------------------------' print df_alignedreturns #self.MonthlyReturnsDataframe = df_alignedreturns return df_alignedreturns