def load_data(): df_adj_close = load_all_data_from_file(prefix + 'etf_data_adj_close.csv', start_date, end_date) ticket_list = load_ticket_list(df_adj_close) print('cleaning data...') df_adj_close = df_adj_close[ticket_list] print('Backfill of data for NaNs...') df_adj_close = df_adj_close.fillna(method='bfill') print('Cleaning anomalies from data...') for ticket in ticket_list: pct = df_adj_close[ticket].pct_change() for i in range(len(df_adj_close[ticket])): if i > 0 and i < len(df_adj_close[ticket]) - 1: bound_lower = pct.iloc[i] bound_upper = pct.iloc[i + 1] # print('%d|%f|%f'%(i,bound_lower,bound_upper)) if np.abs(bound_lower) > 0.1 and np.abs(bound_upper) > 0.1: # print('changing price from %f to %f' %(df_adj_close[ticket].iloc[i], df_adj_close[ticket].iloc[i+1])) df_adj_close[ticket].iloc[i] = df_adj_close[ticket].iloc[i + 1] ticket_list = remove_anomalies_tickets(df_adj_close, ticket_list) df_adj_close = df_adj_close[ticket_list] return ticket_list, df_adj_close
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()
# 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 print('starting to load data') prefix = 'xetra_' start_date = '1993-01-01' end_date = '2017-12-31' df_adj_close = load_all_data_from_file(prefix + 'etf_data_adj_close.csv', start_date, end_date) etf = ['DBPG.DE'] data = df_adj_close[etf].values print(len(data)) ror = [] for _ in range(1): df_price = get_data_random_dates(df_adj_close) investor = compute_one_etf(etf, df_price[etf]) if investor.invested == 0: continue print('invested:' + str(investor.invested_history[-1]))
import sys sys.path.insert(0, '../../../etf_data') from etf_data_loader import load_all_data_from_file import numpy as np import matplotlib.pyplot as plt import pandas as pd dir_data = 'data_ltc_eur/' dir_models = 'models_ltc_eur/' ticket = 'LTC_EUR' start_date = '2017-01-01' end_date = '2018-06-01' df_adj_close = load_all_data_from_file('btc_data_open.csv', start_date, end_date) data = pd.DataFrame(df_adj_close['Open']) # data = data.ewm(alpha=0.1).mean() np.warnings.filterwarnings('ignore') plt.plot(data.pct_change().cumsum().as_matrix()) legends = ['benchmark'] agent = CryptoTraderAgent('btc', model='models/decision_tree_13.pkl') agent.invest(data, window=30) plt.plot(agent.ror_history) legends.append('ror decision_tree_11.pkl') chaos_counts = [