def test_04_(self) : msg = {} for stock, ret in T.extract() : daily = HELPER.findDailyReturns(ret) _risk, _returns = HELPER.findRiskAndReturn(daily, period=HELPER.YEAR) value = [_returns,_risk,_returns/_risk] value = dict(zip(['returns','risk','sharpe'],value)) msg[stock] = value msg = pd.DataFrame(msg).T logging.info(msg)
def test_05_(self) : msg = {} for stock, ret in T.extract() : value_1 = ret['Adj Close'].pct_change() + 1 _risk, _returns = HELPER.findRiskAndReturn(value_1, period=HELPER.YEAR) value_1 = [_returns,_risk,_returns/_risk] value_1 = dict(zip(['returns','risk','sharpe'],value_1)) value_2 = ret['Adj Close'].pct_change() _risk, _returns = HELPER.findRiskAndReturn(value_2, period=HELPER.YEAR) value_2 = [_returns,_risk,_returns/_risk] value_2 = dict(zip(['returns','risk','sharpe'],value_2)) msg[stock] = value_2 msg[stock + "+ 1"] = value_1 msg = pd.DataFrame(msg) logging.info(msg.T)
def _find(cls, data, stocks, num_portfolios, risk_free_rate, period): data.sort_index(inplace=True) returns = FINANCE.findDailyReturns(data) #set up array to hold results #We have increased the size of the array to hold the weight values for each stock size = len(stocks) ret = np.zeros((3 + size, num_portfolios)) #calculate mean daily return and covariance of daily returns mean = returns.mean() cov_matrix = returns.cov() for weights, i in cls._weights(size, num_portfolios): returns, risk, sharpe = cls._sharpe(cov_matrix, mean, period, risk_free_rate, weights) #store results in results array ret[0, i] = returns ret[1, i] = risk ret[2, i] = sharpe for j in range(len(weights)): ret[j + 3, i] = weights[j] #convert results array to Pandas DataFrame columns = cls.columns + stocks ret = pd.DataFrame(ret.T, columns=columns) logging.info(ret.head(3)) logging.info(ret.tail(3)) return ret
def transformReturns(cls, returns) : ret = FINANCE.findDailyReturns(returns) mean = ret.mean() cov_matrix = ret.cov() #logging.info(cov_matrix) #logging.info(ret) #logging.info(mean) return ret, mean, cov_matrix
def t01(cls, baseline, test): baseline = baseline.reindex(test.index) test = FINANCE.findDailyReturns(baseline) ret = test / baseline ret = ret.replace([np.inf, -np.inf], np.nan) ret = ret.fillna(0) ret = ret.dropna(how='all') return ret
def prototype(file_list, stock_list): name_list, _ret = load(file_list, stock_list) ret = FINANCE.graphDailyReturns(_ret) value_list = map(lambda x: _ret[x], name_list) value_list = map(lambda data: data.sort_index(inplace=True), value_list) value_list = map( lambda data: MONTECARLO.find( data, risk_free_rate=0.02, period=FINANCE.YEAR, span=0), value_list) sharpe = dict(zip(name_list, value_list)) return name_list, ret, sharpe
def find(cls, data, **kwargs): target = "period" period = kwargs.get(target, 0) target = "risk_free_rate" risk_free_rate = kwargs.get(target, 0.02) target = "span" span = kwargs.get(target, 2 * FINANCE.YEAR) data, risk_free_rate, period, span = cls.validate( data, risk_free_rate, period, span) data = FINANCE.findDailyReturns(data, period) if data is None: ret = dict(zip(cls.key_list, [0, 0, 0, 0])) return ret risk, returns = FINANCE.findRiskAndReturn(data, span) sharpe = 0 if risk != 0: sharpe = (returns - risk_free_rate) / risk ret = dict(zip(cls.key_list, [returns, risk, sharpe, len(data)])) return ret
def main(file_list, portfolio_ini, ini_list): local_enrich = enrich(*ini_list) bench_list = benchmark(*ini_list) SNP = 'SNP500' snp = bench_list[SNP] gcps = snp[0] name_list, snp_returns, snp_sharpe = prototype(file_list, snp) snp_returns.rename(columns={gcps: SNP}, inplace=True) FUNDS = 'NASDAQMUTFUND' funds = bench_list[FUNDS] fund_name_list, fund_returns, fund_sharpe = prototype(file_list, funds) portfolio_list = prep(*portfolio_ini) portfolio_list = pd.DataFrame(portfolio_list.values()) portfolio_list = portfolio_list.sort_values(['risk']) logging.info(portfolio_list) ret_detail_list = [] ret_name_return_list = [] ret_summary_list = {} ret_diversified_list = [] ret_name_diversified_list = [] ret_sharpe_list = {} for weights, sharpe, diversified, name_diversified, name_returns in find( local_enrich, portfolio_list): stock_list = sorted(weights.index) name_list, ret = load(file_list, stock_list) ret = FINANCE.findDailyReturns(ret) logging.info(weights) logging.info(stock_list) logging.info(sharpe) portfolio_return = weights.dot(ret.T).dropna(how="all") ret[name_returns] = portfolio_return ret = 1 + ret ret.iloc[0] = 1 # set first day pseudo-price ret = ret.cumprod() print ret.head(2) print ret.tail(2) print name_diversified, name_returns ret_detail_list.append(ret) ret_summary_list[name_returns] = ret[name_returns] ret_sharpe_list[name_returns] = sharpe ret_diversified_list.append(diversified) ret_name_return_list.append(name_returns) ret_name_diversified_list.append(name_diversified) ret_summary_list[SNP] = snp_returns ret_sharpe_list[SNP] = snp_sharpe[gcps] for name in fund_name_list: ret_summary_list[name] = fund_returns[name] ret_sharpe_list[name] = fund_sharpe[name] return ret_detail_list, ret_name_return_list, ret_summary_list, ret_diversified_list, ret_name_diversified_list, ret_sharpe_list
def _get(cls, df, positions, initial_capital): target = 'Adj Close' # Initialize the portfolio with value owned ret = positions.multiply(df[target], axis=0) # Store the difference in shares owned pos_diff = positions.diff() ret['holdings'] = (positions.multiply(df[target], axis=0)).sum(axis=1) ret['cash'] = initial_capital - (pos_diff.multiply( df[target], axis=0)).sum(axis=1).cumsum() ret['total'] = ret['cash'] + ret['holdings'] ret['returns'] = FINANCE.getDailyReturns(ret['total']) return ret
def smartMassage(cls,data) : data.fillna(method='backfill',inplace=True) data = data / data.iloc[0] data = data - 1 key_list = sorted(data.columns.values) ticker_list = filter(lambda ref : cls.isReserved(ref), key_list) reserved = data[ticker_list] ticker_list = filter(lambda ref : cls.isReserved(ref) == False, key_list) ret = data[ticker_list] ret = FINANCE.new_graphReturns(ret) ret.fillna(method='backfill',inplace=True) ret = pd.concat([ret,reserved],axis=1, sort=False) logging.info(ret) return ret
def by_month(cls, name, data): year_to_year = {} many_years = {} for month, year, dataSeries in cls.group_by_month(data): percent = FINANCE.findDailyReturns(dataSeries) if len(percent) == 0: continue if month not in year_to_year: year_to_year[month] = pd.DataFrame(percent) else: init = len(year_to_year[month]) year_to_year[month] = year_to_year[month].append(percent) if init == len(year_to_year[month]): raise ValueError("dataframe did not append") label = '{},{}-{}'.format(month, year, name) many_years[label] = cls.many_years(month, year, percent) return year_to_year, many_years
def by_quarter(cls, data): year_to_year = {} for month, year, dataSeries in cls.group_by_month(data): quarter = 1 if month in [4, 5, 6]: quarter = 2 elif month in [7, 8, 9]: quarter = 3 elif month in [10, 11, 12]: quarter = 4 percent = FINANCE.getDailyReturns(dataSeries) percent = percent.fillna(0) if len(percent) == 0: continue if quarter not in year_to_year: year_to_year[quarter] = pd.DataFrame(percent) else: init = len(year_to_year[quarter]) year_to_year[quarter] = year_to_year[quarter].append(percent) if init == len(year_to_year[quarter]): raise ValueError("dataframe did not append") return year_to_year
def demo_returns(data, path): data = FINANCE.graphReturns(data) logging.debug(data.describe()) data.rename(columns={'^GSPC': 'SNP500'}, inplace=True) LINE.plot(data, title="Returns") save(path)
def _getSharpe(cls, data): risk, returns = FINANCE.findRiskAndReturn(data, period=FINANCE.YEAR) ret = TRANSFORM_SHARPE.sharpe(risk, returns, cls.risk_free_rate) return ret
def summarizeReturns(cls, data): stock_list = cls.parseStockList(data) ret = map(lambda stock: FINANCE.findDailyReturns(data[stock]), stock_list) ret = dict(zip(stock_list, ret)) return ret
def test_01_(self) : for stock, ret in T.extract() : logging.info (stock) logging.info (ret.describe().round(2)) daily = HELPER.findDailyReturns(ret) logging.info (daily.describe().round(2))