start_date=train_start_date, end_date=train_end_date, tickers=train_tickers, device=device) print("Resample ...") buy_samples, sell_samples, none_samples = \ DataPreparator.over_sample( raw_buy_samples, raw_sell_samples, raw_none_samples, seed) os.makedirs(f'data/networks/{run_id}', exist_ok=True) Logger.log(run_id, f"Id: {run_id}") if args.train_buyer > 0: buyer_features = np.array(buy_samples, dtype=np.float32) print("Train buyer samples detector ...") buyer_result = gym.train_auto_encoder('buyer', trader.buyer, buyer_optimizer, buyer_features, buyer_result, max_epochs=500, max_steps=10) print("Reload trader with best training result after training ...") buyer_optimizer, buyer_result = manager.load( 'buyer', trader.buyer, buyer_optimizer, trader.reset_buyer, lambda: manager.create_buyer_optimizer(trader), buyer_result)
def trade(self, run_id, profit_taking_threshold=None, buy_and_hold=False, intra_day=False): result = '' print( f"Trade limited all stocks from {self.start_date} to {self.end_date} ..." ) message, limit_all_investments, limit_all_gain_loss = \ self._trade( quotes=self.all_quotes, all_tickers=self.all_tickers, profit_taking_threshold=profit_taking_threshold, buy_and_hold=buy_and_hold, report_each_trade=True, tickers=self.stock_exchange.tickers, max_positions=self.max_limit_positions) result += f'\nTrade Portfolio (max {self.max_limit_positions} stocks): {message}' print( f"Trade all stocks from {self.start_date} to {self.end_date} ...") message, all_investments, all_gain_loss = \ self._trade( quotes=self.all_quotes, all_tickers=self.all_tickers, profit_taking_threshold=profit_taking_threshold, buy_and_hold=buy_and_hold, report_each_trade=True, tickers=self.stock_exchange.tickers, max_positions=self.max_positions) result += f'\nTrade All ({self.max_positions} stocks): {message}' print( f"Buy and hold all stocks from {self.start_date} to {self.end_date} ..." ) message = \ self._buy_and_hold( self.all_quotes, self.all_tickers, False, self.stock_exchange.tickers) result += f'\nBuy % Hold All ({len(self.stock_exchange.tickers)} stocks): {message}' print(result) Logger.log(run_id, result) index_ticker = 'URTH' index_title = self.stock_exchange.etf_tickers[index_ticker] if intra_day: compare_index = self.stock_exchange.load_intra_day( index_ticker, self.start_date, self.end_date, True) else: compare_index = self.stock_exchange.load(index_ticker, self.start_date, self.end_date, True) all_title = f'All stocks ({self.max_positions} positions)' limit_all_title = f'All stocks (max. {self.max_limit_positions} positions at once)' gain_loss_all_title = f'Return all stocks ({self.max_positions} positions)' gain_loss_limit_all_title = f'Return all stocks (max. {self.max_limit_positions} positions at once)' length = (len(compare_index) if len(compare_index) < len(all_investments) else len(all_investments)) resulting_frame = pd.DataFrame( data={ 'index': range(length), 'date': np.array(compare_index['date'].values[-length:]), index_title: np.array(compare_index['adj_close'].values[-length:]), all_title: np.array(all_investments[-length:]), limit_all_title: np.array(limit_all_investments[-length:]), gain_loss_all_title: np.array(all_gain_loss[-length:]) + self.start_capital, gain_loss_limit_all_title: np.array(limit_all_gain_loss[-length:]) + self.start_capital }) all_columns = [ index_title, all_title, limit_all_title, gain_loss_all_title, gain_loss_limit_all_title ] for column in all_columns: change_column = f'Change {column}' resulting_frame[change_column] = resulting_frame[ column].pct_change(1).fillna(0.0) * 100.0 resulting_frame[column] = \ resulting_frame.apply( lambda row: resulting_frame[change_column].values[0:int(row['index'] + 1)].sum(), axis=1) resulting_frame.set_index(resulting_frame['date'], inplace=True) fig, axis = plt.subplots(nrows=2) investment_columns = [all_title, limit_all_title] resulting_frame[index_title].plot.area(ax=axis[0], stacked=False) resulting_frame[investment_columns].plot( ax=axis[0], figsize=(20, 10), linewidth=2, title=f'Investment vs {index_title}') gain_loss_columns = [gain_loss_all_title, gain_loss_limit_all_title] resulting_frame[index_title].plot.area(ax=axis[1], stacked=False) resulting_frame[gain_loss_columns].plot( ax=axis[1], figsize=(20, 10), linewidth=2, title=f'Portfolio Changes vs {index_title}') x_min, x_max = self._get_x_min_max(resulting_frame) axis[0].set_xlim(x_min, x_max) axis[1].set_xlim(x_min, x_max) y_min, y_max = self._get_y_min_max(resulting_frame, investment_columns, index_title) axis[0].set_ylim(y_min, y_max) y_min, y_max = self._get_y_min_max(resulting_frame, gain_loss_columns, index_title) axis[1].set_ylim(y_min, y_max) results = resulting_frame[gain_loss_columns].copy() results.to_csv(f'data/{run_id}.trading.gain_loss.csv') self._colorize_plot(fig, axis) plt.savefig(f'data/{run_id}.chart.png') plt.show() plt.close()