def simulate(na_rets, lf_alloc): ''' Simulate Function''' # Estimate portfolio returns na_portrets = np.sum(na_rets * lf_alloc, axis=1) cum_ret = na_portrets[-1] tsu.returnize0(na_portrets) # Statistics to calculate stddev = np.std(na_portrets) daily_ret = np.mean(na_portrets) sharpe = (np.sqrt(252) * daily_ret) / stddev # Return all the variables return stddev, daily_ret, sharpe, cum_ret
def ks_statistic(ts_fund): fund_ts = deepcopy(ts_fund) if len(fund_ts.values) > 60: seq1 = fund_ts.values[0:-60] seq2 = fund_ts.values[-60:] tsu.returnize0(seq1) tsu.returnize0(seq2) (ks, p) = scst.ks_2samp(seq1, seq2) return ks, p # elif len(fund_ts.values) > 5: # seq1 = fund_ts.values[0:-5] # seq2 = fund_ts.values[-5:] # (ks, p) = scst.ks_2samp(seq1, seq2) # return ks, p ks = -1 p = -1 return ks, p
def calDailyReturn(self): # Normalizing the prices to start at 1 and see relative returns na_normalized_price = self.na_price / self.na_price[0, :] # Copy the normalized prices to a new ndarry to find returns. na_rets = na_normalized_price.copy() # Calculate the daily returns of the prices. (Inplace calculation) # returnize0 works on ndarray and not dataframes. tsu.returnize0(na_rets) log = input( '<<< 1. Dump data in to file .. \n<<< 2. Print on console\n<<< 0. Press 0 to ignore: ' ) #open log file if log == 1: writer = csv.writer(open('daliy_returns.csv', 'wb'), delimiter=',') writer.writerow(self.ls_symbols) row = list() writer = csv.writer(open('daliy_returns.csv', 'ab'), delimiter='\n') for i in range(len(self.ldt_timestamps)): row.append(str(self.ldt_timestamps[i]) + str(na_rets[i] * 100)) writer.writerow(row) #log_file=open("daily_returns.txt","w") #log_file.write("Daily Return,\n") #for i in range(len(self.ls_symbols)): #log_file.write(str(self.ls_symbols[i])) #log_file.write("\n") #log_file.write(str(na_rets * 100)) #log_file.close() #write column headings print '\n' if log == 2: print ' Date ', '\t\t', for i in range(len(self.ls_symbols)): print '\t\t', self.ls_symbols[i], for i in range(len(self.ldt_timestamps)): print self.ldt_timestamps[i], '\t', na_rets[i] * 100, '\n' if log == 0: return na_rets # Plotting the prices with x-axis=timestamps plt.clf() plt.plot(self.ldt_timestamps, na_rets) plt.legend(self.ls_symbols) plt.ylabel('Daily Return') plt.xlabel('Date') plt.grid(axis='both') plt.show() return na_rets
def portfolioAnalyz(): ''' Main Function This method will analyze your performance of portfolio from date specified. Create Excel sheet with your portfolio allocation and it will be read by this method to perform analysis ''' # Reading the portfolio print '<<<(s) Update your portfolioanalyze.csv file..' if os.path.isfile("C:\MRT3.0\portfolioanalyze.csv"): na_portfolio = np.loadtxt('portfolioanalyze.csv', dtype='S5,f4', delimiter=',', comments="#", skiprows=1) else: print '<<<(w) File Not Found: portfolioanalyze.csv ' sleep(2) basic.go_back() # Sorting the portfolio by symbol name na_portfolio = sorted(na_portfolio, key=lambda x: x[0]) print na_portfolio # Create two list for symbol names and allocation ls_port_syms = [] lf_port_alloc = [] for port in na_portfolio: ls_port_syms.append(port[0]) lf_port_alloc.append(port[1]) # Creating an object of the dataaccess class with Yahoo as the source. c_dataobj = da.DataAccess('Yahoo') ls_all_syms = c_dataobj.get_all_symbols() # Bad symbols are symbols present in portfolio but not in all syms ls_bad_syms = list(set(ls_port_syms) - set(ls_all_syms)) if len(ls_bad_syms) != 0: print "<<<(w) Portfolio contains bad symbols : ", ls_bad_syms for s_sym in ls_bad_syms: i_index = ls_port_syms.index(s_sym) ls_port_syms.pop(i_index) lf_port_alloc.pop(i_index) # Reading the historical data. print '<<<(i) Reading Historical data...' #dt_end = dt.datetime(2011, 1, 1) #dt_start = dt_end - dt.timedelta(days=95) # Three years # We need closing prices so the timestamp should be hours=16. dt_timeofday = dt.timedelta(hours=16) start_month, start_day, start_year, end_month, end_day, end_year = modDatesReturn.get_dates( ) dt_start = dt.datetime(start_year, start_month, start_day) dt_end = dt.datetime(end_year, end_month, end_day) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) # Keys to be read from the data, it is good to read everything in one go. ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] # Reading the data, now d_data is a dictionary with the keys above. # Timestamps and symbols are the ones that were specified before. ldf_data = c_dataobj.get_data(ldt_timestamps, ls_port_syms, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) # Copying close price into separate dataframe to find rets df_rets = d_data['close'].copy() # Filling the data. df_rets = df_rets.fillna(method='ffill') df_rets = df_rets.fillna(method='bfill') # Numpy matrix of filled data values na_rets = df_rets.values # returnize0 works on ndarray and not dataframes. tsu.returnize0(na_rets) # Estimate portfolio returns na_portrets = np.sum(na_rets * lf_port_alloc, axis=1) na_port_total = np.cumprod(na_portrets + 1) na_component_total = np.cumprod(na_rets + 1, axis=0) # Plotting the results plt.clf() #fig = plt.figure() #fig.add_subplot(111) plt.plot(ldt_timestamps, na_component_total) plt.plot(ldt_timestamps, na_port_total) ls_names = ls_port_syms ls_names.append('Portfolio') plt.legend(ls_names) plt.ylabel('Cumulative Returns') plt.xlabel('Date') #fig.autofmt_xdate(rotation=45) plt.show()
def portfolioAnalyz(): ''' Main Function This method will analyze your performance of portfolio from date specified. Create Excel sheet with your portfolio allocation and it will be read by this method to perform analysis ''' # Reading the portfolio print '<<<(s) Update your portfolioanalyze.csv file..' if os.path.isfile("C:\MRT3.0\portfolioanalyze.csv"): na_portfolio = np.loadtxt('portfolioanalyze.csv', dtype='S5,f4', delimiter=',', comments="#", skiprows=1) else: print '<<<(w) File Not Found: portfolioanalyze.csv ' sleep(2) basic.go_back() # Sorting the portfolio by symbol name na_portfolio = sorted(na_portfolio, key=lambda x: x[0]) print na_portfolio # Create two list for symbol names and allocation ls_port_syms = [] lf_port_alloc = [] for port in na_portfolio: ls_port_syms.append(port[0]) lf_port_alloc.append(port[1]) # Creating an object of the dataaccess class with Yahoo as the source. c_dataobj = da.DataAccess('Yahoo') ls_all_syms = c_dataobj.get_all_symbols() # Bad symbols are symbols present in portfolio but not in all syms ls_bad_syms = list(set(ls_port_syms) - set(ls_all_syms)) if len(ls_bad_syms) != 0: print "<<<(w) Portfolio contains bad symbols : ", ls_bad_syms for s_sym in ls_bad_syms: i_index = ls_port_syms.index(s_sym) ls_port_syms.pop(i_index) lf_port_alloc.pop(i_index) # Reading the historical data. print '<<<(i) Reading Historical data...' #dt_end = dt.datetime(2011, 1, 1) #dt_start = dt_end - dt.timedelta(days=95) # Three years # We need closing prices so the timestamp should be hours=16. dt_timeofday = dt.timedelta(hours=16) start_month,start_day,start_year,end_month,end_day,end_year = modDatesReturn.get_dates() dt_start = dt.datetime(start_year, start_month, start_day) dt_end = dt.datetime(end_year, end_month, end_day) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) # Keys to be read from the data, it is good to read everything in one go. ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] # Reading the data, now d_data is a dictionary with the keys above. # Timestamps and symbols are the ones that were specified before. ldf_data = c_dataobj.get_data(ldt_timestamps, ls_port_syms, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) # Copying close price into separate dataframe to find rets df_rets = d_data['close'].copy() # Filling the data. df_rets = df_rets.fillna(method='ffill') df_rets = df_rets.fillna(method='bfill') # Numpy matrix of filled data values na_rets = df_rets.values # returnize0 works on ndarray and not dataframes. tsu.returnize0(na_rets) # Estimate portfolio returns na_portrets = np.sum(na_rets * lf_port_alloc, axis=1) na_port_total = np.cumprod(na_portrets + 1) na_component_total = np.cumprod(na_rets + 1, axis=0) # Plotting the results plt.clf() #fig = plt.figure() #fig.add_subplot(111) plt.plot(ldt_timestamps, na_component_total) plt.plot(ldt_timestamps, na_port_total) ls_names = ls_port_syms ls_names.append('Portfolio') plt.legend(ls_names) plt.ylabel('Cumulative Returns') plt.xlabel('Date') #fig.autofmt_xdate(rotation=45) plt.show()
def eventprofiler(df_events_arg, d_data, i_lookback=20, i_lookforward=20, s_filename='study', b_market_neutral=True, b_errorbars=True, s_market_sym='SPY'): ''' Event Profiler for an event matix''' df_close = d_data['close'].copy() df_rets = df_close.copy() # Do not modify the original event dataframe. df_events = df_events_arg.copy() tsu.returnize0(df_rets.values) if b_market_neutral == True: df_rets = df_rets - df_rets[s_market_sym] del df_rets[s_market_sym] del df_events[s_market_sym] df_close = df_close.reindex(columns=df_events.columns) # Removing the starting and the end events df_events.values[0:i_lookback, :] = np.NaN df_events.values[-i_lookforward:, :] = np.NaN # Number of events i_no_events = int(np.logical_not(np.isnan(df_events.values)).sum()) if i_no_events == 0: return 0 #assert i_no_events > 0, "Zero events in the event matrix" na_event_rets = "False" # Looking for the events and pushing them to a matrix for i, s_sym in enumerate(df_events.columns): for j, dt_date in enumerate(df_events.index): if df_events[s_sym][dt_date] == 1: na_ret = df_rets[s_sym][j - i_lookback:j + 1 + i_lookforward] if type(na_event_rets) == type(""): na_event_rets = na_ret else: na_event_rets = np.vstack((na_event_rets, na_ret)) if len(na_event_rets.shape) == 1: na_event_rets = np.expand_dims(na_event_rets, axis=0) # Computing daily rets and retuns na_event_rets = np.cumprod(na_event_rets + 1, axis=1) na_event_rets = (na_event_rets.T / na_event_rets[:, i_lookback]).T # Study Params na_mean = np.mean(na_event_rets, axis=0) na_std = np.std(na_event_rets, axis=0) li_time = range(-i_lookback, i_lookforward + 1) # Plotting the chart plt.clf() plt.axhline(y=1.0, xmin=-i_lookback, xmax=i_lookforward, color='k') if b_errorbars == True: plt.errorbar(li_time[i_lookback:], na_mean[i_lookback:], yerr=na_std[i_lookback:], ecolor='#AAAAFF', alpha=0.1) plt.plot(li_time, na_mean, linewidth=3, label='mean', color='b') plt.xlim(-i_lookback - 1, i_lookforward + 1) if b_market_neutral == True: plt.title('Market Relative mean return of ' +\ str(i_no_events) + ' events') else: plt.title('Mean return of ' + str(i_no_events) + ' events') plt.xlabel('Days') plt.ylabel('Cumulative Returns') plt.savefig(s_filename, format='pdf')
def calDailyReturn(self): # Normalizing the prices to start at 1 and see relative returns na_normalized_price = self.na_price / self.na_price[0, :] # Copy the normalized prices to a new ndarry to find returns. na_rets = na_normalized_price.copy() # Calculate the daily returns of the prices. (Inplace calculation) # returnize0 works on ndarray and not dataframes. tsu.returnize0(na_rets) log = input('<<< 1. Dump data in to file .. \n<<< 2. Print on console\n<<< 0. Press 0 to ignore: ') #open log file if log == 1: writer = csv.writer(open('daliy_returns.csv','wb'),delimiter=',') writer.writerow(self.ls_symbols) row = list() writer = csv.writer(open('daliy_returns.csv','ab'),delimiter='\n') for i in range(len(self.ldt_timestamps)): row.append(str(self.ldt_timestamps[i]) + str(na_rets[i] * 100)) writer.writerow(row) #log_file=open("daily_returns.txt","w") #log_file.write("Daily Return,\n") #for i in range(len(self.ls_symbols)): #log_file.write(str(self.ls_symbols[i])) #log_file.write("\n") #log_file.write(str(na_rets * 100)) #log_file.close() #write column headings print '\n' if log == 2: print ' Date ', '\t\t' , for i in range(len(self.ls_symbols)): print '\t\t', self.ls_symbols[i], for i in range(len(self.ldt_timestamps)): print self.ldt_timestamps[i],'\t', na_rets[i] * 100,'\n' if log == 0: return na_rets # Plotting the prices with x-axis=timestamps plt.clf() plt.plot(self.ldt_timestamps, na_rets) plt.legend(self.ls_symbols) plt.ylabel('Daily Return') plt.xlabel('Date') plt.grid(axis='both') plt.show() return na_rets