def simulate(dt_start, dt_end, ls_symbols, allocations): dt_timeofday = dt.timedelta(hours=16) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) c_dataobj = da.DataAccess('Yahoo') # 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_symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) # Getting the numpy ndarray of close prices. na_price = d_data['close'].values # Normalizing the prices to start at 1 and see relative returns na_normalized_price = na_price / na_price[0, :] 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) #print na_normalized_price daily = np.zeros(len(na_normalized_price)) i = 0 for row in na_normalized_price: daily[i] = row[0]*allocations[0]+row[1]*allocations[1]+row[2]*allocations[2]+row[3]*allocations[3] i += 1 #print daily daily_returns = daily.copy() tsu.returnize0(daily_returns) #print daily_returns return (tsu.get_sharpe_ratio(daily_returns), np.std(daily_returns), np.average(daily_returns), (daily[-1]/daily[0]))
def getPortfolioStats(na_price, allocation): # Getting the numpy ndarray or pandas dataframe of close prices. #Normalize the prices according to the first day. The first row for each stock should have a value of 1.0 at this point. # Normalizing the prices to start at 1 and see relative returns if type(na_price) == type(pd.DataFrame()): na_price = na_price.values na_normalized_price = na_price / na_price[0,:] else: na_normalized_price = na_price / na_price[0] #Multiply each colunpmn by the allocation to the corresponding equity. allocatedprice = na_normalized_price * allocation #Sum each row for each day. That is your cumulative daily portfolio value. cum_daily_port_value = allocatedprice cum_ret = cum_daily_port_value[-1] #daily return daily_port_returns = cum_daily_port_value.copy() tsu.returnize0(daily_port_returns) vol = np.std(daily_port_returns) daily_ret = np.average(daily_port_returns) sharpe = tsu.get_sharpe_ratio(daily_port_returns) #tsu.sharpeRatio(cum_daily_port_value) return cum_ret, vol, daily_ret, sharpe, na_normalized_price
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 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))
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 portfolio_simulate(start_date, end_date, symbols, allocations): """ For the given portfolio and dates, calculate the std deviation of daily returns (volatility), the average daily return, the sharpe ratio, and the cumulative return """ # Generate timestamps for the NYSE closing times closing_time = dt.timedelta(hours=16) timestamps = date_util.getNYSEdays(start_date, end_date, closing_time) # Get adjusted closing prices #stock_dao = data_access.DataAccess('Yahoo', cachestalltime=0) stock_dao = data_access.DataAccess('Yahoo') stock_data_as_list_of_data_frames = stock_dao.get_data(timestamps, symbols, ['close']) portfolio_closing_values = stock_data_as_list_of_data_frames[0] # Calculate adjusted closing prices normalized relative to initial closing prices initial_portfolio_closing_values = portfolio_closing_values.values[0,:] portfolio_normalized_closing_values = portfolio_closing_values / initial_portfolio_closing_values # Calculated portfolio normalized values portfolio_normalized_weighted_closing_values = portfolio_normalized_closing_values * allocations portfolio_normalized_values = portfolio_normalized_weighted_closing_values.sum(axis=1) # Calculate the portfolio statistics cumulative_return = portfolio_normalized_values[-1] daily_returns = tsu.returnize0(portfolio_normalized_values) ave_daily_return = daily_returns.mean() std_deviation = daily_returns.std() sharpe_ratio = tsu.get_sharpe_ratio(daily_returns, 0.0)[0] return start_date, end_date, symbols, allocations, sharpe_ratio, std_deviation, ave_daily_return, cumulative_return
def simulate(values): values = values normalizedvalues = values normalizedvalues = normalizedvalues[:] / normalizedvalues[0] dailyreturns = normalizedvalues.copy() tsutil.returnize0(dailyreturns) standarddeviation = numpy.std(dailyreturns) averagedailyreturn = numpy.average(dailyreturns) sharperatio = tsutil.get_sharpe_ratio(dailyreturns) # Estimate portfolio returns> cumulativereturn = numpy.prod(dailyreturns+1) return sharperatio,standarddeviation,averagedailyreturn,cumulativereturn
def simulate(dt_start, dt_end, ls_symbols, allocation): # We need closing prices so the timestamp should be hours=16. dt_timeofday = dt.timedelta(hours=16) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) # Creating an object of the dataaccess class with Yahoo as the source. c_dataobj = da.DataAccess('Yahoo') # 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_symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) # Filling the data for NAN 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) # Getting the numpy ndarray of close prices. na_price = d_data['close'].values # Compute normalized daily returns # Normalizing the prices to start at 1 and see relative returns na_normalized_price = na_price / 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) # Compute average daily return of the total portfolio # Compute standard deviation of daily returns of the total portfolio daily_ret_portfolio = np.average(na_rets, axis=1, weights=allocation) avg_daily_ret_portfolio = np.average(daily_ret_portfolio) std_daily_ret_portfolio = np.std(daily_ret_portfolio) # Compute sharpe ratio (252 trading days in year, risk free rate = 0) sharpe_ratio_portfolio = tsu.get_sharpe_ratio(rets=daily_ret_portfolio, risk_free=0.00) # Compute return of the total portfolio yearly_ret_portfolio = np.average(na_normalized_price[-1], weights=allocation) return (sharpe_ratio_portfolio, std_daily_ret_portfolio, avg_daily_ret_portfolio, yearly_ret_portfolio)
def simulate(startdate, enddate, symbols, allocations): # We need closing prices so the timestamp should be hours=16. dt_timeofday = dt.timedelta(hours=16) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(startdate, enddate, dt_timeofday) # Creating an object of the dataaccess class with Yahoo as the source. c_dataobj = da.DataAccess('Yahoo') # Keys to be read from the data, it is good to read everything in one go. ls_keys = ['close'] # Reading the data, now d_data is a dictionary with the keys above. # Timestamps and symbols are the ones that were specified before. data = c_dataobj.get_data(ldt_timestamps, symbols, ls_keys)[0] returns = tsu.returnize1(data) previous_day = None for row in data.iterrows(): day = row[0] series = row[1] if previous_day is None: for symbol in symbols: index = symbols.index(symbol) allocation = allocations[index] series[symbol] = allocation else: daily_returns = returns.loc[day] for (symbol, price) in series.iteritems(): series[symbol] = previous_day[symbol] * daily_returns[symbol] previous_day = series daily_sum = data.apply(lambda row : row[0] + row[1] + row[2] + row[3], 1) tsu.returnize0(daily_sum) stats = daily_sum.describe() std_dr = stats['std'] mean_dr = stats['mean'] sharpe = tsu.get_sharpe_ratio(daily_sum.values) cumulative_return = 1 for value in daily_sum: value += 1 cumulative_return = cumulative_return * value return std_dr, mean_dr, sharpe, cumulative_return
def get_performance(na_price): na_normalized_price = na_price / na_price[0.0, :] # Our daily return tsu.returnize0(na_normalized_price) sharpe_ratio = tsu.get_sharpe_ratio(na_normalized_price)[0] na_cumulative_returns = np.cumprod(na_normalized_price + 1) total_returns = na_cumulative_returns[-1] std_dev = np.std(na_normalized_price) mean = np.mean(na_normalized_price) return sharpe_ratio, total_returns, std_dev, mean
def calculate_ratios(portfolio_plus_cash, column_name): total_return = portfolio_plus_cash.iloc[len (portfolio_plus_cash.index) - 1][column_name] / portfolio_plus_cash.iloc[0][column_name] returns = tsu.returnize0(portfolio_plus_cash) stats = returns.describe() sharpe_ratio = tsu.get_sharpe_ratio(returns.values)[0] standard_deviation = stats.ix['std'][column_name] average_daily_return = stats.ix['mean'][column_name] return total_return, sharpe_ratio, standard_deviation, average_daily_return
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 calculatePortfolio(startDate, endDate, symbols, allocations): stockDataDictionary = getStockData(startDate, endDate, symbols) closePrices = stockDataDictionary['close'].values tsu.returnize0(closePrices) sumDailyreturns = np.sum(closePrices * allocations, axis=1) sharpeRatio = tsu.get_sharpe_ratio(sumDailyreturns) standardDeviation = np.std(sumDailyreturns) averageDailyReturn = np.average(sumDailyreturns) totalReturn = np.prod(sumDailyreturns + 1) return sharpeRatio, standardDeviation, averageDailyReturn, totalReturn
def simulate(dt_start, dt_end, ls_symbols, allocations): """ outputs: - stadard deviation of returns - average daily returns - sharpe ratio - cumulative return of portfolio data : dict of keys (ie close, vol, open etc) where each value is a data fram """ portfolio_stdev_ret = 0 avg_daily_ret = 0 sharpe = 0 cum_ret = 0 dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) data = get_data(ldt_timestamps, ls_symbols, ["close","vol"]) d_allocations = dict(zip(ls_symbols, allocations)) # for each day,the weighted sum of the returns of all stocks # calculate the portfolio return to date price = data["close"].values normalized_price = (price/price[0,:]) normalized_prices = normalized_price.copy() #daily_returns = tsu.returnize1(na_returns) daily_returns = normalized_prices #df_cum = pd.DataFrame(cum_returns, index=ldt_timestamps, columns=ls_symbols) df_dr = pd.DataFrame(daily_returns, index=ldt_timestamps, columns=ls_symbols) df_dr.to_csv("/home/mcstar/daily_returns_cum.csv") portfolio_dr = pd.DataFrame() portfolio_cum = pd.DataFrame() for symbol in ls_symbols: portfolio_dr[symbol] = (df_dr[symbol]) * d_allocations[symbol] portfolio_dr["ret"] = portfolio_dr.sum(axis=1) ret = portfolio_dr["ret"].copy() portfolio_dr["daily_ret"] = tsu.returnize0(ret) #portfolio_dr["ret_cum"] = (1+ portfolio_dr["ret"]).cumprod() -1 portfolio_dr.to_csv("/home/mcstar/portfolio.csv") portfolio_stdev_ret = float(portfolio_dr["daily_ret"].std()) cum_ret = float(portfolio_dr["ret"].iloc[-1]) avg_daily_ret = float(portfolio_dr["daily_ret"].mean()) sharpe = float(tsu.get_sharpe_ratio(portfolio_dr["daily_ret"])) return (portfolio_stdev_ret, avg_daily_ret, sharpe, cum_ret)
def main(): if (len(sys.argv) == 3): order_book = list(read_csv(sys.argv[2])) s_date = dt.datetime(2008, 2, 25) e_date = dt.datetime(2009, 12, 31) symbols, data = setup(s_date, e_date, order_book) dates, port = trade(float(sys.argv[1]), order_book, symbols, data) port = [x for x in port if not np.isnan(x)] print port returns = tsu.returnize0(port) spx_data = [{'Sym': '$SPX'}] symbols, data = setup(s_date, e_date, spx_data) spx_returns = tsu.returnize0(data['close']['$SPX'].values) sharpe = tsu.get_sharpe_ratio(returns)[0] spx_sharpe = tsu.get_sharpe_ratio(spx_returns)[0] std_dev = np.std(returns) spx_std_dev = np.std(spx_returns) avg_daily = returns / len(returns) spx_avg_daily = spx_returns / len(spx_returns) print "Sharpe Ratio: " + str(sharpe) print "SPX Sharpe Ratio: " + str(spx_sharpe) print "\nTotal Return: " + str(float(sum(returns) + 1)) print "SPX Total Return: " + str(float(sum(spx_returns) + 1)) print "\nStandard Deviation: " + str(std_dev) print "SPX Standard Deviation: " + str(spx_std_dev) print "\nAverage Daily Return: " + str(float(sum(avg_daily))) print "SPX Average Daily Return: " + str(float(sum(spx_avg_daily))) else: print("Please specify orders file and output file.")
def simulate (startDate, endDate, stocks, stocksDistribution): stockDataDictionary=getStockDataAsDictionary(startDate,endDate,stocks) closePrices = stockDataDictionary['close'].values closePrices = closePrices normalizedPrices = closePrices.copy() normalizedPrices = normalizedPrices / normalizedPrices[0,:] dailyReturns = normalizedPrices.copy() tsutil.returnize0(dailyReturns) sumDailyreturns = numpy.sum(dailyReturns*stocksDistribution,axis=1) standardDeviation = numpy.std(sumDailyreturns) averageDailyReturn = numpy.average(sumDailyreturns) sharpeRatio = tsutil.get_sharpe_ratio(sumDailyreturns) # Estimate portfolio returns cumulativeReturn = numpy.prod(sumDailyreturns+1) return sharpeRatio,standardDeviation,averageDailyReturn,cumulativeReturn
def simulate(dt_start, dt_end, ls_syms, ls_alloc): """ # We need closing prices so the timestamp should be hours=16. dt_timeofday = dt.timedelta(hours=16) # Get a list of trading days between the start and the end. ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) # Creating an object of the dataaccess class with Yahoo as the source. c_dataobj = da.DataAccess('Yahoo') # 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_syms, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) # Filling the data for NAN 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) """ d_data = get_stock_data(dt_start, dt_end, ls_syms) # Getting the numpy ndarray of close prices. na_price = d_data['close'].values na_normalized_price = na_price / na_price[0, :] # Daily returns by allocation na_returns_alloc = np.sum(na_normalized_price * ls_alloc, axis=1) na_rets = na_returns_alloc.copy() # Our daily return tsu.returnize0(na_returns_alloc) sharpe_ratio = tsu.get_sharpe_ratio(na_returns_alloc) na_cumulative_returns = np.cumprod(na_returns_alloc + 1) std_dev = np.std(na_returns_alloc) mean = np.mean(na_returns_alloc) return na_rets, sharpe_ratio, std_dev, mean, na_cumulative_returns[-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 calculatePerformance(na_portfolio_value): #Normalize data na_norm_return = na_portfolio_value / na_portfolio_value[1] #Portfolio daily return => ret(t) = (price(t)/price(t-1)) -1 na_ret = na_norm_return.copy() tsu.returnize0(na_ret) #Portfolio stadistics ##Standard daily deviation na_std_ret = np.std(na_ret) ##Final return na_final_ret = na_norm_return[-1] ##Mean portfolio return na_mean_ret = np.mean(na_ret) ##Average daily return na_avg_ret = np.mean(na_ret) ##Sharpe Ratio na_sharpe_ratio = tsu.get_sharpe_ratio(na_ret) return (na_std_ret, na_final_ret, na_mean_ret, na_sharpe_ratio)
def simulate(startdate, enddate, symbols, allocation, plot): #Get data values (only interested in close ftm) dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) c_dataobj = da.DataAccess('Yahoo') ls_keys = ['close'] ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_data)) #Normalize data na_price = d_data['close'].values na_norm_price = na_price / na_price[0,:] #Weight normalized values with portfolio allocation na_normAllocated_price = na_norm_price.copy() * allocation #Cumulative daily portfolio value (normalized) na_cumulative_price = np.sum(na_normAllocated_price, axis=1) #Portfolio daily return => ret(t) = (price(t)/price(t-1)) -1 na_ret = na_cumulative_price.copy() tsu.returnize0(na_ret) #Portfolio stadistics ##Standard daily deviation na_std_ret = np.std(na_ret) ##Final return na_final_ret = na_cumulative_price[-1] ##Mean portfolio return na_mean_ret = np.mean(na_ret) ##Average daily return na_avg_ret = np.mean(na_ret) ##Sharpe Ratio na_sharpe_ratio = tsu.get_sharpe_ratio(na_ret) if plot: plotNormalized(ldt_timestamps, na_cumulative_price) return (na_std_ret, na_final_ret, na_mean_ret, na_sharpe_ratio)
def simulate(startdate, enddate, symbols, allocations, use_cache=0): """ A Python function that can simulate and assess the performance of a 4 stock portfolio. Inputs to the function include: Start date End date Symbols for for equities (e.g., GOOG, AAPL, GLD, XOM) Allocations to the equities at the beginning of the simulation (e.g., 0.2, 0.3, 0.4, 0.1) """ # Get actual end timestamps for trading days on NYSE trading_duration = dt.timedelta(hours=16) trading_timestamps = du.getNYSEdays(startdate, enddate, trading_duration) # Get data from Yahoo data_provider = da.DataAccess('Yahoo', cachestalltime=use_cache) data_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] data_list = data_provider.get_data(trading_timestamps, symbols, data_keys) data = dict(zip(data_keys, data_list)) # Get 'close' prices and normalized then close_prices = data['close'].values normalized_close = close_prices / close_prices[0, :] # Compute portfolio by multiplying weights portfolio_prices = (allocations * normalized_close).sum(axis=1) # Compute daily returns for portfolio portfolio_rets = portfolio_prices.copy() tsu.returnize0(portfolio_rets) # Final statistics volatility = portfolio_rets.std() avg_return = portfolio_rets.mean() sharpe = tsu.get_sharpe_ratio(portfolio_rets) cum_return = np.prod(1 + portfolio_rets) return (volatility, avg_return, sharpe, cum_return)
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=';')
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 compute_sharpe_ratio(values): return tsu.get_sharpe_ratio(values)
## ##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 print "Average Daily Return of Fund : ", avg_dailyret_fund print "Average Daily Return of $SPX : ", avg_dailyret_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)
import matplotlib.pyplot as plt from pylab import * import pandas print pandas.__version__ # # Prepare to read the data startday = dt.datetime(2011, 1, 1) endday = dt.datetime(2011, 12, 31) timeofday=dt.timedelta(hours=16) timestamps = du.getNYSEdays(startday, endday, timeofday) # #Read close data from symbols list dataobj = da.DataAccess('Yahoo') symbols = dataobj.get_all_symbols() close = dataobj.get_data(timestamps, symbols, "close", verbose=True) # #Calculate daily_return trading_date = close.index daily_price = close.values.copy() daily_rets = close.values.copy() tsu.fillforward(daily_rets) tsu.fillbackward(daily_rets) tsu.returnize0(daily_rets) sharpe_list = sorted(zip(tsu.get_sharpe_ratio(daily_rets), daily_price[-1, :] / daily_price[0, :] - 1, symbols), reverse=True) #Filter NaN value sharpe_list = [f for f in sharpe_list if not math.isnan(f[0]) and not math.isnan(f[1])] print sharpe_list
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 main(): # Read in starting cash balance, file names, and orders [values_file, comp_symbol] = sys.argv[1:3] # loading text from: http://wiki.quantsoftware.org/index.php?title=QSTK_Tutorial_2 values = pd.read_csv(values_file, index_col=0) # from Homework 2 # TODO: Correct bad naming conventions for values. # TODO: make into function for comparision with S&P # for fund na_price = values['value'].values na_normalized_price = na_price / na_price[0] na_rets = na_normalized_price.copy() tsu.returnize0(na_rets) daily_ret_fund = na_rets avg_daily_ret_fund = np.average(daily_ret_fund) std_daily_ret_fund = np.std(daily_ret_fund) sharpe_ratio_fund = tsu.get_sharpe_ratio(rets=daily_ret_fund, risk_free=0.00) total_ret_fund = na_normalized_price[-1] # for comparision symbol # Fetch stock data and fill in NANs. c_dataobj = da.DataAccess('Yahoo') # Start and end dates # TODO: Get from values.csv dt_start = dt.datetime(2011, 1, 1, 16) dt_end = dt.datetime(2011, 12, 31, 16) dt_timeofday = dt.timedelta(hours=16) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt_timeofday) ls_symbols = [comp_symbol] # ls_keys = ['open', 'high', 'low', 'close', 'volume', 'actual_close'] ls_keys = ['actual_close'] ldf_data = c_dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys) d_data = dict(zip(ls_keys, ldf_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) # TODO: Flatten na_price list of lists. na_price = d_data['actual_close'].values na_normalized_price = na_price / na_price[0] na_rets = na_normalized_price.copy() tsu.returnize0(na_rets) daily_ret_comp = na_rets avg_daily_ret_comp = np.average(daily_ret_comp) std_daily_ret_comp = np.std(daily_ret_comp) sharpe_ratio_comp = tsu.get_sharpe_ratio(rets=daily_ret_comp, risk_free=0.00) total_ret_comp = na_normalized_price[-1] print "sharpe_ratio_fund = ", sharpe_ratio_fund print "sharpe_ratio_comp = ", sharpe_ratio_comp print "total_ret_fund = ", total_ret_fund print "total_ret_comp = ", total_ret_comp print "std_daily_ret_fund = ", std_daily_ret_fund print "std_daily_ret_comp = ", std_daily_ret_comp print "avg_daily_ret_fund = ", avg_daily_ret_fund print "avg_daily_ret_comp = ", avg_daily_ret_comp return