# In[197]: print("==============Get Backtest Results===========") perf_stats_all = backtest_stats(account_value=df_account_value, value_col_name = 'total_assets') # <a id='6.2'></a> # ## 7.2 BackTestPlot # In[198]: print("==============Compare to DJIA===========") get_ipython().run_line_magic('matplotlib', 'inline') # S&P 500: ^GSPC # Dow Jones Index: ^DJI # NASDAQ 100: ^NDX backtest_plot(df_account_value, baseline_ticker = '^DJI', baseline_start = '2019-01-01', baseline_end = '2021-01-01', value_col_name = 'total_assets') # In[ ]:
df_account_value, df_actions = DRLAgent.DRL_prediction(model=trained_a2c, environment=e_trade_gym) df_account_value.shape df_account_value.tail() ############# BACKTEST ############# print("==============Get Backtest Results===========") now = datetime.datetime.now().strftime('%Y%m%d-%Hh%M') perf_stats_all = backtest_stats(account_value=df_account_value) perf_stats_all = pd.DataFrame(perf_stats_all) #baseline stats print("==============Get Baseline Stats===========") baseline_df = get_baseline(ticker="^DJI", start='2019-01-01', end='2021-01-01') stats = backtest_stats(baseline_df, value_col_name='close') print("==============Compare to DJIA===========") # S&P 500: ^GSPC # Dow Jones Index: ^DJI # NASDAQ 100: ^NDX backtest_plot(df_account_value, baseline_ticker='^DJI', baseline_start='2019-01-01', baseline_end='2021-01-01') df_account_value.date
def main(): if not os.path.exists("./" + config.DATA_SAVE_DIR): os.makedirs("./" + config.DATA_SAVE_DIR) if not os.path.exists("./" + config.TRAINED_MODEL_DIR): os.makedirs("./" + config.TRAINED_MODEL_DIR) if not os.path.exists("./" + config.TENSORBOARD_LOG_DIR): os.makedirs("./" + config.TENSORBOARD_LOG_DIR) if not os.path.exists("./" + config.RESULTS_DIR): os.makedirs("./" + config.RESULTS_DIR) print(config.START_DATE) print(config.END_DATE) print(config.PENNY_STOCKS) df = YahooDownloader(start_date=config.START_DATE, end_date=config.END_DATE, ticker_list=config.PENNY_STOCKS).fetch_data() fe = FeatureEngineer(use_technical_indicator=True, tech_indicator_list=config.TECHNICAL_INDICATORS_LIST, use_turbulence=True, user_defined_feature=False) processed = fe.preprocess_data(df) information_cols = list(processed) information_cols.remove('date') information_cols.remove('tic') stock_dimension = len(processed.tic.unique()) state_space = 1 + 2 * stock_dimension + len( information_cols) * stock_dimension print("Stock Dimension: {}, State Space: {}".format( stock_dimension, state_space)) env_kwargs = { "hmax": 100, "initial_amount": 5000, # Since in Indonesia the minimum number of shares per trx is 100, then we scaled the initial amount by dividing it with 100 "buy_cost_pct": 0.00, # IPOT has 0.19% buy cost "sell_cost_pct": 0.00, # IPOT has 0.29% sell cost "state_space": state_space, "stock_dim": stock_dimension, "tech_indicator_list": information_cols, "action_space": stock_dimension, "reward_scaling": 1e-4, "print_verbosity": 5 } rebalance_window = 63 # rebalance_window is the number of days to retrain the model validation_window = 63 # validation_window is the number of days to do validation and trading (e.g. if validation_window=63, then both validation and trading period will be 63 days) train_start = config.START_DATE train_end = config.START_TRADE_DATE val_test_start = config.START_TRADE_DATE val_test_end = config.END_DATE ensemble_agent = DRLEnsembleAgent(df=processed, train_period=(train_start, train_end), val_test_period=(val_test_start, val_test_end), rebalance_window=rebalance_window, validation_window=validation_window, **env_kwargs) A2C_model_kwargs = { 'n_steps': 5, 'ent_coef': 0.01, 'learning_rate': 0.0005 } PPO_model_kwargs = { "ent_coef": 0.01, "n_steps": 2048, "learning_rate": 0.00025, "batch_size": 128 } DDPG_model_kwargs = { "action_noise": "ornstein_uhlenbeck", "buffer_size": 50000, "learning_rate": 0.000005, "batch_size": 128 } TD3_model_kwargs = { "batch_size": 100, "buffer_size": 1000000, "learning_rate": 0.001 } timesteps_dict = {'a2c': 4000, 'ppo': 4000, 'ddpg': 4000, 'td3': 4000} df_summary = ensemble_agent.run_ensemble_strategy(A2C_model_kwargs, PPO_model_kwargs, DDPG_model_kwargs, TD3_model_kwargs, timesteps_dict) print(df_summary) unique_trade_date = processed[(processed.date > val_test_start) & ( processed.date <= val_test_end)].date.unique() df_trade_date = pd.DataFrame({'datadate': unique_trade_date}) df_account_value = pd.DataFrame() for i in range(rebalance_window + validation_window, len(unique_trade_date) + 1, rebalance_window): print(rebalance_window + validation_window) print(len(unique_trade_date) + 1) print(rebalance_window) try: temp = pd.read_csv('results/account_value_trade_{}_{}.csv'.format( 'ensemble', i)) df_account_value = df_account_value.append(temp, ignore_index=True) except: break sharpe = (252**0.5) * df_account_value.account_value.pct_change( 1).mean() / df_account_value.account_value.pct_change(1).std() print('Sharpe Ratio: ', sharpe) df_account_value = df_account_value.join( df_trade_date[validation_window:].reset_index(drop=True)) df_account_value.account_value.plot() print("==============Get Backtest Results===========") now = datetime.datetime.now().strftime('%Y%m%d-%Hh%M') perf_stats_all = backtest_stats(account_value=df_account_value) perf_stats_all = pd.DataFrame(perf_stats_all) print("==============Compare to IHSG===========") backtest_plot(df_account_value, baseline_ticker='^DJI', baseline_start=df_account_value.loc[0, 'date'], baseline_end=df_account_value.loc[len(df_account_value) - 1, 'date'])
environment = e_trade_gym) df_account_value.shape df_account_value.head() df_actions.head() print("==============Get Backtest Results===========") now = datetime.datetime.now().strftime('%Y%m%d-%Hh%M') perf_stats_all = backtest_stats(account_value=df_account_value) perf_stats_all = pd.DataFrame(perf_stats_all) perf_stats_all.to_csv("./"+config.RESULTS_DIR+"/perf_stats_all_"+now+'.csv') print("==============Compare to DJIA===========") # %matplotlib inline # S&P 500: ^GSPC # Dow Jones Index: ^DJI # NASDAQ 100: ^NDX backtest_plot(df_account_value, config, baseline_start = '2019-01-01', baseline_end = '2021-01-01') print("==============Get Baseline Stats===========") baseline_df = get_baseline( ticker="^DJI", start = '2019-01-01', end = '2021-01-01') stats = backtest_stats(baseline_df, value_col_name = 'close')
def train_one(fetch=False): """ train an agent """ if fetch: df = fetch_and_store() else: df = load() counts = df[['date', 'tic']].groupby(['date']).count().tic assert counts.min() == counts.max() print("==============Start Feature Engineering===========") fe = FeatureEngineer( use_technical_indicator=True, tech_indicator_list=config.TECHNICAL_INDICATORS_LIST, use_turbulence=True, # use_turbulence=False, user_defined_feature=False, ) processed = fe.preprocess_data(df) # Training & Trading data split start_date, trade_date, end_date = calculate_split(df, start=config.START_DATE) print(start_date, trade_date, end_date) train = data_split(processed, start_date, trade_date) trade = data_split(processed, trade_date, end_date) print( f'\n******\nRunning from {start_date} to {end_date} for:\n{", ".join(config.CRYPTO_TICKER)}\n******\n' ) # calculate state action space stock_dimension = len(train.tic.unique()) state_space = (1 + (2 * stock_dimension) + (len(config.TECHNICAL_INDICATORS_LIST) * stock_dimension)) env_kwargs = { "hmax": 100, "initial_amount": 100000, "buy_cost_pct": 0.0026, "sell_cost_pct": 0.0026, "state_space": state_space, "stock_dim": stock_dimension, "tech_indicator_list": config.TECHNICAL_INDICATORS_LIST, "action_space": stock_dimension, "reward_scaling": 1e-4 } e_train_gym = StockTradingEnv(df=train, **env_kwargs) e_trade_gym = StockTradingEnv(df=trade, turbulence_threshold=250, make_plots=True, **env_kwargs) env_train, _ = e_train_gym.get_sb_env() env_trade, obs_trade = e_trade_gym.get_sb_env() agent = DRLAgent(env=env_train) print("==============Model Training===========") now = datetime.datetime.now().strftime(config.DATETIME_FMT) model_sac = agent.get_model("sac") trained_sac = agent.train_model( model=model_sac, tb_log_name="sac", # total_timesteps=100 total_timesteps=80000) print("==============Start Trading===========") df_account_value, df_actions = DRLAgent.DRL_prediction( # model=trained_sac, test_data=trade, test_env=env_trade, test_obs=obs_trade trained_sac, e_trade_gym) df_account_value.to_csv( f"./{config.RESULTS_DIR}/df_account_value_{now}.csv") df_actions.to_csv(f"./{config.RESULTS_DIR}/df_actions_{now}.csv") df_txns = pd.DataFrame(e_trade_gym.transactions, columns=['date', 'amount', 'price', 'symbol']) df_txns = df_txns.set_index(pd.DatetimeIndex(df_txns['date'], tz=pytz.utc)) df_txns.to_csv(f'./{config.RESULTS_DIR}/df_txns_{now}.csv') df_positions = pd.DataFrame(e_trade_gym.positions, columns=['date', 'cash'] + config.CRYPTO_TICKER) df_positions = df_positions.set_index( pd.DatetimeIndex(df_positions['date'], tz=pytz.utc)).drop(columns=['date']) df_positions['cash'] = df_positions.astype( {col: np.float64 for col in df_positions.columns}) df_positions.to_csv(f'./{config.RESULTS_DIR}/df_positions_{now}.csv') print("==============Get Backtest Results===========") perf_stats_all = backtest_stats(df_account_value, transactions=df_txns, positions=df_positions) perf_stats_all = pd.DataFrame(perf_stats_all) perf_stats_all.to_csv(f"./{config.RESULTS_DIR}/perf_stats_all_{now}.csv") backtest_plot(df_account_value, baseline_start=trade_date, baseline_end=end_date, positions=df_positions, transactions=df_txns)