def compute_bah(etf,period,cash_sum): dca = bah.DCA(period, cash_sum) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(df_adj_close[etf]) return investor
def compute_one_etf(etf): start_date, end_date = compute_dates(etf) print(start_date) df_adj_close = load_all_data_from_file('etf_data_adj_close.csv', start_date, end_date) dca = bah.DCA(30, 300.) investor = bah.Investor(etf, np.full(len(etf), 1.0 / len(etf)), dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(df_adj_close[etf]) investor.compute_means() _, ax = plt.subplots(3, 1) for rms in investor.rms_list: ax[2].plot(rms) print('invested:' + str(investor.invested_history[-1])) print('value gained:' + str(investor.history[-1])) print('ror:' + str(investor.ror_history[-1])) print('mean:' + str(investor.m)) print('std:' + str(investor.std)) for rms in investor.means: print(str(rms)) #ax[0].plot(np.log(df_adj_close[etf])) ax[0].plot(sim.investor.invested_history) ax[0].plot(sim.investor.history) # ax[0].plot(sim.investor.history) ax[1].plot(sim.investor.ror_history) ax[0].legend(['nav', 'invested', 'value']) ax[1].legend(['RoR']) plt.show()
def compute_one_etf(etf, inception, results): print(etf) result = etf[0] if inception is not None: inception_date = inception[inception.ticket == etf[0]].inc_date.values[0] data = df_adj_close[df_adj_close.Date > inception_date][etf] else: data = df_adj_close[etf] dca = bah.DCA(period, cash_to_invest) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(data) investor.compute_means() result += ';' + str(investor.invested_history[-1]) result += ';' + str(investor.history[-1]) result += ';' + str(investor.ror_history[-1]) result += ';' + str(investor.m) result += ';' + str(investor.std) for rms in investor.means: result += ';' + str(rms) result += '\n' print(result) with open(results, 'a+') as fd: fd.write(result)
def compute_bah(etf): dca = bah.DCA(30, 300.) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(df_adj_close[etf]) investor.compute_means() return investor
def run_bah_sim(etf,period,cash_sum): data = get_data_random_dates() dca = bah.DCA(period, cash_sum) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(data[etf]) return investor
def run_bah_sim(etf): data = get_data_random_dates() dca = bah.DCA(30, 300.) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(data[etf]) investor.compute_means() return investor
def compute_one_etf(etf): inception_date = inception[inception.ticket == etf[0]].inc_date.values[0] data = df_adj_close[df_adj_close.Date > inception_date][etf] dca = bah.DCA(30, 300.) investor = bah.Investor(etf, np.full(len(etf), 1.0 / len(etf)), dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(data) investor.compute_means() investor.compute_rank() return data, investor
def compute_one_etf(etf, data): dca = bah.DCA(30, 300.) investor = bah.Investor(etf, np.full(len(etf), 1.0 / len(etf)), dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(data) # _, ax = plt.subplots(3, 1) # # ax[0].plot(data) # ax[1].plot(sim.investor.invested_history) # ax[1].plot(sim.investor.history) # ax[2].plot(sim.investor.ror_history) # ax[0].legend(['nav']) # ax[1].legend(['invested', 'value']) # ax[2].legend(['Returns']) # # plt.show() return investor
def compute_one_etf(): global fd print(etf) result = etf[0] + '+' + etf[1] df_open, df_close, df_high, df_low, df_adj_close = load_data( etf, start_date, end_date) dca = bah.DCA(30, 300.) investor = bah.Investor([0.5, 0.5], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(df_adj_close, df_high, df_low) means = [] rms_list = [] for i in range(1, 11): rms = rolling_mean(np.array(sim.investor.ror_history), i * 365) m = mean(rms) if m > 0: rms_list.append(m.round(2)) means.append(m.round(2)) else: rms_list.append(0.) means = np.array(means) m = np.mean(means).round(2) if np.isnan(m): m = 0. std = np.std(means).round(2) if np.isnan(std): std = 0. result += ';' + str(sim.investor.invested_history[-1]) result += ';' + str(sim.investor.history[-1]) result += ';' + str(sim.investor.ror_history[-1]) result += ';' + str(m) result += ';' + str(std) for rms in rms_list: result += ';' + str(rms) result += '\n' with open(results, 'a+') as fd: fd.write(result)
plt.plot(df_adj_close) plt.legend(df_adj_close) plt.show() reb_inv_list = [] for pc in pc_list: print(pc) rebalance_inv = rebalancer.simulate(df_adj_close, pc=pc) reb_inv_list.append(rebalance_inv) rebalancer.writeResults('reb', etf, df_adj_close.tail(1).as_matrix()[0], rebalance_inv) dca = bah.DCA(30, 300.) investor = bah.Investor(etf, [1.0], dca) sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) sim.invest(df_adj_close, tickets=etf) print('B&H') print('zisk:', investor.history[-1]) print('ror:', investor.ror_history[-1]) interpet_results(reb_inv_list, investor, pc_list) # dca = bah.DCA(30, 300.) # investor = bah.Investor(etf[1], [1.0], dca) # sim = bah.BuyAndHoldInvestmentStrategy(investor, 2.) # sim.invest(data[etf[1]], [etf[1]]) # # print('B&H')