def monthlyreturns(self, list_of_symbols, startdate_string='2005-01-01', showresults=0): #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI import pullreturns as pr dict_of_dfs = {} mysymbolslist = list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] for symbol in mysymbolslist: df = pr.monthlyreturnsusingyahoosymbol(symbol, startdate_string) dict_of_dfs[symbol] = df #df = pr.monthlyreturnsusingyahoosymbol('^GSPC','2005-01-01') #dict_of_dfs['^GSPC'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^DJI','2005-01-01') #dict_of_dfs['^DJI'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^MID','2005-01-01') #dict_of_dfs['^MID'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^VIX','2005-01-01') #dict_of_dfs['^VIX'] = df passed = 0 for k, v in dict_of_dfs.items(): if passed == 0: df_align = v[['b_monthend', 'e_pctchange']] df_align = df_align.set_index('b_monthend') df_align.columns = [k] #sLength = len(df_align[k]) #originalid = k else: df_new = v[['b_monthend', 'e_pctchange']] df_new = df_new.set_index('b_monthend') df_new.columns = [k] df_new.sort_index #print df_new #df_align[k] = df_new.loc[k].shape[0] #print df_new #df_align[k] = pd.Series(df_new, index=df_align.index) #df_align[k] = df_align[originalid].map(lambda x: df_new[k]) df_align[k] = df_new[k] passed = 1 if showresults == 1: print '----------------------------------------------------' print ' monthly returns' print '----------------------------------------------------' print df_align #self.DataFrameMonthlyReturns = df_align return df_align
#x = np.array([[0, 2], [1, 1], [2, 0]]).T #print np.cov(x) #import pandas as pd #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI import numpy as np import pandas as pd import pullreturns as pr dict_of_dfs = {} mysymbolslist = ['^GSPC', '^DJI', '^MID', '^OEX', 'AAPL', 'LEO'] for symbol in mysymbolslist: df = pr.monthlyreturnsusingyahoosymbol(symbol, '2005-01-01') dict_of_dfs[symbol] = df #df = pr.monthlyreturnsusingyahoosymbol('^GSPC','2005-01-01') #dict_of_dfs['^GSPC'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^DJI','2005-01-01') #dict_of_dfs['^DJI'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^MID','2005-01-01') #dict_of_dfs['^MID'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^VIX','2005-01-01') #dict_of_dfs['^VIX'] = df passed = 0
# [5, 500, 0], # [1, 100, 7]]) # #print np.cov(x) import pandas as pd import numpy as np x = np.array([[0, 2], [1, 1], [2, 0]]).T print np.cov(x) #import pandas as pd #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI import pullreturns as pr dict_of_dfs = {} df = pr.monthlyreturnsusingyahoosymbol('^GSPC', '2005-01-01') dict_of_dfs['^GSPC'] = df df = pr.monthlyreturnsusingyahoosymbol('^DJI', '2005-01-01') dict_of_dfs['^DJI'] = df df = pr.monthlyreturnsusingyahoosymbol('^MID', '2005-01-01') dict_of_dfs['^MID'] = df df = pr.monthlyreturnsusingyahoosymbol('^VIX', '2005-01-01') dict_of_dfs['^VIX'] = df passed = 0 for k, v in dict_of_dfs.items(): if passed == 0: df_align = v[['b_monthend', 'e_pctchange']]
def monthlyreturns(self, list_of_symbols, startdate_string='2005-01-01', showresults=0): #'^GSmr ^OEX ^VIX ^OEX ^MID ^RUT ^DJI import pullreturns as pr dict_of_dfs = {} mysymbolslist = list_of_symbols #['^GSPC','^DJI','^MID','^OEX','AAPL','LEO'] for symbol in mysymbolslist: df = pr.monthlyreturnsusingyahoosymbol(symbol, startdate_string) dict_of_dfs[symbol] = df #df = pr.monthlyreturnsusingyahoosymbol('^GSPC','2005-01-01') #dict_of_dfs['^GSPC'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^DJI','2005-01-01') #dict_of_dfs['^DJI'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^MID','2005-01-01') #dict_of_dfs['^MID'] = df # #df = pr.monthlyreturnsusingyahoosymbol('^VIX','2005-01-01') #dict_of_dfs['^VIX'] = df #passed = 0 #import datetime import pandas as pd #import numpy as np #todays_date = datetime.datetime.now().date() #index = pd.date_range(todays_date-datetime.timedelta(10), periods=10, freq='D') index = ['X'] columns = ['A', 'B', 'C'] df_largest = pd.DataFrame(index=index, columns=columns) df_largest = df_largest.fillna(0) # with 0s rather than NaNs #print df_largest #while len(dict_of_dfs_bysize) < len(dict_of_dfs): keyoflargestdf = '' for k, v in dict_of_dfs.items(): if len(v) > len(df_largest): df_largest = v keyoflargestdf = k #break df_align = df_largest[['b_monthend', 'e_pctchange']] df_align = df_align.set_index('b_monthend') df_align.columns = [keyoflargestdf] df_align.sort_index #print df_align #print df_largest # if passed == 0: # df_align = v[['b_monthend','e_pctchange']] # df_align = df_align.set_index('b_monthend') # df_align.columns = [k] # df_align.sort_index # #sLength = len(df_align[k]) # #originalid = k # # else: for k, v in dict_of_dfs.items(): if not k == keyoflargestdf: df_new = v[['b_monthend', 'e_pctchange']] df_new = df_new.set_index('b_monthend') df_new.columns = [k] df_new.sort_index #print df_new #df_align[k] = df_new.loc[k].shape[0] #print df_new #df_align[k] = pd.Series(df_new, index=df_align.index) #df_align[k] = df_align[originalid].map(lambda x: df_new[k]) df_align[k] = df_new[k] if showresults == 1: print '----------------------------------------------------' print ' monthly returns' print '----------------------------------------------------' print df_align #self.DataFrameMonthlyReturns = df_align return df_align