def setclassdictionaries(self, list_of_tickers=[], closepricesfilepath='', fromdate='2017-01-01', todate='2017-12-31'): ana_class = ana.analyze() print 'started def setclassdictionaries' #import pandas as pd if len(closepricesfilepath) == 0: print 'go to pull prices' import pullprices as pp o = pp.pull() o.setclassdataframes(symbols=list_of_tickers, fromdate=fromdate, todate=todate) df = o.ClosePricesDataframe else: myfile = closepricesfilepath #'C:\Batches\GitStuff\$work\closeprices_sample.csv' df = pd.read_csv(myfile) df.set_index("Date", drop=True, inplace=True) if len(list_of_tickers) == 0: columns = list(df.columns.values) else: columns = list_of_tickers df = df[columns] self.ClosePricesDataframe = df self.SymbolsList = columns print 'self.SymbolsList = columns', self.SymbolsList dict_pairdiff_prices = {} dict_pairhr_prices = {} i2 = 0 for s1 in columns: print 'setclassdictionaries', s1 df_diff_prices = df[columns].sub(df[s1], axis=0) dict_pairdiff_prices[s1] = df_diff_prices i3 = 0 for s2 in columns: if not s1 == s2: df_hr_prices = df[columns].div(df[s1], axis=0) dict_pairhr_prices[s1] = df_hr_prices i2 += 1 #stop i2 += 1 self.PairPricesDiffDictionary = dict_pairdiff_prices self.PairPricesHedgeRatioDictionary = dict_pairhr_prices print 'finished def setclassdictionaries' return True
def setbetahrdictionary(self,list_of_tickers = [],closepricesfilepath = '',fromdate = '2017-01-01',todate = '2017-12-31'): ana_class = ana.analyze() print 'started def setbetahrdictionary' #import pandas as pd if len(closepricesfilepath) == 0: print 'go to pull prices' import pullprices as pp o = pp.pull() o.setclassdataframes(symbols=list_of_tickers,fromdate = fromdate,todate = todate) df = o.ClosePricesDataframe else: myfile = closepricesfilepath #'C:\Batches\GitStuff\$work\closeprices_sample.csv' df = pd.read_csv(myfile) df.set_index("Date", drop=True, inplace=True) if len(list_of_tickers) == 0: columns = list(df.columns.values) else: columns = list_of_tickers df = df[columns] self.ClosePricesDataframe = df self.SymbolsList = columns print 'self.SymbolsList = columns', self.SymbolsList dict_pairdiff_prices = {} dict_pairhr_prices = {} dict_pairbetahr_prices = {} i2 = 0 for s1 in columns: print 'setclassdictionaries',s1 #df_diff_prices = df[columns].sub(df[s1], axis=0) #dict_pairdiff_prices[s1] = df_diff_prices #df_hr_prices = df[columns].div(df[s1], axis=0) #print 'df_hr_prices',df_hr_prices #dict_pairhr_prices[s1] = df_hr_prices df_betahr_group = pd.DataFrame(index = df.index) i3 = 0 for s2 in columns: if 1 == 1: i2 +=1 #stop Y=df[s2].tolist() X=df[s1].tolist() betahr_list = [] i0 = 0 for idx, rows in df.iterrows(): #print idx if i0 >= 0: res = sm.OLS(Y[:i0+1],X[:i0+1]) betahr = res.fit().params[0] mydict = {'Date':idx,s2:betahr} betahr_list.append(mydict) i0 += 1 #if i0 >= 10: # stop df_betahr = pd.DataFrame(betahr_list) df_betahr.set_index("Date", drop=True, inplace=True) #print 'df_betahr',s1,s2,df_betahr df_betahr_group = pd.concat([df_betahr_group, df_betahr], axis=1) #print 'df_betahr_group',s1,s2,df_betahr_group #stop #df_b['a-b'] = df_b['actual']- df_b['betahr'] #df_b["res"] = df_b[s2] - df_b['betahr']*df_b[s1] dict_pairbetahr_prices[s1] = df_betahr_group print 'betahr',s1,dict_pairbetahr_prices[s1] i2 +=1 #stop #self.PairPricesDiffDictionary = dict_pairdiff_prices #self.PairPricesHedgeRatioDictionary = dict_pairhr_prices self.PairPricesBetaHedgeRatioDictionary = dict_pairbetahr_prices print 'finished def setbetahrdictionary' return True