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('Yahoo') 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('Yahoo') 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 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 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 simulate(startdate, enddate, symbols, allocation, type): timeofday = dt.timedelta(hours=16) timestamps = du.getNYSEdays(startdate, enddate, timeofday) c_dataobj = da.DataAccess('Yahoo') ls_keys = [type] ldf_data = c_dataobj.get_data(timestamps, symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) #print d_data.values na_price = d_data[type].values print 'price', na_price[0], na_price[-1] #print 'na', na_price #print 'na_price', na_price normalized_price = na_price / na_price[0, :] #print 'norm', normalized_price c = normalized_price * allocation #print 'n*a', allocation, c invest = c.sum(axis=1) #print 'c', c #print 'invesi', invest daily_returns = tsu.daily(invest) mean_daily_return = np.mean(daily_returns) stdev_daily_return = np.std(daily_returns) sharpe_ratio = tsu.get_sharpe_ratio(daily_returns) return stdev_daily_return, mean_daily_return, sharpe_ratio, invest[-1]
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 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 simulate(distro, data): prices = data['close'].values normal_prices = normalize(prices) adj_values = alloc_adj(normal_prices.copy(), distro) daily_values = daily_value(adj_values) daily_rets = tsu.daily(daily_values) volatility = np.std(daily_rets) sharpe = tsu.get_sharpe_ratio(daily_rets)[0] cum_ret = np.sum(daily_rets, axis=0)[0] avg_daily_ret = cum_ret / len(daily_rets) return daily_rets, volatility, avg_daily_ret, sharpe, (cum_ret + 1)
def run(values, symbol): #data = pd.read_csv(values, header=False) data = np.loadtxt(values, delimiter=';', skiprows=0) values = data[:,3] #values = data.values[:,3] #invest = values.sum(axis=1) normalized_price = values / values[0] daily_returns = tsu.daily(normalized_price) mean_daily_return = np.mean(daily_returns) stdev_daily_return = np.std(daily_returns) sharpe_ratio = tsu.get_sharpe_ratio(daily_returns) total_return = values[-1] / values[0] print 'returns', sharpe_ratio, total_return, stdev_daily_return, mean_daily_return startdate = dt.datetime(int(data[0][0]), int(data[0][1]), int(data[0][2]), 16) enddate = dt.datetime(int(data[-1][0]), int(data[-1][1]), int(data[-1][2]), 16) allocation = [1.0] print 'SPX', startdate, enddate, symbol, allocation result = sm.simulate(startdate, enddate, [symbol], allocation, 'close') print 'result', result[2], result[3], result[0], result[1]
def run(values, symbol): #data = pd.read_csv(values, header=False) data = np.loadtxt(values, delimiter=';', skiprows=0) #print 'Data', data values = data[:,3] #print 'Values', values #values = data.values[:,3] #invest = values.sum(axis=1) normalized_price = values / values[0] daily_returns = tsu.daily(normalized_price) mean_daily_return = np.mean(daily_returns) stdev_daily_return = np.std(daily_returns) sharpe_ratio = tsu.get_sharpe_ratio(daily_returns) total_return = values[-1] / values[0] print 'Total', values[-1], values[0] # print 'returns', sharpe_ratio, total_return, stdev_daily_return, mean_daily_return startdate = dt.datetime(int(data[0][0]), int(data[0][1]), int(data[0][2]), 16) enddate = dt.datetime(int(data[-1][0]), int(data[-1][1]), int(data[-1][2]), 16) allocation = [1.0] # print 'SPX', startdate, enddate, symbol, allocation result = sm.simulate(startdate, enddate, [symbol], allocation, 'close') # print 'result', result[2], result[3], result[0], result[1] f = data[-1] print 'The final value of the portfolio using the sample file is -- %d-%d-%d -> %.2f ' % (f[0], f[1], f[2], f[3]) #2009,12,28,54824.0 print '' print 'Details of the Performance of the portfolio' print '' print 'Data Range : %d-%d-%d to %d-%d-%d' % (data[0][0], data[0][1], data[0][2], data[-1][0], data[-1][1], data[-1][2]) print '' print 'Sharpe Ratio of Fund : %.12f' % (sharpe_ratio)# 0.527865227084 print 'Sharpe Ratio of $SPX : %.12f'% (result[2])#-0.184202673931 print '' print 'Total Return of Fund %.12f:' % (total_return)# 1.09648 print 'Total Return of $SPX %.12f:' % (result[3])# 0.779305674563' print '' print 'Standard Deviation of Fund : %.12f' % stdev_daily_return# 0.0060854156452 print 'Standard Deviation of $SPX : %.12f' % result[0]#0.022004631521' print '' print 'Average Daily Return of Fund : %.12f' % mean_daily_return# 0.000202354576186 print 'Average Daily Return of $SPX : %.12f' % result[1]#-0.000255334653467'
def print_performance( fund, bench): print "Details of the Performance of the portfolio :\n" print "Data Range : ", fund.index[0], " to " ,fund.index[-1],"\n" print "Sharpe Ratio of Fund : ", tsu.get_sharpe_ratio(tsu.daily(fund))[0] print "Sharpe Ratio of " + benchmark +" :", tsu.get_sharpe_ratio(tsu.daily(bench))[0],"\n" print 'Total Return of Fund :', (fund[-1] / fund[0] - 1) + 1 print 'Total Return of ' + benchmark + ' : ', (bench[-1] / bench[0] -1) + 1, "\n" print 'Standard Deviation of Fund : ' + str(np.std(tsu.daily(fund.values))) print 'Standard Deviation of ' + benchmark + ' : ', np.std(tsu.daily(bench.values)), "\n" print 'Average Daily Return of Fund : ', np.mean( tsu.daily(fund.values)) print 'Average Daily Return of ' + benchmark + ' : ', np.mean( tsu.daily(bench.values))
dataobj = da.DataAccess('Yahoo') close = dataobj.get_data(timestamps, [symbol], "close", verbose=True) close = close.fillna(method='ffill') close = close.fillna(method='bfill') return close[symbol] ts_fund = _csv_read_fund(sys.argv[1]) benchmark = sys.argv[2] bench_vals = _read_bench(benchmark, list(ts_fund.index)) # print bench_vals multiple = ts_fund[0] / bench_vals[0] bench_vals = bench_vals * multiple print "Details of the Performance of the portfolio" print 'Data Range : ', ts_fund.index[0], ' to ', ts_fund.index[-1] print 'Sharpe Ratio of Fund :', tsu.get_sharpe_ratio(tsu.daily(ts_fund))[0] print 'Sharpe Ratio of ' + benchmark + ' :', tsu.get_sharpe_ratio( tsu.daily(bench_vals))[0] print 'Total Return of Fund : ', (((ts_fund[-1] / ts_fund[0]) - 1) + 1) print 'Total Return of ' + benchmark + ' :', (((bench_vals[-1] / bench_vals[0]) - 1) + 1) print 'Standard Deviation of Fund : ', np.std(tsu.daily( ts_fund.values)) print 'Standard Deviation of ' + benchmark + ' :', np.std( tsu.daily(bench_vals.values)) print 'Average Daily Return of Fund : ', np.mean(tsu.daily( ts_fund.values)) print 'Average Daily Return of ' + benchmark + ' :', np.mean( tsu.daily(bench_vals.values))
close = dataobj.get_data(timestamps, [symbol], "close", verbose=True) close = close.fillna(method='ffill') close = close.fillna(method='bfill') return close[symbol] ts_fund = _csv_read_fund(sys.argv[1]) benchmark = sys.argv[2] bench_vals = _read_bench(benchmark, list(ts_fund.index)) # print bench_vals multiple = ts_fund[0] / bench_vals[0] bench_vals = bench_vals * multiple print "Details of the Performance of the portfolio" print 'Data Range : ', ts_fund.index[0], ' to ', ts_fund.index[-1] print 'Sharpe Ratio of Fund :', tsu.get_sharpe_ratio(tsu.daily(ts_fund))[0] print 'Sharpe Ratio of ' + benchmark + ' :', tsu.get_sharpe_ratio( tsu.daily(bench_vals))[0] print '\n' print 'Total Return of Fund : ', (((ts_fund[-1] / ts_fund[0]) - 1) + 1) print 'Total Return of ' + benchmark + ' :', (( (bench_vals[-1] / bench_vals[0]) - 1) + 1) print '\n' print 'Standard Deviation of Fund : ', np.std(tsu.daily(ts_fund.values)) print 'Standard Deviation of ' + benchmark + ' :', np.std( tsu.daily(bench_vals.values)) print '\n' print 'Average Daily Return of Fund : ', np.mean(tsu.daily(ts_fund.values)) print 'Average Daily Return of ' + benchmark + ' :', np.mean( tsu.daily(bench_vals.values))
def run(cash, orders, values, key): # print 'Thomas' data = pd.read_csv( orders, parse_dates=True, names=["year", "month", "day", "symbol", "transaction", "nr_shares", "empty"], header=0, ) data = data.sort(["year", "month", "day"]) old = data data.to_csv("regneark.csv") # print "Data2", data.values # print 'Gyldig', data[(data.day == 10) & (data.month == 6) ] # print 'Ugyldug', data[(data.day == 31) & (data.month == 6) ] array = np.array(data) # print array symbols = np.unique(array[:, 3]) symbols = np.array(symbols, dtype="S").tolist() dt_start = dt.datetime(array[0, 0], array[0, 1], array[0, 2]) dt_end = dt.datetime(array[-1, 0], array[-1, 1], array[-1, 2], 16) print dt_start, dt_end "Get stock prices" timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16)) # print 'Ti', timestamps[-1] dataobj = da.DataAccess("Yahoo") data = dataobj.get_data(timestamps, symbols, key) price = data.values p = pd.DataFrame(price, timestamps) # print 'price', price dict = {} for i in range(0, len(array)): orderRow = array[i] date = dt.datetime(orderRow[0], orderRow[1], orderRow[2], 16) datacolumn = symbols.index(orderRow[3]) cell = p.ix[date][datacolumn] # cell = dict[date][datacolumn] quantity = orderRow[5] if orderRow[4] == "Buy": cash = cash - cell * quantity # stocks[i][datacolumn] = stockcell + quantity elif orderRow[4] == "Sell": cash = cash + cell * quantity # stocks[i][datacolumn] = stockcell - quantity dict[date] = cash # print 'cash', orderRow[4], cash, cell, quantity # print dict cash_array = np.zeros(len(timestamps)) for i in range(0, len(timestamps)): k = timestamps[i] # print k, k in dict if k in dict: cash_array[i] = dict[k] else: cash_array[i] = cash_array[i - 1] # print cash_array[i] # print 'Cash array', cash_array stocks = np.zeros((len(timestamps), len(symbols))) for i in range(0, len(timestamps)): ts = timestamps[i] for j in range(0, len(symbols)): o = old[(old.day == ts.day) & (old.month == ts.month) & (old.year == ts.year) & (old.symbol == symbols[j])] if i > 0: stocks[i][j] = stocks[i - 1][j] val = stocks[i][j] if len(o) > 0: gf = o.values.tolist() for k in range(0, len(gf)): row = gf[k] if row[4] == "Buy": val = val + row[5] else: val = val - row[5] stocks[i][j] = val # print 'stocks', stocks[:5] # print 'prices', price[:5] value = np.zeros((len(timestamps), 3)) # value[:,2]=cash for i in range(0, len(timestamps)): rowprice = 0.0 for j in range(0, len(symbols)): st = stocks[i][j] pr = price[i][j] mul = st * pr if not np.isnan(mul): rowprice = rowprice + mul value[i][0] = rowprice value[i][1] = cash_array[i] value[i][2] = rowprice + cash_array[i] # print 'value dr', value # print 'Vvalue', value[:,2] # print 'stocks', stocks[5:], len(stocks) # print 'Data', data[:5] na_price = value[:, 0] # print 'na', len(na_price), na_price[0] # print 'type', type(na_price) normalized_price = price / price[0, :] # print 'normal', normalized_price[5:], len(normalized_price) # invest = normalized_price.sum() # stocks = [1.0, 1.0, 1.0, 0.0] # print 'stocks', stocks[:5] c = normalized_price * stocks # print 'n*a', allocation, c # print "c", c[:10] # print 'normal', normalized_price[:20] invest = c.sum(axis=1) # print 'c', c[-5:] # print 'invest', invest[:20] # daily_returns = tsu.returnize0(invest) daily_returns = tsu.daily(invest) mean_daily_return = np.mean(daily_returns) stdev_daily_return = np.std(daily_returns) sharpe_ratio = tsu.get_sharpe_ratio(daily_returns) # print 'returns', mean_daily_return, stdev_daily_return, sharpe_ratio, invest[-1] times = np.zeros((len(timestamps), 4)) value = value.astype(np.int64) for i in range(0, len(timestamps)): times[i][0] = timestamps[i].year times[i][1] = timestamps[i].month times[i][2] = timestamps[i].day times[i][3] = value[:, 2][i] # print 'Times', timestamps[i].year, timestamps[i].month, timestamps[i].day, value[:,2][i] np.savetxt(values, times, fmt="%d", delimiter=";")
def run(cash, orders, values, key): #print 'Thomas' data = pd.read_csv(orders, parse_dates=True, names=['year','month','day','symbol','transaction','nr_shares','empty'], header=0) data = data.sort(['year', 'month', 'day']); old = data #print 'Gyldig', data[(data.day == 10) & (data.month == 6) ] #print 'Ugyldug', data[(data.day == 31) & (data.month == 6) ] array = np.array(data) #print array symbols = np.unique(array[:,3]) symbols = np.array(symbols, dtype='S').tolist() dt_start = dt.datetime(array[0,0], array[0,1], array[0,2]) dt_end = dt.datetime(array[-1,0], array[-1,1], array[-1,2], 16) print dt_start, dt_end 'Get stock prices' timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16)) #print 'Ti', timestamps[-1] dataobj = da.DataAccess('Yahoo') data = dataobj.get_data(timestamps, symbols, key) price = data.values p = pd.DataFrame(price, timestamps) #print 'price', price dict = {} for i in range(0, len(array)): orderRow = array[i] date = dt.datetime(orderRow[0], orderRow[1], orderRow[2], 16) datacolumn = symbols.index(orderRow[3]) cell = p.ix[date][datacolumn] #cell = dict[date][datacolumn] quantity = orderRow[5] if (orderRow[4] == 'Buy'): cash = cash - cell * quantity #stocks[i][datacolumn] = stockcell + quantity elif (orderRow[4] == 'Sell'): cash = cash + cell * quantity #stocks[i][datacolumn] = stockcell - quantity dict[date] = cash # print 'cash', orderRow[4], cash, cell, quantity #print dict cash_array = np.zeros(len(timestamps)) for i in range(0, len(timestamps)): k = timestamps[i] #print k, k in dict if (k in dict): cash_array[i] = dict[k] else: cash_array[i] = cash_array[i-1] #print cash_array[i] #print cash_array stocks = np.zeros((len(timestamps), len(symbols))) for i in range(0, len(timestamps)): ts = timestamps[i] for j in range(0, len(symbols)): o = old[(old.day == ts.day) & (old.month == ts.month) & (old.year == ts.year) & (old.symbol == symbols[j])] if i > 0: stocks[i][j] = stocks[i-1][j] val = stocks[i][j] if len(o) > 0: gf = o.values.tolist() for k in range(0, len(gf)): row = gf[k] if row[4] == 'Buy': val = val + row[5] else: val = val - row[5] stocks[i][j] = val #print 'stocks', stocks value = np.zeros((len(timestamps), 3)) for i in range(0,len(timestamps)): rowprice = 0 for j in range(0, len(symbols)): rowprice = rowprice + stocks[i][j] * price[i][j] value[i][0] = rowprice value[i][1] = cash_array[i] value[i][2] = rowprice + cash_array[i] #print 'value', value[:,2] #print 'stocks', stocks[5:], len(stocks) #print 'Data', data[:5] na_price = value[:,0] #print 'na', len(na_price), na_price[0] #print 'type', type(na_price) normalized_price = price / price[0, :] #print 'normal', normalized_price[5:], len(normalized_price) #invest = normalized_price.sum() #stocks = [1.0, 1.0, 1.0, 0.0] #print 'stocks', stocks[:5] c = normalized_price * stocks #print 'n*a', allocation, c #print "c", c[:10] #print 'normal', normalized_price[:20] #invest = c.sum(axis=1) invest = c.sum(axis=1) print 'c', c#[-5:] print 'invest', invest[:20] #daily_returns = tsu.returnize0(invest) daily_returns = tsu.daily(invest) mean_daily_return = np.mean(daily_returns) stdev_daily_return = np.std(daily_returns) sharpe_ratio = tsu.get_sharpe_ratio(daily_returns) print 'returns', mean_daily_return, stdev_daily_return, sharpe_ratio, invest[-1] times = np.zeros((len(timestamps),4)) for i in range(0, len(timestamps)): times[i][0] = timestamps[i].year times[i][1] = timestamps[i].month times[i][2] = timestamps[i].day times[i][3] = value[:,2][i] np.savetxt(values, times, fmt='%d', delimiter=';')
def plot_portfolio(fund, benchmark): #Read the value from the funds ldt_timestamps = [] ls_values = [] fund_csv = csv.reader(open(fund, 'rU'), delimiter=',') for row in fund_csv: ls_values.append(float(row[3])) ldt_timestamps.append(dt.datetime(int(row[0]), int(row[1]), int(row[2]), 16)) ts_fund = pd.TimeSeries(ls_values, index = ldt_timestamps) #Retrieve Data for Benchmark dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(ldt_timestamps[0], ldt_timestamps[-1], dt_timeofday) c_dataobj = da.DataAccess('Yahoo', cachestalltime=0) ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] bench_data = c_dataobj.get_data(ldt_timestamps, [benchmark], ls_keys) d_data = dict(zip(ls_keys, bench_data)) benchmark_price = d_data['close'].values mul = ts_fund[0] / benchmark_price[0] benchmark_price = mul * benchmark_price #Evaluate portfolio performance #Total risk of the portfolio daily_ret = tsu.daily(ts_fund.values) vol = daily_ret.std() #Average Daily Returns of the Portfolio avg_daily_ret = daily_ret.mean() #Calcluate sharpe ratio of the Portfolio (k = 252) sharpe_ratio = (avg_daily_ret/(vol)) * math.sqrt(252) #Calculate the Cumulative Return of the portfolio cum_ret = ts_fund[-1]/ts_fund[0] #Evaluate portfolio performance #Total risk of the benchmark daily_ret_bench = tsu.daily(benchmark_price) vol_bench = daily_ret_bench.std() #Average Daily Returns of the Benchmark avg_daily_ret_bench = daily_ret_bench.mean() #Calcluate sharpe ratio of the benchmark (k = 252) sharpe_ratio_bench = (avg_daily_ret_bench/(vol_bench)) * math.sqrt(252) #Calculate the Cumulative Return of the portfolio cum_ret_bench = benchmark_price[-1]/benchmark_price[0] #Plot the performance of the fund and the benchmark plt.clf() plt.plot(ldt_timestamps, ts_fund.values, ldt_timestamps, benchmark_price) plt.legend(['Portfolio', benchmark]) plt.ylabel('Value') plt.xlabel('Date') plt.savefig('fund_performance.pdf', format='pdf') plt.xticks(size='xx-small') plt.yticks(size='xx-small') print '\nPortfolio Performance' print '---------------------------------------' print ldt_timestamps[0].strftime('Start Date: %B %d, %Y') print ldt_timestamps[-1].strftime('End Date: %B %d, %Y') print print 'Sharpe Ratio of the Fund: ' + str(sharpe_ratio) print 'Sharpe Ratio of ' + benchmark + ': ' + str(sharpe_ratio_bench) + '\n' print 'Total Return of the Fund: ' + str(cum_ret) print 'Total Return of ' + benchmark + ': ' + str(cum_ret_bench[0]) + '\n' print 'Standard Deviation of the Fund: ' + str(vol) print 'Standard Deviate of ' + benchmark + ': ' + str(vol_bench) + '\n' print 'Average Daily Return of the Fund: ' + str(avg_daily_ret) print 'Average Daily Return of ' + benchmark + ': ' + str(avg_daily_ret_bench) + '\n'
#ts_benchmark_values = _read_in_benchmark(input_benchmark, list(ts_fundvalues.index)) #read in the benchmark closing prices for the same timestamps as ts_fundvalues ts_benchmark_values = _read_bench(input_benchmark, list(ts_fundvalues.index)) ## ##dataobj = da.DataAccess('Yahoo') #data from yahoo ##close = dataobj.get_data(list(ts_fundvalues.index), ['SPY'], ['Close']) #pass a list of symbols to da.get_data ##close = close.fillna(method='ffill') ##close = close.fillna(method='bfill') ##ts_benchmark_values = pd.TimeSeries( dict(zip(list(ts_fundvalues.index), close['SPY']))) #### # calculate charpe ratios daily_fund_rets = tsu.daily(ts_fundvalues) daily_benchmark_rets = tsu.daily(ts_benchmark_values) sharpe_fund = tsu.get_sharpe_ratio(daily_fund_rets)[0] #note the tsu.get_sharpe_raio returns an ARRAY of ONE value (sharpe ratio) sharpe_benchmark = tsu.get_sharpe_ratio(daily_benchmark_rets)[0] std_fund = np.std(daily_fund_rets, axis = 0)[0] #numpy.std returns an ARRAY, so take the 0th elt in the array std_benchmark = np.std(daily_benchmark_rets, axis = 0)[0] avg_dailyret_fund = np.mean(daily_fund_rets, axis = 0)[0] avg_dailyret_benchmark = np.mean(daily_benchmark_rets, axis = 0)[0] print 'The final value of the portfolio using the sample file is: ' , ts_fundvalues.index[-1], ': $' , ts_fundvalues[-1] print "Details of the performance of the portfolio: " print "Data range: ", start_date, " to " , end_date print "Sharpe Ratio of fund: ", sharpe_fund print "Sharpe Ratio of Benchmark: ", input_benchmark, ": ", sharpe_benchmark print "Standard Deviation of Fund : ", std_fund print "Standard Deviation of $SPX : ", std_benchmark
def do_analysis(values, benchmark_sym): # first, needs figure out begin and end date. # then needs to take out benchmark values. # finnaly plot together. first_value = values[0] last_value = values[-1] dt_start = dt.datetime(first_value[0], first_value[1], first_value[2]) dt_end = dt.datetime(last_value[0], last_value[1], last_value[2]) dt_end += dt.timedelta(days=1) symbols = [benchmark_sym] dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday); dataobj = da.DataAccess('Yahoo') ls_keys = ['close']#['open', 'high', 'low', 'close', 'volume', 'actual_close'] ldf_data = dataobj.get_data(ldt_timestamps, symbols, ls_keys, verbose=True) d_data = dict(zip(ls_keys, ldf_data)) # print d_data for s_key in ls_keys: d_data[s_key] = d_data[s_key].fillna(method='ffill') d_data[s_key] = d_data[s_key].fillna(method='bfill') # d_data[s_key] = d_data[s_key].fillna(1.0) benchmark = d_data['close'][benchmark_sym]; # print normalized_benchmark[0]; factory = values[0][3] / benchmark[0]; # normalized to same factory to fund value. normalized_benchmark = benchmark * factory normalized_values = map(lambda x: x[3], values) plt.clf() plt.plot(ldt_timestamps, normalized_benchmark, "r", ldt_timestamps, normalized_values) plt.legend([ benchmark_sym, 'fund value']) plt.ylabel('Fund Value') plt.xlabel('Date') plt.savefig('analysis.pdf', format='pdf') daily_ret_my = daily_ret(normalized_values) daily_ret_bm = tsu.daily(d_data['close'][benchmark_sym]) sharp_ratio_my = tsu.get_sharpe_ratio(daily_ret_my) sharp_ratio_bm = tsu.get_sharpe_ratio(daily_ret_bm)[0] # why -1 and +1 ? total_ret_my = (float(normalized_values[-1]) / float(normalized_values[0])) total_ret_bm = float(benchmark[-1] / float(benchmark[0])) stddev_my = np.std(daily_ret_my) stddev_bm = np.std(daily_ret_bm) avg_my = np.average(daily_ret_my) avg_bm = np.average(daily_ret_bm) print "Details of the Performance of the portfolio :" print "" print "Data Range : %s to %s" % (ldt_timestamps[0], ldt_timestamps[-1]) print "" print "Sharpe Ratio of Fund : %f" % sharp_ratio_my print "Sharpe Ratio of $SPX : %fy" % sharp_ratio_bm print "" print "Total Return of Fund : %f" % total_ret_my print "Total Return of $SPX : %f" % total_ret_bm print "" print "Standard Deviation of Fund : %f" % stddev_my print "Standard Deviation of $SPX : %f" % stddev_bm print "" print "Average Daily Return of Fund : %f" % avg_my print "Average Daily Return of %s : %f" % (benchmark_sym, avg_bm)