def run(self, trX, trY, tsX, tsY, maxiter, validating=False): self.scale(trX, tsX) weights = self.model.weights(trX, seed=1) gradients = zeros(weights.shape) trR = zeros(maxiter+1) tsR = zeros(maxiter+1) costs = [] for i in range(maxiter): #rs = RandomState(i) #r = rs.randint(0, trX.shape[0]/2) #tempX = trX[r:, :] #tempY = trY[r:] gradient = self.model.grad(weights, trX, trY) gradients += power(gradient, 2) rate = 1. / sqrt(gradients) weights = weights - rate*gradient if validating: costs.append(self.model.cost(weights, tsX, tsY)) # Calculate returns and decisions on test set. returns, decisions = self.model.returns(weights, tsX, tsY) s = sharpe(returns) w = wealth(returns) return returns, decisions
def run(self, trX, trY, tsX, tsY, maxiter, validating=False): self.scale(trX, tsX) weights = self.model.weights(trX, seed=1) gradients = zeros(weights.shape) trR = zeros(maxiter + 1) tsR = zeros(maxiter + 1) costs = [] for i in range(maxiter): #rs = RandomState(i) #r = rs.randint(0, trX.shape[0]/2) #tempX = trX[r:, :] #tempY = trY[r:] gradient = self.model.grad(weights, trX, trY) gradients += power(gradient, 2) rate = 1. / sqrt(gradients) weights = weights - rate * gradient if validating: costs.append(self.model.cost(weights, tsX, tsY)) # Calculate returns and decisions on test set. returns, decisions = self.model.returns(weights, tsX, tsY) s = sharpe(returns) w = wealth(returns) return returns, decisions
# filename = 'figures/synthetic_noside_single_w_%i_s_%i_l_%i_d_%f.pdf' % ( # window, slide, lookback, delta) # title = 'Single Layer - Synthetic (No Side Information)' # print "%s\tWealth: %f\tSharpe: %f" % (filename, # wealth(returns)[-1], sharpe(returns)[-1]) # plotter.save(filename, title, series, returns, decisions) for window in [200]: for slide in [5]: for lookback in [10]: for delta in [0.001]: models = [] for hidden in [4, 6, 8]: for lmb in [0.0, 0.0001, 0.001, 0.01]: models.append( Nonlinear(delta=delta, lmb=lmb, hidden=hidden)) trainer = ValidatingTrainer(data, models) returns, decisions = trainer.train(window=window, lookback=lookback, slide=slide, maxiter=20) filename = 'figures/synthetic_noside_multi_w_%i_s_%i_l_%i_d_%f.pdf' % ( window, slide, lookback, delta) title = 'Multiple Layer - Synthetic (No Side Information)' print "%s\tWealth: %f\tSharpe: %f" % ( filename, wealth(returns)[-1], sharpe(returns)[-1]) plotter.save(filename, title, series, returns, decisions)
def test_calculates_sharpe(self): self.assertAlmostEqual(sharpe(self.returns)[-1], 1.03209369)
def exp_symbols_statistics(fout_path=os.path.join( DATA_DIR, 'exp_symbols_statistics.xlsx')): """ statistics of experiment symbols output the results to xlsx """ t0 = time() fin_path = os.path.join(SYMBOLS_PKL_DIR, 'TAIEX_2005_largest50cap_panel.pkl') # shape: (n_exp_period, n_stock, ('simple_roi', 'close_price')) panel = pd.read_pickle(fin_path) assert panel.major_axis.tolist() == EXP_SYMBOLS panel = panel.loc[date(2005, 1, 3):date(2014, 12, 31)] # the roi in the first experiment date is zero panel.loc[date(2005, 1, 3), :, 'simple_roi'] = 0. stat_indices = ( # basic information 'start_date', 'end_date', 'n_exp_period', 'n_period_up', 'n_period_down', # roi 'cum_roi', 'daily_roi', 'daily_mean_roi', 'daily_std_roi', 'daily_skew_roi', 'daily_kurt_roi', # roi/risk indices 'sharpe', 'sortino_full', 'sortino_full_semi_std', 'sortino_partial', 'sortino_partial_semi_std', 'max_abs_drawdown', # normal tests 'JB', 'JB_pvalue', # uni-root tests 'ADF_c', 'ADF_c_pvalue', 'ADF_ct', 'ADF_ct_pvalue', 'ADF_ctt', 'ADF_ctt_pvalue', 'ADF_nc', 'ADF_nc_pvalue', 'DFGLS_c', 'DFGLS_c_pvalue', 'DFGLS_ct', 'DFGLS_ct_pvalue', 'PP_c', 'PP_c_pvalue', 'PP_ct', 'PP_ct_pvalue', 'PP_nc', 'PP_nc_pvalue', 'KPSS_c', 'KPSS_c_pvalue', 'KPSS_ct', 'KPSS_ct_pvalue', # performance 'SPA_l_pvalue', 'SPA_c_pvalue', 'SPA_u_pvalue' ) stat_df = pd.DataFrame(np.zeros((len(stat_indices), len(EXP_SYMBOLS))), index=stat_indices, columns=EXP_SYMBOLS) for rdx, symbol in enumerate(EXP_SYMBOLS): t1 = time() rois = panel[:, symbol, 'simple_roi'] # basic stat_df.loc['start_date', symbol] = rois.index[0].strftime("%Y/%b/%d") stat_df.loc['end_date', symbol] = rois.index[-1].strftime("%Y/%b/%d") stat_df.loc['n_exp_period', symbol] = len(rois) stat_df.loc['n_period_up', symbol] = (rois > 0).sum() stat_df.loc['n_period_down', symbol] = (rois < 0).sum() # roi stat_df.loc['cum_roi', symbol] = (rois + 1.).prod() - 1 stat_df.loc['daily_roi', symbol] = np.power((rois + 1.).prod(), 1. / len(rois)) - 1 stat_df.loc['daily_mean_roi', symbol] = rois.mean() stat_df.loc['daily_std_roi', symbol] = rois.std() stat_df.loc['daily_skew_roi', symbol] = rois.skew() stat_df.loc['daily_kurt_roi', symbol] = rois.kurt() # excess # roi/risk indices stat_df.loc['sharpe', symbol] = sharpe(rois) (stat_df.loc['sortino_full', symbol], stat_df.loc['sortino_full_semi_std', symbol]) = sortino_full(rois) (stat_df.loc['sortino_partial', symbol], stat_df.loc['sortino_partial_semi_std', symbol]) = sortino_partial( rois) stat_df.loc['max_abs_drawdown', symbol] = maximum_drawdown(rois) # normal tests jb = jarque_bera(rois) stat_df.loc['JB', symbol] = jb[0] stat_df.loc['JB_pvalue', symbol] = jb[1] # uniroot tests adf_c = adfuller(rois, regression='c') stat_df.loc['ADF_c', symbol] = adf_c[0] stat_df.loc['ADF_c_pvalue', symbol] = adf_c[1] adf_ct = adfuller(rois, regression='ct') stat_df.loc['ADF_ct', symbol] = adf_ct[0] stat_df.loc['ADF_ct_pvalue', symbol] = adf_ct[1] adf_ctt = adfuller(rois, regression='ctt') stat_df.loc['ADF_ctt', symbol] = adf_ctt[0] stat_df.loc['ADF_ctt_pvalue', symbol] = adf_ctt[1] adf_nc = adfuller(rois, regression='nc') stat_df.loc['ADF_nc', symbol] = adf_nc[0] stat_df.loc['ADF_nc_pvalue', symbol] = adf_nc[1] dfgls_c_instance = DFGLS(rois, trend='c') dfgls_c, dfgls_c_pvalue = (dfgls_c_instance.stat, dfgls_c_instance.pvalue) stat_df.loc['DFGLS_c', symbol] = dfgls_c stat_df.loc['DFGLS_c_pvalue', symbol] = dfgls_c_pvalue dfgls_ct_instance = DFGLS(rois, trend='ct') dfgls_ct, dfgls_ct_pvalue = (dfgls_ct_instance.stat, dfgls_ct_instance.pvalue) stat_df.loc['DFGLS_ct', symbol] = dfgls_ct stat_df.loc['DFGLS_ct_pvalue', symbol] = dfgls_ct_pvalue pp_c_instance = PhillipsPerron(rois, trend='c') pp_c, pp_c_pvalue = (pp_c_instance.stat, pp_c_instance.pvalue) stat_df.loc['PP_c', symbol] = pp_c stat_df.loc['PP_c_pvalue', symbol] = pp_c_pvalue pp_ct_instance = PhillipsPerron(rois, trend='ct') pp_ct, pp_ct_pvalue = (pp_ct_instance.stat, pp_ct_instance.pvalue) stat_df.loc['PP_ct', symbol] = pp_ct stat_df.loc['PP_ct_pvalue', symbol] = pp_ct_pvalue pp_nc_instance = PhillipsPerron(rois, trend='nc') pp_nc, pp_nc_pvalue = (pp_nc_instance.stat, pp_nc_instance.pvalue) stat_df.loc['PP_nc', symbol] = pp_nc stat_df.loc['PP_nc_pvalue', symbol] = pp_nc_pvalue kpss_c_instance = KPSS(rois, trend='c') kpss_c, kpss_c_pvalue = (kpss_c_instance.stat, kpss_c_instance.pvalue) stat_df.loc['KPSS_c', symbol] = kpss_c stat_df.loc['KPSS_c_pvalue', symbol] = kpss_c_pvalue kpss_ct_instance = KPSS(rois, trend='ct') kpss_ct, kpss_ct_pvalue = (kpss_ct_instance.stat, kpss_ct_instance.pvalue) stat_df.loc['KPSS_ct', symbol] = kpss_ct stat_df.loc['KPSS_ct_pvalue', symbol] = kpss_ct_pvalue # performance spa = SPA(rois, np.zeros(len(rois)), reps=5000) spa.seed(np.random.randint(0, 2 ** 31 - 1)) spa.compute() stat_df.loc['SPA_l_pvalue', symbol] = spa.pvalues[0] stat_df.loc['SPA_c_pvalue', symbol] = spa.pvalues[1] stat_df.loc['SPA_u_pvalue', symbol] = spa.pvalues[2] print ("[{}/{}] {} roi statistics OK, {:.3f} secs".format( rdx + 1, len(EXP_SYMBOLS), symbol, time() - t1 )) # write to excel writer = pd.ExcelWriter(fout_path, engine='xlsxwriter') stat_df = stat_df.T stat_df.to_excel(writer, sheet_name='stats') # Get the xlsxwriter workbook and worksheet objects. workbook = writer.book worksheet = writer.sheets['stats'] # basic formats. # set header header_fmt = workbook.add_format() header_fmt.set_text_wrap() worksheet.set_row(0, 15, header_fmt) # set date date_fmt = workbook.add_format({'num_format': 'yy/mmm/dd'}) date_fmt.set_align('right') worksheet.set_column('B:C', 12, date_fmt) # set percentage percent_fmt = workbook.add_format({'num_format': '0.00%'}) worksheet.set_column('G:J', 8, percent_fmt) worksheet.set_column('M:Q', 8, percent_fmt) worksheet.set_column('T:T', 8, percent_fmt) worksheet.set_column('V:V', 8, percent_fmt) worksheet.set_column('X:X', 8, percent_fmt) worksheet.set_column('Z:Z', 8, percent_fmt) worksheet.set_column('AB:AB', 8, percent_fmt) worksheet.set_column('AD:AD', 8, percent_fmt) worksheet.set_column('AF:AF', 8, percent_fmt) worksheet.set_column('AH:AH', 8, percent_fmt) worksheet.set_column('AJ:AJ', 8, percent_fmt) worksheet.set_column('AL:AL', 8, percent_fmt) worksheet.set_column('AN:AN', 8, percent_fmt) worksheet.set_column('AP:AP', 8, percent_fmt) worksheet.set_column('AQ:AS', 8, percent_fmt) writer.save() print ("all roi statistics OK, {:.3f} secs".format(time() - t0))
# slide=slide, maxiter=20) # # filename = 'figures/synthetic_noside_single_w_%i_s_%i_l_%i_d_%f.pdf' % ( # window, slide, lookback, delta) # title = 'Single Layer - Synthetic (No Side Information)' # print "%s\tWealth: %f\tSharpe: %f" % (filename, # wealth(returns)[-1], sharpe(returns)[-1]) # plotter.save(filename, title, series, returns, decisions) for window in [200]: for slide in [5]: for lookback in [10]: for delta in [0.001]: models = [] for hidden in [4, 6, 8]: for lmb in [0.0, 0.0001, 0.001, 0.01]: models.append(Nonlinear(delta=delta, lmb=lmb, hidden=hidden)) trainer = ValidatingTrainer(data, models) returns, decisions = trainer.train(window=window, lookback=lookback, slide=slide, maxiter=20) filename = 'figures/synthetic_noside_multi_w_%i_s_%i_l_%i_d_%f.pdf' % ( window, slide, lookback, delta) title = 'Multiple Layer - Synthetic (No Side Information)' print "%s\tWealth: %f\tSharpe: %f" % (filename, wealth(returns)[-1], sharpe(returns)[-1]) plotter.save(filename, title, series, returns, decisions)
window=window, lookback=lookback, slide=slide, maxiter=40) padding = zeros(len(series)-len(returns)) returns = append(padding, returns) decisions = append(padding, decisions) # Linear : 2,618,987,047.18 # Nonlin (15): 2,272,781,214.71 # Nonlin (10): 2,884,104,773.60 # Nonlin (5) : 1,232,632,146.82 # Linear: print "Wealth: ", wealth(returns)[-1] x_axis = range(len(series)) # Two subplots, the axes array is 1-d f, axarr = plt.subplots(5, sharex=True) axarr[0].plot(x_axis, series) axarr[0].set_title('Prices') axarr[1].plot(x_axis, wealth(returns)) axarr[1].set_title('Wealth') axarr[2].plot(x_axis, sharpe(returns)) axarr[2].set_title('Sharpe') axarr[3].plot(x_axis, returns) axarr[3].set_title('Returns') axarr[4].plot(x_axis, decisions) axarr[4].set_title('Decisions') plt.show()
def exp_symbols_statistics(fout_path=os.path.join( DATA_DIR, 'exp_symbols_statistics.xlsx')): """ statistics of experiment symbols output the results to xlsx """ t0 = time() fin_path = os.path.join(SYMBOLS_PKL_DIR, 'TAIEX_2005_largest50cap_panel.pkl') # shape: (n_exp_period, n_stock, ('simple_roi', 'close_price')) panel = pd.read_pickle(fin_path) assert panel.major_axis.tolist() == EXP_SYMBOLS panel = panel.loc[date(2005, 1, 3):date(2014, 12, 31)] # the roi in the first experiment date is zero panel.loc[date(2005, 1, 3), :, 'simple_roi'] = 0. stat_indices = ( # basic information 'start_date', 'end_date', 'n_exp_period', 'n_period_up', 'n_period_down', # roi 'cum_roi', 'daily_roi', 'daily_mean_roi', 'daily_std_roi', 'daily_skew_roi', 'daily_kurt_roi', # roi/risk indices 'sharpe', 'sortino_full', 'sortino_full_semi_std', 'sortino_partial', 'sortino_partial_semi_std', 'max_abs_drawdown', # normal tests 'JB', 'JB_pvalue', # uni-root tests 'ADF_c', 'ADF_c_pvalue', 'ADF_ct', 'ADF_ct_pvalue', 'ADF_ctt', 'ADF_ctt_pvalue', 'ADF_nc', 'ADF_nc_pvalue', 'DFGLS_c', 'DFGLS_c_pvalue', 'DFGLS_ct', 'DFGLS_ct_pvalue', 'PP_c', 'PP_c_pvalue', 'PP_ct', 'PP_ct_pvalue', 'PP_nc', 'PP_nc_pvalue', 'KPSS_c', 'KPSS_c_pvalue', 'KPSS_ct', 'KPSS_ct_pvalue', # performance 'SPA_l_pvalue', 'SPA_c_pvalue', 'SPA_u_pvalue') stat_df = pd.DataFrame(np.zeros((len(stat_indices), len(EXP_SYMBOLS))), index=stat_indices, columns=EXP_SYMBOLS) for rdx, symbol in enumerate(EXP_SYMBOLS): t1 = time() rois = panel[:, symbol, 'simple_roi'] # basic stat_df.loc['start_date', symbol] = rois.index[0].strftime("%Y/%b/%d") stat_df.loc['end_date', symbol] = rois.index[-1].strftime("%Y/%b/%d") stat_df.loc['n_exp_period', symbol] = len(rois) stat_df.loc['n_period_up', symbol] = (rois > 0).sum() stat_df.loc['n_period_down', symbol] = (rois < 0).sum() # roi stat_df.loc['cum_roi', symbol] = (rois + 1.).prod() - 1 stat_df.loc['daily_roi', symbol] = np.power( (rois + 1.).prod(), 1. / len(rois)) - 1 stat_df.loc['daily_mean_roi', symbol] = rois.mean() stat_df.loc['daily_std_roi', symbol] = rois.std() stat_df.loc['daily_skew_roi', symbol] = rois.skew() stat_df.loc['daily_kurt_roi', symbol] = rois.kurt() # excess # roi/risk indices stat_df.loc['sharpe', symbol] = sharpe(rois) (stat_df.loc['sortino_full', symbol], stat_df.loc['sortino_full_semi_std', symbol]) = sortino_full(rois) (stat_df.loc['sortino_partial', symbol], stat_df.loc['sortino_partial_semi_std', symbol]) = sortino_partial(rois) stat_df.loc['max_abs_drawdown', symbol] = maximum_drawdown(rois) # normal tests jb = jarque_bera(rois) stat_df.loc['JB', symbol] = jb[0] stat_df.loc['JB_pvalue', symbol] = jb[1] # uniroot tests adf_c = adfuller(rois, regression='c') stat_df.loc['ADF_c', symbol] = adf_c[0] stat_df.loc['ADF_c_pvalue', symbol] = adf_c[1] adf_ct = adfuller(rois, regression='ct') stat_df.loc['ADF_ct', symbol] = adf_ct[0] stat_df.loc['ADF_ct_pvalue', symbol] = adf_ct[1] adf_ctt = adfuller(rois, regression='ctt') stat_df.loc['ADF_ctt', symbol] = adf_ctt[0] stat_df.loc['ADF_ctt_pvalue', symbol] = adf_ctt[1] adf_nc = adfuller(rois, regression='nc') stat_df.loc['ADF_nc', symbol] = adf_nc[0] stat_df.loc['ADF_nc_pvalue', symbol] = adf_nc[1] dfgls_c_instance = DFGLS(rois, trend='c') dfgls_c, dfgls_c_pvalue = (dfgls_c_instance.stat, dfgls_c_instance.pvalue) stat_df.loc['DFGLS_c', symbol] = dfgls_c stat_df.loc['DFGLS_c_pvalue', symbol] = dfgls_c_pvalue dfgls_ct_instance = DFGLS(rois, trend='ct') dfgls_ct, dfgls_ct_pvalue = (dfgls_ct_instance.stat, dfgls_ct_instance.pvalue) stat_df.loc['DFGLS_ct', symbol] = dfgls_ct stat_df.loc['DFGLS_ct_pvalue', symbol] = dfgls_ct_pvalue pp_c_instance = PhillipsPerron(rois, trend='c') pp_c, pp_c_pvalue = (pp_c_instance.stat, pp_c_instance.pvalue) stat_df.loc['PP_c', symbol] = pp_c stat_df.loc['PP_c_pvalue', symbol] = pp_c_pvalue pp_ct_instance = PhillipsPerron(rois, trend='ct') pp_ct, pp_ct_pvalue = (pp_ct_instance.stat, pp_ct_instance.pvalue) stat_df.loc['PP_ct', symbol] = pp_ct stat_df.loc['PP_ct_pvalue', symbol] = pp_ct_pvalue pp_nc_instance = PhillipsPerron(rois, trend='nc') pp_nc, pp_nc_pvalue = (pp_nc_instance.stat, pp_nc_instance.pvalue) stat_df.loc['PP_nc', symbol] = pp_nc stat_df.loc['PP_nc_pvalue', symbol] = pp_nc_pvalue kpss_c_instance = KPSS(rois, trend='c') kpss_c, kpss_c_pvalue = (kpss_c_instance.stat, kpss_c_instance.pvalue) stat_df.loc['KPSS_c', symbol] = kpss_c stat_df.loc['KPSS_c_pvalue', symbol] = kpss_c_pvalue kpss_ct_instance = KPSS(rois, trend='ct') kpss_ct, kpss_ct_pvalue = (kpss_ct_instance.stat, kpss_ct_instance.pvalue) stat_df.loc['KPSS_ct', symbol] = kpss_ct stat_df.loc['KPSS_ct_pvalue', symbol] = kpss_ct_pvalue # performance spa = SPA(rois, np.zeros(len(rois)), reps=5000) spa.seed(np.random.randint(0, 2**31 - 1)) spa.compute() stat_df.loc['SPA_l_pvalue', symbol] = spa.pvalues[0] stat_df.loc['SPA_c_pvalue', symbol] = spa.pvalues[1] stat_df.loc['SPA_u_pvalue', symbol] = spa.pvalues[2] print("[{}/{}] {} roi statistics OK, {:.3f} secs".format( rdx + 1, len(EXP_SYMBOLS), symbol, time() - t1)) # write to excel writer = pd.ExcelWriter(fout_path, engine='xlsxwriter') stat_df = stat_df.T stat_df.to_excel(writer, sheet_name='stats') # Get the xlsxwriter workbook and worksheet objects. workbook = writer.book worksheet = writer.sheets['stats'] # basic formats. # set header header_fmt = workbook.add_format() header_fmt.set_text_wrap() worksheet.set_row(0, 15, header_fmt) # set date date_fmt = workbook.add_format({'num_format': 'yy/mmm/dd'}) date_fmt.set_align('right') worksheet.set_column('B:C', 12, date_fmt) # set percentage percent_fmt = workbook.add_format({'num_format': '0.00%'}) worksheet.set_column('G:J', 8, percent_fmt) worksheet.set_column('M:Q', 8, percent_fmt) worksheet.set_column('T:T', 8, percent_fmt) worksheet.set_column('V:V', 8, percent_fmt) worksheet.set_column('X:X', 8, percent_fmt) worksheet.set_column('Z:Z', 8, percent_fmt) worksheet.set_column('AB:AB', 8, percent_fmt) worksheet.set_column('AD:AD', 8, percent_fmt) worksheet.set_column('AF:AF', 8, percent_fmt) worksheet.set_column('AH:AH', 8, percent_fmt) worksheet.set_column('AJ:AJ', 8, percent_fmt) worksheet.set_column('AL:AL', 8, percent_fmt) worksheet.set_column('AN:AN', 8, percent_fmt) worksheet.set_column('AP:AP', 8, percent_fmt) worksheet.set_column('AQ:AS', 8, percent_fmt) writer.save() print("all roi statistics OK, {:.3f} secs".format(time() - t0))
def step(self, t, action_t): self.action[t] = action_t # end = t+2 if self.trading_rule == 'daytrading0': if action_t != 0: self.reward[t] = utils.sharpe(self.action[t + 1 - self.L:t + 1] \ * self.r_t[t + 2 - self.L:t +2] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward # end = t+2 elif self.trading_rule == 'daytrading1': if action_t != 0: self.reward[t] = utils.sharpe( self.action[t] * self.r_t[t + 2 - self.L:t + 2] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward # start = t+1 elif self.trading_rule == 'daytrading2': if action_t != 0: self.reward[t] = utils.sharpe( self.action[t] * self.r_t[t + 1:t + self.L + 1] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward # start = t+1 elif self.trading_rule == 'daytrading3': if action_t != 0: self.reward[t] = utils.sharpe_short_term( self.action[t] * self.r_t[t + 1:t + self.L + 1] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward elif self.trading_rule == 'daytrading4': if action_t != 0: self.reward[t] = utils.sharpe_short_term2( self.action[t] * self.r_t[t + 2 - self.L:t + 2] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward # start = t+1 elif self.trading_rule == 'fixed_period': if action_t != 0 and t >= self.last_position_time + self.L: self.reward[t] = utils.sharpe( self.action[t] * self.r_t[t + 1:t + self.L + 1] \ - self.transaction_cost) self.last_position_time = t else: self.reward[t] = self.no_trade_reward # start = t+1 elif self.trading_rule == 'hold0': if self.action[t] != self.action[t - 1] and self.action[t] != 0: self.reward[t] = utils.sharpe_short_term( self.action[t] * self.r_t[t + 1:t + self.L + 1] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward # start = t+1 elif self.trading_rule == 'hold1': if self.action[t] != self.action[t - 1] and self.action[t] != 0: self.reward[t] = utils.sharpe( self.action[t] * self.r_t[t + 1:t + self.L + 1] \ - self.transaction_cost) else: self.reward[t] = self.no_trade_reward elif self.trading_rule == 'hold2': if self.action[t] != self.action[t - 1] and self.action[t] != 0: self.reward[t] = utils.sharpe(self.action[t + 1 - self.L:t + 1] \ * self.r_t[t + 2 - self.L:t + 2] - self.transaction_cost) else: self.reward[t] = self.no_trade_reward else: raise ValueError( 'invalid trading rule: supported daytrading, fixed_period and hold' ) next_state = self.rl_method.update(t, self.action) return (next_state, self.reward[t])