def __init__(self, config, restore_dir=None, device="cpu"): self.__config = config self.__coin_number = config["input"]["coin_number"] self.__net = network.CNN( config["input"]["feature_number"], self.__coin_number, config["input"]["window_size"], config["layers"], device=device, ) self.__global_step = tf.Variable(0, trainable=False, name="global_step") self.__train_operation = None self.__y = tf.placeholder(tf.float32, shape=[ None, self.__config["input"]["feature_number"], self.__coin_number ], name="y") self.__future_price = tf.concat( [tf.ones([self.__net.input_num, 1]), self.__y[:, 0, :]], 1) self.__future_omega = ( self.__future_price * self.__net.output) / tf.reduce_sum( self.__future_price * self.__net.output, axis=1)[:, None] # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0)) self.__commission_ratio = self.__config["trading"][ "trading_consumption"] self.__pv_vector = tf.reduce_sum( self.__net.output * self.__future_price, reduction_indices=[1]) * ( tf.concat([tf.ones(1), self.__pure_pc()], axis=0)) self.__log_mean_free = tf.reduce_mean( tf.log( tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]))) self.__portfolio_value = tf.reduce_prod(self.__pv_vector) self.__mean = tf.reduce_mean(self.__pv_vector) self.__log_mean = tf.reduce_mean(tf.log(self.__pv_vector)) self.__standard_deviation = tf.sqrt( tf.reduce_mean((self.__pv_vector - self.__mean)**2)) self.__sharp_ratio = (self.__mean - 1) / self.__standard_deviation self.__loss = self.__set_loss_function() self.__train_operation = self.init_train( learning_rate=self.__config["training"]["learning_rate"], decay_steps=self.__config["training"]["decay_steps"], decay_rate=self.__config["training"]["decay_rate"], training_method=self.__config["training"]["training_method"], ) self.__saver = tf.train.Saver() if restore_dir: logging.info("Restoring pre-trained model from %s" % restore_dir) self.__saver.restore(self.__net.session, restore_dir) else: self.__net.session.run(tf.global_variables_initializer())
def __init__(self, config, restore_dir=None, device="cpu"): self.__config = config self.__asset_number = config["input"]["asset_number"] self.__net = network.CNN(config["input"]["feature_number"], self.__asset_number, config["input"]["window_size"], config["layers"], device=device) self.__global_step = tf.Variable(0, trainable=False) self.__train_operation = None # y: price relative vector (논문 식(1) y) self.__y = tf.placeholder(tf.float32, shape=[ None, self.__config["input"]["feature_number"], self.__asset_number ]) # t+1 시점의 상대가격(t시점 대비) (맨 처음 값 1은 원화의 가격을 의미) self.__future_price = tf.concat( [tf.ones([self.__net.input_num, 1]), self.__y[:, 0, :]], 1) # t+1 시점의 가격이 변동됨에 따라 달라진 portfolio vector(논문 식(7) w') self.__future_omega = (self.__future_price * self.__net.output) /\ tf.reduce_sum(self.__future_price * self.__net.output, axis=1)[:, None] # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0)) self.__commission_ratio = self.__config["trading"][ "trading_consumption"] # portfolio value 의 vector 각각의 종목별 portfolio value 변화율을 의미 self.__pv_vector = tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]) *\ (tf.concat([tf.ones(1), self.__pure_pc()], axis=0)) self.__log_mean_free = tf.reduce_mean( tf.log( tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]))) self.__portfolio_value = tf.reduce_prod(self.__pv_vector) self.__mean = tf.reduce_mean(self.__pv_vector) self.__log_mean = tf.reduce_mean(tf.log(self.__pv_vector)) self.__standard_deviation = tf.sqrt( tf.reduce_mean((self.__pv_vector - self.__mean)**2)) self.__sharp_ratio = (self.__mean - 1) / self.__standard_deviation self.__loss = self.__set_loss_function() self.__train_operation = self.init_train( learning_rate=self.__config["training"]["learning_rate"], decay_steps=self.__config["training"]["decay_steps"], decay_rate=self.__config["training"]["decay_rate"], training_method=self.__config["training"]["training_method"]) self.__saver = tf.train.Saver() if restore_dir: self.__saver.restore(self.__net.session, restore_dir) else: self.__net.session.run(tf.global_variables_initializer())
def __init__(self, config, restore_dir=None, device="cpu"): self.__config = config self.__coin_number = config["input"]["coin_number"] self.__net = network.CNN(config["input"]["feature_number"], self.__coin_number, config["input"]["window_size"], config["layers"], device=device) self.__global_step = tf.Variable(0, trainable=False) self.__train_operation = None self.__y = tf.placeholder(tf.float32, shape=[ None, self.__config["input"]["feature_number"], self.__coin_number ]) self.__future_price = tf.concat( [tf.ones([self.__net.input_num, 1]), self.__y[:, 0, :]], 1) #lijin:price[batch,coin_number+1],加入现金 self.__future_omega = (self.__future_price * self.__net.output) /\ tf.reduce_sum(self.__future_price * self.__net.output, axis=1)[:, None] #lijin:算出预测权重经过价格调整后的权重 # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0)) self.__commission_ratio = self.__config["trading"][ "trading_consumption"] self.__pv_vector = tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]) *\ (tf.concat([tf.ones(1), self.__pure_pc()], axis=0)) #lijin: [batch,1] 元素为mu*Y*omega,算上交易费用后的资产价值 self.__log_mean_free = tf.reduce_mean( tf.log( tf.reduce_sum( self.__net.output * self.__future_price, reduction_indices=[1]))) #lijin:整个batch的单步平均奖励(不计交易费用) self.__portfolio_value = tf.reduce_prod( self.__pv_vector) #lijin:batch的整体资产增值后价值(算了交易费用) self.__mean = tf.reduce_mean( self.__pv_vector) #lijin:单步资产增值的平均值(计上交易费用) self.__log_mean = tf.reduce_mean(tf.log( self.__pv_vector)) #lijin:整个batch的单步平均奖励(计上交易费用) self.__standard_deviation = tf.sqrt( tf.reduce_mean( (self.__pv_vector - self.__mean)**2)) #lijin:增值率的标准差 self.__sharp_ratio = ( self.__mean - 1 ) / self.__standard_deviation #lijin:夏普比率 收益率/收益标准差 !!评价体系必须基于发生了操作的基础上吗?可以不这么看,因为持有也是一种操作策略 self.__loss = self.__set_loss_function() self.__train_operation = self.init_train( learning_rate=self.__config["training"]["learning_rate"], decay_steps=self.__config["training"]["decay_steps"], decay_rate=self.__config["training"]["decay_rate"], training_method=self.__config["training"]["training_method"]) self.__saver = tf.train.Saver() if restore_dir: self.__saver.restore(self.__net.session, restore_dir) else: self.__net.session.run(tf.global_variables_initializer())
def __init__(self, config, device="cpu"): self.__config = config self.__coin_number = config["input"]["coin_number"] self.__net = network.CNN(config["input"]["feature_number"], self.__coin_number, config["input"]["window_size"], config["layers"], device=device) self.__stockNum = config["input"]["stock_num"] self.__global_step = tf.Variable(0, trainable=False) self.__train_operation = None self.__y = tf.placeholder(tf.float32, shape=[ None, self.__config["input"]["feature_number"], self.__coin_number ]) self.__stock_index = tf.placeholder(tf.float32, shape=[None, 1, 1]) self.__stock_index = tf.squeeze(self.__stock_index) # shape[batch,1,stockNum,2] # self.all_market_capitalization = tf.placeholder(tf.float32, shape=[None, 1, self.__stockNum,2]) # self.all_market_capitalization_squeeze = tf.squeeze(self.all_market_capitalization) # shape[batch,1,coin_number,2] # self.market_capitalization = tf.placeholder(tf.float32, shape=[None, 1, self.__coin_number,2]) # self.market_capitalization_squeeze = tf.squeeze(self.market_capitalization) # self.__future_price = tf.concat([tf.ones([self.__net.input_num, 1]), # self.__y[:, 0, :]], 1) self.__future_price = self.__y[:, 0, :] # self.__future_price = tf.concat([tf.ones([self.__net.input_num, 1]), # self.__y[:, 0, :]], 1) self.__future_omega = (self.__future_price * self.__net.output) /\ tf.reduce_sum(self.__future_price * self.__net.output, axis=1)[:, None] # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0)) self.__commission_ratio = self.__config["trading"][ "trading_consumption"] self.__pv_vector = tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]) *\ (tf.concat([tf.ones(1), self.__pure_pc()], axis=0)) # rt self.__log_mean_free = tf.reduce_mean( tf.log( tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]))) self.__portfolio_value = tf.reduce_prod(self.__pv_vector) # pf self.__mean = tf.reduce_mean(self.__pv_vector) self.__log_mean = tf.reduce_mean(tf.log(self.__pv_vector)) self.__stock_return = tf.log(self.__pv_vector) # 个股收益率 self.__index_return = tf.log(self.__stock_index) # 指数收益率 self.__tracking_error = tf.sqrt( tf.reduce_mean( (self.__stock_return - self.__index_return)**2)) # 跟踪误差 self.__excess_return = tf.reduce_mean(self.__stock_return - self.__index_return) # 超额收益 self.__objective = 0.3 * self.__tracking_error - ( 1 - 0.3) * self.__excess_return # 0.3为平衡因子 self.__information_ratio = self.__excess_return / self.__tracking_error # 信息比率 # self.__tracking_ratio = tf.divide( # 跟踪比率 # tf.divide(tf.reduce_sum(self.all_market_capitalization_squeeze[:,:,1],axis=1), # tf.reduce_sum(self.all_market_capitalization_squeeze[:,:,0],axis=1)), # tf.divide(tf.reduce_sum(self.__net.output*self.market_capitalization_squeeze[:,:,1],axis=1), # output[batch_size, coin_number] # tf.reduce_sum(self.__net.output*self.market_capitalization_squeeze[:,:,0],axis=1)) # ) self.__test1 = self.__tracking_error self.__test2 = self.__excess_return self.__test = self.__tracking_error self.__standard_deviation = tf.sqrt( tf.reduce_mean(tf.square(self.__pv_vector - self.__mean))) self.__sharp_ratio = (self.__mean - 1) / self.__standard_deviation self.__loss = self.__set_loss_function() self.__train_operation = self.init_train( learning_rate=self.__config["training"]["learning_rate"], decay_steps=self.__config["training"]["decay_steps"], decay_rate=self.__config["training"]["decay_rate"], training_method=self.__config["training"]["training_method"]) self.__saver = tf.train.Saver() self.__net.session.run(tf.global_variables_initializer())
def __init__(self, config, restore_dir=None, device="cpu"): self.__config = config self.__coin_number = config["input"]["coin_number"] self.__net = network.CNN(config["input"]["feature_number"], self.__coin_number, config["input"]["window_size"], device=device) self.__Gamma = config["training"]["gamma"] self.__Alpha = config["training"]["alpha"] self.__global_step = tf.Variable(0, trainable=False) self.__y = tf.placeholder(tf.float32, shape=[ None, self.__config["input"]["feature_number"], self.__coin_number ]) self.__future_price = tf.concat( [tf.ones([self.__net.input_num, 1]), self.__y[:, 0, :]], 1) # rebalance Omega self.__future_omega = (self.__future_price * self.__net.output) /\ tf.reduce_sum(self.__future_price * self.__net.output, axis=1)[:, None] # turnover v_t = self.__future_omega[:self.__net.input_num - 1] # rebalanced v_t1 = self.__net.output[1:self.__net.input_num] self.__turn_over = tf.reduce_mean( tf.reduce_sum(tf.abs(v_t1[:, 1:] - v_t[:, 1:]), axis=1)) # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0)) self.__commission_ratio = self.__config["trading"][ "trading_consumption"] self.__pv_vector = tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]) *\ (tf.concat([tf.ones(1), self.__pure_pc()], axis=0)) self.__log_mean_free = tf.reduce_mean( tf.log( tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]))) self.__portfolio_value = tf.reduce_prod(self.__pv_vector) self.__mean = tf.reduce_mean(self.__pv_vector) self.__log_mean = tf.reduce_mean(tf.log(self.__pv_vector)) self.__log_standard_deviation = tf.sqrt( tf.reduce_mean((tf.log(self.pv_vector) - self.log_mean)**2)) self.__log_sharp_ratio = ( self.__log_mean) / self.__log_standard_deviation self.__standard_deviation = tf.sqrt( tf.reduce_mean((self.__pv_vector - self.__mean)**2)) self.__sharp_ratio = (self.__mean - 1) / self.__standard_deviation self.__loss = self.__set_loss_function() self.__train_operation = self.init_train( learning_rate=self.__config["training"]["learning_rate"], decay_steps=self.__config["training"]["decay_steps"], decay_rate=self.__config["training"]["decay_rate"], training_method=self.__config["training"]["training_method"]) # decay_step 50000, lr 0.00028 # training_method is Adam self.__saver = tf.train.Saver() if restore_dir: self.__saver.restore( self.__net.session, restore_dir) # if restore_dir exists,the reload the model else: self.__net.session.run( tf.global_variables_initializer()) # initialize the model