api_key = "" #Dirctory Input For Data and Reports root_path = "/Users/zhaoyanbo/Desktop/RIF/nic 4:5:19/Portfolio-Tracker-master" #------------Run Program----------------------# if __name__ == '__main__': #import rebalance #rebalance.rebalance(allocations=allocations) # 1.) Import the module import report import data import performance # # # Select Functions end_date = dt.date.today() data.portfolio(symbols, allocations, start_date) data.benchmark(bench_symbol, start_date) performance.portfolio() r = report.rep(fname=root_path + '/Reports/Daily Report ' + str(end_date) + '.pdf',fund_name=fund_name,logo_path="/Users/zhaoyanbo/Desktop/RIF/nic 4:5:19/Portfolio-Tracker-master/Reports/rif.jpg") print ('--------Cover-----') r.cover() print ('--------Performance-----') r.perf() print ('--------Metrics-----') r.mets() print ('--------Divesification-----') r.diversification() r.savePDF() print ('I Ran')
def perf(self): c = self.c logo = self.logo_path #Get Data from module port_rets, bench_rets = performance.portfolio() #Header c.setFontSize(size=18) c.setFillColor(aColor=colors.maroon) c.drawString(x=72, y=505, text="Performance") c.setFillColor(colors.maroon) c.rect(x=0, y=500, height="3", width="842", stroke=0, fill=1) if self.logo_path != 0: c.drawImage(logo,x=250, y=505) #Draw price chart c.drawImage(root_path+'/Figures/port_perf.png',x=50, y=50,width=400,height=400,preserveAspectRatio=1) #Draw periodic returns table dates = ['2019-01-02'] pperf = "{0:.2f}%".format(float(port_rets.iloc[-1:, -1]) * 100) bperf = "{0:.2f}%".format(float(bench_rets.iloc[-1:, -1]) * 100) port_val = pd.read_csv(root_path+'/Daily Data/Portfolio/Portfolio Value.csv',index_col=0) bench_data = pd.read_csv(root_path+'/Daily Data/Benchmark/Benchmark Price Data.csv' ,index_col=0, names = ['Benchmark Values']) port_val = port_val['Portfolio Value'] port_val.index = pd.to_datetime(port_val.index) bench_data.index = pd.to_datetime(bench_data.index) praw = "{0:.2f}%".format((port_val.iloc[-1] / port_val.iloc[0] - 1) * 100) pytd = "{0:.2f}%".format((port_val.iloc[-1] / float(port_val[port_val.index.isin(dates)]) - 1) * 100) braw = "{0:.2f}%".format((float(bench_data.iloc[-1]) / float(bench_data.iloc[0]) - 1) * 100) bytd = "{0:.2f}%".format((float(bench_data.iloc[-1]) / float(bench_data[bench_data.index.isin(dates)].iloc[0]) - 1) * 100) prets = [] brets = [] nums = [1,3,6,12] for x in nums: month_add = start_date + relativedelta(months=+x) pret = port_val.iloc[port_val.index.get_loc(month_add, method='nearest')]/port_val.iloc[0] - 1 prets.append("{0:.2f}%".format(pret * 100)) bret = float(bench_data.iloc[bench_data.index.get_loc(month_add, method='nearest')]) / float(bench_data.iloc[0]) - 1 brets.append("{0:.2f}%".format(bret * 100)) #Labels for Asset Returns c.setFontSize(size=12) c.setFillColor(aColor=colors.black) c.drawString(x=500, y=400, text="Total Return Asset") c.drawString(x=625, y=400, text="1 Day Return Asset") #Data for table data = [['',"YTD","Today's Performance", "1 Month Change", "3 Month Change","6 Month Change", "1 Year Change", "Total Raw Return"], ['Portfolio',pytd,pperf, prets[0],prets[1],prets[2],prets[3],praw], ['Benchmark',bytd,bperf,brets[0],brets[1],brets[2],brets[3],braw]] #Create table with styles t1 = Table(data) t1.setStyle(TableStyle([("BOX", (0, 0), (-1, -1), 0.25, colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BACKGROUND', (0, 0), (0, 0), colors.black), ('ALIGN', (1, 1), (-1, -1), 'CENTER')])) #Draw on Canvas t1.wrapOn(self.c, self.width, self.height) t1.drawOn(c, x=72, y=440) #Draw asset Perfomance Tables df = performance.asset_performance() df = pd.DataFrame(df) df.reset_index(level=0, inplace=True) df.columns = ["Symbol", "Return"] # if len(df) > 21: # df = df[:21] df = df.sort_values(['Return'], ascending=False) top_10 = df.nlargest(10, 'Return') last_10 = df.nsmallest(10, 'Return') last_10 = last_10[::-1] df = pd.concat([top_10, last_10]) df['Return'] = pd.Series(["{0:.2f}%".format(val * 100) for val in df['Return']], index=df.index) data = [df.columns[:, ].values.astype(str).tolist()] + df.values.tolist() t = Table(data, rowHeights = 14) data_len = len(data) x = 390 #Need to do for alternating colors for each in range(data_len): x -= 14 if each % 2 == 0: bg_color = colors.white else: bg_color = colors.gold if each <= 10: fontcolor = colors.green else: fontcolor = colors.red t.setStyle(TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color), ('FONTSIZE', (0, each), (-1, each), 9), ('TEXTCOLOR', (0, each), (-1, each), fontcolor)])) t.setStyle(TableStyle([('BACKGROUND', (0, 0), (0, 0), colors.black), ('BACKGROUND', (1, 0), (1, 0), colors.black), ('TEXTCOLOR',(0,0),(0, 0),colors.white), ('LINEBELOW', (0, -1), (-1, -1), 1.5, colors.black), ('TEXTCOLOR', (1, 0), (1, 0), colors.white), ('FONTSIZE', (0, each), (-1, each), 9) ])) t.wrapOn(self.c, self.width, self.height) t.drawOn(c, x=500, y=x) df = pd.read_csv(root_path+'/Daily Data/Portfolio/Portfolio Returns.csv') data = df.iloc[-1,1:len(df.columns)-1] df = pd.DataFrame(data) # if len(df) > 21: # df = df[:21] df.reset_index(level=0, inplace=True) df.columns = ["Symbol", "Return"] df = df.sort_values(by='Return',ascending=False) df['Return'] = df['Return'].astype(float, copy = False) # print df['Return'] # print df['Return'].dtypes top_10 = df.nlargest(10, 'Return') last_10 = df.nsmallest(10, 'Return') last_10 = last_10[::-1] df = pd.concat([top_10, last_10]) df['Return'] = pd.Series(["{0:.2f}%".format(val * 100) for val in df['Return']], index=df.index) data = [df.columns[:, ].values.astype(str).tolist()] + df.values.tolist() t = Table(data, rowHeights = 14) data_len = len(data) x = 390 #Need to do for alternating colors for each in range(data_len): x -= 14 if each % 2 == 0: bg_color = colors.gold else: bg_color = colors.white if each <= 10: fontcolor = colors.green else: fontcolor = colors.red t.setStyle(TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color), ('FONTSIZE', (0, each), (-1, each), 9), ('TEXTCOLOR', (0, each), (-1, each), fontcolor)])) t.setStyle(TableStyle([('BACKGROUND', (0, 0), (0, 0), colors.black), ('BACKGROUND', (1, 0), (1, 0), colors.black), ('TEXTCOLOR',(0,0),(0, 0),colors.white), ('LINEBELOW', (0, -1), (-1, -1), 1.5, colors.black), ('TEXTCOLOR', (1, 0), (1, 0), colors.white) ,('FONTSIZE', (0, each), (-1, each), 9)])) t.wrapOn(self.c, self.width, self.height) t.drawOn(c, x=635, y=x) c.showPage()