def print_industry_coer(fund_ts, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ industries = [['$DJUSBM', 'Materials'], ['$DJUSNC', 'Goods'], ['$DJUSCY', 'Services'], ['$DJUSFN', 'Financials'], ['$DJUSHC', 'Health'], ['$DJUSIN', 'Industrial'], ['$DJUSEN', 'Oil & Gas'], ['$DJUSTC', 'Technology'], ['$DJUSTL', 'TeleComm'], ['$DJUSUT', 'Utilities']] for i in range(0, len(industries) ): if(i%2==0): ostream.write("\n") #load data norObj = de.DataAccess('mysql') ldtTimestamps = du.getNYSEdays( fund_ts.index[0], fund_ts.index[-1], dt.timedelta(hours=16) ) ldfData = norObj.get_data( ldtTimestamps, [industries[i][0]], ['close'] ) #get corelation ldfData[0]=ldfData[0].fillna(method='pad') ldfData[0]=ldfData[0].fillna(method='bfill') a=np.corrcoef(np.ravel(tsu.daily(ldfData[0][industries[i][0]])),np.ravel(tsu.daily(fund_ts.values))) b=np.ravel(tsu.daily(ldfData[0][industries[i][0]])) f=np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b,f,1) ostream.write("%10s(%s):%+6.2f, %+6.2f " % (industries[i][1], industries[i][0], a[0,1], fBeta))
def print_industry_coer(fund_ts, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ industries = [['$DJUSBM', 'Materials'], ['$DJUSNC', 'Goods'], ['$DJUSCY', 'Services'], ['$DJUSFN', 'Financials'], ['$DJUSHC', 'Health'], ['$DJUSIN', 'Industrial'], ['$DJUSEN', 'Oil & Gas'], ['$DJUSTC', 'Technology'], ['$DJUSTL', 'TeleComm'], ['$DJUSUT', 'Utilities']] for i in range(0, len(industries)): if (i % 2 == 0): ostream.write("\n") #load data norObj = de.DataAccess('mysql') ldtTimestamps = du.getNYSEdays(fund_ts.index[0], fund_ts.index[-1], dt.timedelta(hours=16)) ldfData = norObj.get_data(ldtTimestamps, [industries[i][0]], ['close']) #get corelation ldfData[0] = ldfData[0].fillna(method='pad') ldfData[0] = ldfData[0].fillna(method='bfill') a = np.corrcoef(np.ravel(tsu.daily(ldfData[0][industries[i][0]])), np.ravel(tsu.daily(fund_ts.values))) b = np.ravel(tsu.daily(ldfData[0][industries[i][0]])) f = np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b, f, 1) ostream.write("%10s(%s):%+6.2f, %+6.2f " % (industries[i][1], industries[i][0], a[0, 1], fBeta))
def fundsAnalysisToPNG(funds,output_file): plt.clf() if(type(funds)!=type(list())): print 'fundsmatrix only contains one timeseries, not able to analyze.' #convert to daily returns count=list() dates=list() sum=list() for i in range(0,len(funds)): ret=tsu.daily(funds[i].values) for j in range(0, len(ret)): if (funds[i].index[j] in dates): sum[dates.index(funds[i].index[j])]+=ret[j] count[dates.index(funds[i].index[j])]+=1 else: dates.append(funds[i].index[j]) count.append(1) sum.append(ret[j]) #compute average tot_ret=deepcopy(sum) for i in range(0,len(sum)): tot_ret[i]=sum[i]/count[i] #compute std std=zeros(len(sum)) for i in range(0,len(funds)): temp=tsu.daily(funds[i].values) for j in range(0,len(temp)): std[dates.index(funds[i].index[j])]=0 std[dates.index(funds[i].index[j])]+=math.pow(temp[j]-tot_ret[dates.index(funds[i].index[j])],2) for i in range(1, len(std)): # std[i]=math.sqrt(std[i]/count[i])+std[i-1] std[i]=math.sqrt(std[i]/count[i]) #compute total returns lower=deepcopy(tot_ret) upper=deepcopy(tot_ret) tot_ret[0]=funds[0].values[0] lower[0]=funds[0].values[0] upper[0]=lower[0] # for i in range(1,len(tot_ret)): # tot_ret[i]=tot_ret[i-1]+(tot_ret[i])*tot_ret[i-1] # lower[i]=tot_ret[i-1]-(std[i])*tot_ret[i-1] # upper[i]=tot_ret[i-1]+(std[i])*tot_ret[i-1] for i in range(1,len(tot_ret)): lower[i]=(tot_ret[i]-std[i]+1)*lower[i-1] upper[i]=(tot_ret[i]+std[i]+1)*upper[i-1] tot_ret[i]=(tot_ret[i]+1)*tot_ret[i-1] plt.clf() plt.plot(dates,tot_ret) plt.plot(dates,lower) plt.plot(dates,upper) plt.legend(('Tot_Ret','Lower','Upper'),loc='upper left') plt.ylabel('Fund Total Return') plt.ylim(ymin=0,ymax=2*tot_ret[0]) plt.draw() savefig(output_file, format='png')
def print_other_coer(fund_ts, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ industries = [['$SPX', ' S&P Index'], ['$DJI', ' Dow Jones'], ['$DJUSEN', 'Oil & Gas'], ['$DJGSP', ' Metals']] for i in range(0, len(industries)): if (i % 2 == 0): ostream.write("\n") #load data norObj = de.DataAccess('mysql') ldtTimestamps = du.getNYSEdays(fund_ts.index[0], fund_ts.index[-1], dt.timedelta(hours=16)) ldfData = norObj.get_data(ldtTimestamps, [industries[i][0]], ['close']) #get corelation ldfData[0] = ldfData[0].fillna(method='pad') ldfData[0] = ldfData[0].fillna(method='bfill') a = np.corrcoef(np.ravel(tsu.daily(ldfData[0][industries[i][0]])), np.ravel(tsu.daily(fund_ts.values))) b = np.ravel(tsu.daily(ldfData[0][industries[i][0]])) f = np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b, f, 1) ostream.write("%10s(%s):%+6.2f, %+6.2f " % (industries[i][1], industries[i][0], a[0, 1], fBeta))
def print_other_coer(fund_ts, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ industries = [['$SPX', ' S&P Index'], ['$DJI', ' Dow Jones'], ['$DJUSEN', 'Oil & Gas'], ['$DJGSP', ' Metals']] for i in range(0, len(industries) ): if(i%2==0): ostream.write("\n") #load data norObj =de.DataAccess('mysql') ldtTimestamps = du.getNYSEdays( fund_ts.index[0], fund_ts.index[-1], dt.timedelta(hours=16) ) ldfData = norObj.get_data( ldtTimestamps, [industries[i][0]], ['close'] ) #get corelation ldfData[0]=ldfData[0].fillna(method='pad') ldfData[0]=ldfData[0].fillna(method='bfill') a=np.corrcoef(np.ravel(tsu.daily(ldfData[0][industries[i][0]])),np.ravel(tsu.daily(fund_ts.values))) b=np.ravel(tsu.daily(ldfData[0][industries[i][0]])) f=np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b,f,1) ostream.write("%10s(%s):%+6.2f, %+6.2f " % (industries[i][1], industries[i][0], a[0,1], fBeta))
def daily_return(value): # daily_ret = [] # daily_ret.append(0) #the daily_return of the first trading day # for i in range(1,len(value)): # daily_ret.append((value[i]/value[i-1])-1) # return daily_ret return tsu.daily(value)
def get_sharpe_ratio(fund_ts): """ @summary Returns daily computed Sharpe ratio of fund time series @param fund_ts: pandas time series of daily fund values @return Sharpe ratio of fund time series """ return tsu.get_sharpe_ratio(tsu.daily(fund_ts))
def get_winning_days(fund_ts): """ @summary Returns percentage of winning days in fund time series @param fund_ts: pandas time series of daily fund values @return Percentage of winning days over fund time series """ return tsu.get_winning_days(tsu.daily(fund_ts))
def print_benchmark_coer(fund_ts, benchmark_close, sym, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ fund_ts=fund_ts.fillna(method='pad') fund_ts=fund_ts.fillna(method='bfill') benchmark_close=benchmark_close.fillna(method='pad') benchmark_close=benchmark_close.fillna(method='bfill') faCorr=np.corrcoef(np.ravel(tsu.daily(fund_ts.values)),np.ravel(tsu.daily(benchmark_close))); b=np.ravel(tsu.daily(benchmark_close)) f=np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b,f, 1); print_line(sym+"Correlattion","%+6.2f" % faCorr[0,1],i_spacing=3,ostream=ostream) print_line(sym+"Beta","%+6.2f" % fBeta,i_spacing=3,ostream=ostream)
def get_std_dev(fund_ts): """ @summary gets standard deviation of returns for a fund as a string @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ fund_ts = fund_ts.fillna(method='pad') fund_ts = fund_ts.fillna(method='bfill') ret = np.std(tsu.daily(fund_ts.values)) * 10000 return ("%+7.2f bps " % ret)
def get_std_dev(fund_ts): """ @summary gets standard deviation of returns for a fund as a string @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ fund_ts=fund_ts.fillna(method='pad') fund_ts=fund_ts.fillna(method='bfill') ret=np.std(tsu.daily(fund_ts.values))*10000 return ("%+7.2f bps " % ret)
def print_benchmark_coer(fund_ts, benchmark_close, sym, ostream): """ @summary prints standard deviation of returns for a fund @param fund_ts: pandas fund time series @param years: list of years to print out @param ostream: stream to print to """ fund_ts = fund_ts.fillna(method='pad') fund_ts = fund_ts.fillna(method='bfill') benchmark_close = benchmark_close.fillna(method='pad') benchmark_close = benchmark_close.fillna(method='bfill') faCorr = np.corrcoef(np.ravel(tsu.daily(fund_ts.values)), np.ravel(tsu.daily(benchmark_close))) b = np.ravel(tsu.daily(benchmark_close)) f = np.ravel(tsu.daily(fund_ts)) fBeta, unused = np.polyfit(b, f, 1) print_line(sym + "Correlattion", "%+6.2f" % faCorr[0, 1], i_spacing=3, ostream=ostream) print_line(sym + "Beta", "%+6.2f" % fBeta, i_spacing=3, ostream=ostream)
def getWinningDays(funds1, funds2, year): days = [] i = 0 win = 0 tot = 0 f1ret = tsu.daily(funds1) f2ret = tsu.daily(funds2) relf1 = [] relf2 = [] for date in funds1.index: if (date.year == year): for date2 in funds2.index: if (date == date2): relf1.append(f1ret[i]) relf2.append(f2ret[i]) i += 1 for i in range(0, len(relf1)): if (f1ret[i] > f2ret[i]): win += 1 tot += 1 return float(win) / tot
def getWinningDays(funds1,funds2,year): days=[] i=0; win=0 tot=0 f1ret=tsu.daily(funds1) f2ret=tsu.daily(funds2) relf1=[] relf2=[] for date in funds1.index: if(date.year==year): for date2 in funds2.index: if(date==date2): relf1.append(f1ret[i]) relf2.append(f2ret[i]) i+=1 for i in range(0,len(relf1)): if(f1ret[i]>f2ret[i]): win+=1 tot+=1 return float(win)/tot
tick.label.set_fontsize(6) plt.plot(timestamp, values) savefig(figureName,format='pdf') # # Compute Cumulative Return, Sharpe ratio, std of daily return for the portfolio # if debug: print "-----Before calculate-----" for v in values: print v daily_returns0 = tsu.daily(values) if debug: print "-----After calculate-----" for v in daily_returns0: print v final_return = values[len(values)-1]/values[0]-1 print "Total Return:" , final_return mean = np.average(daily_returns0) print "Expected Return:", mean std2 = np.std(daily_returns0) print "Standard Deviation:", std2 sharp_ratio = tsu.get_sharpe_ratio(daily_returns0)
def analyzeValue(valueFilename=valueFilename, benchmark=benchmark,figureName=figureName, closefield=closefield): valueCSV = open(valueFilename, 'rU') reader = csv.reader(valueCSV, delimiter=',') timestamp = [] values = [] for row in reader: timestamp.append(dt.date(int(row[0]),int(row[1]),int(row[2]))) values.append(float(row[3])) # # Plot portfolio value plt.clf() fig = plt.figure() ax = fig.add_subplot(111) ax.plot(timestamp, values) # adjust tick size for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(6) plt.plot(timestamp, values) savefig(figureName,format='pdf') # # Compute Cumulative Return, Sharpe ratio, std of daily return for the portfolio # if debug: print "-----Before calculate-----" for v in values: print v daily_returns0 = tsu.daily(values) if debug: print "-----After calculate-----" for v in daily_returns0: print v final_return = values[len(values)-1]/values[0]-1 print "Total Return:" , final_return mean = np.average(daily_returns0) print "Expected Return:", mean std2 = np.std(daily_returns0) print "Standard Deviation:", std2 sharp_ratio = tsu.get_sharpe_ratio(daily_returns0) print "Calculated Sharpe Ratio:", tsu.sqrt(252) * mean/std2 print "Get Sharpe Ration from QSTK:", sharp_ratio # # Calculate Benchmark metrics # timeofday = dt.timedelta(hours=h) timestamps = du.getNYSEdays(timestamp[0],timestamp[len(timestamp)-1],timeofday) dataobj = da.DataAccess('Yahoo') print "reading benchmark data...",benchmark close = dataobj.get_data(timestamps, benchmark, closefield) if debug: for time in timestamps: print time, close[benchmark[0]][time] close = (close.fillna(method='ffill')).fillna(method='backfill') bValues = close.values print final_return_b = (bValues[len(bValues)-1]/bValues[0])-1 print benchmark,"Total Return:" , final_return_b # daily returns b_daily_returns0 = tsu.daily(bValues) mean = np.average(b_daily_returns0) print benchmark, "Expected Return:", mean std2 = np.std(b_daily_returns0) print benchmark,"Standard Deviation:", std2 sharp_ratio = tsu.get_sharpe_ratio(b_daily_returns0) print benchmark,"Calculated Sharpe Ratio:", tsu.sqrt(252) * mean/std2 print benchmark,"Get Sharpe Ration from QSTK:", sharp_ratio