Ejemplo n.º 1
0
    def eval_and_plot(self):

        x, label = self.env.get_stock_test_data()

        y = self.predict(x)

        with open(self.save_path + '_y.json', mode='w') as fp:
            json.dump(y.tolist(), fp, indent=True)

        with open(self.save_path + '_label.json', mode='w') as fp:
            json.dump(label.tolist(), fp, indent=True)

        data_ploter.plot_stock_series(self.env.codes, y, label, self.save_path)
Ejemplo n.º 2
0
    def eval_and_plot(self):

        x, label = self.env.get_test_data()  # x的形状为(561,5,25), label的形状为(561,5) ; 分别返回整个测试集的所有输入序列 和 整个测试集的输出序列,对应着每个输入序列后一天的close

        y = self.predict(x) #形状为(561,5) , 0.2*总数据=561

        with open(self.save_path + '_y.json', mode='w') as fp:
            json.dump(y.tolist(), fp, indent=True) #保存为model_y.json 在checkpoints\SL\stock\model_y.json 保存着预测值y

        with open(self.save_path + '_label.json', mode='w') as fp:
            json.dump(label.tolist(), fp, indent=True) # #保存为model_label.json 在checkpoints\SL\stock\model_label.json 保存着测试集的真实的标签值

        data_ploter.plot_stock_series(self.env.codes, # env.codes == <class 'list'>: ['600036', '601328', '601998', '601398']
                                      y, # 形状为(561,5)
                                      label, # label的形状为(561,5)
                                      self.save_path) # 保存着和单独运行plot_prices.py一样的结果的图片
Ejemplo n.º 3
0
    def eval_and_plot(self):
        def _to_price(list):
            ### return to price
            func = np.vectorize(lambda x: x / 100 + 1)
            ### return to daily change
            list = func(list)
            label1 = [
                reduce(lambda x, y: x * y, list[:i + 1]).tolist()
                for i in range(len(list))
            ]
            price_list = np.array([[i[0] * first_close] for i in label1])
            return price_list

        x, label = self.env.get_test_data()
        # price = self.env.original_frame
        # add new_code, scale the label and predict back
        first_index = self.env.e_data_indices[0] - 1
        first_close = self.env.origin_frames['600036']['close'].iloc[
            first_index]

        # mean = self.env.scaler[0].mean_[-1]
        # std = math.sqrt(self.env.scaler[0].var_[-1])
        label_price = _to_price(label)

        y = self.predict(x)
        y_price = _to_price(y)
        with open(self.save_path + '_label_price.json', mode='w') as fp:
            json.dump(label_price.tolist(), fp, indent=True)

        with open(self.save_path + '_label_daily_change.json', mode='w') as fp:
            json.dump(label.tolist(), fp, indent=True)

        with open(self.save_path + '_y_price.json', mode='w') as fp:
            json.dump(y_price.tolist(), fp, indent=True)

        with open(self.save_path + '_y_daily_change.json', mode='w') as fp:
            json.dump(y.tolist(), fp, indent=True)
        data_ploter.plot_stock_series(self.env.codes, y_price, label_price,
                                      self.save_path + '_price')
        data_ploter.plot_stock_series(self.env.codes, y, label,
                                      self.save_path + '_daily_change')
Ejemplo n.º 4
0
    def eval_and_plot_backtest(self, code, model_name):
        x, label = self.env.get_test_data()
        y = self.predict(x)
        with open(self.save_path + '_y.json', mode='w') as fp:
            json.dump(y.tolist(), fp, indent=True)

        with open(self.save_path + '_label.json', mode='w') as fp:
            json.dump(label.tolist(), fp, indent=True)
        data_ploter.plot_stock_series(self.env.codes, y, label, self.save_path)
        # reformat the data and transformed the data to back testing data
        date_index = self.env.dates[self.env.e_data_indices[0] +
                                    self.env.seq_length + 1:]
        dataframe_backtest = pd.DataFrame(
            {
                'label': label.flatten(),
                'y': y.flatten()
            }, index=date_index)
        original_frames = self.env.origin_frames[
            self.env.codes[0]][self.env.e_data_indices[0] +
                               self.env.seq_length:]
        dataframe_backtest = pd.concat([dataframe_backtest, original_frames],
                                       axis=1)
        dataframe_backtest['date'] = dataframe_backtest.index
        dataframe_backtest['date'] = dataframe_backtest['date'].apply(
            lambda x: pd.to_datetime(x).strftime("%Y-%m-%d %H:%M:%S"))
        dataframe_backtest.dropna(how="any", inplace=True)
        dataframe_backtest.set_index(['date'], inplace=True)
        dataframe_backtest['openinterest'] = 0
        col_order = [
            'open', 'high', 'low', 'close', 'volume', 'openinterest', 'label',
            'y'
        ]
        dataframe_backtest = dataframe_backtest[col_order]
        # dataframe_backtest = dataframe_backtest.round(2)
        dataframe_backtest.to_csv(
            "../../back_testing/data/{}_{}_for_backtest.csv".format(
                model_name, code),
            date_format='%Y-%m-%d %H:%M:%S')
        print('dataframe is updated')
Ejemplo n.º 5
0
    def eval_and_plot(self):
        x, label = self.env.get_test_data()
        y = self.predict(x)
        with open(self.save_path + '_y.json', mode='w') as fp:
            json.dump(y.tolist(), fp, indent=True)

        with open(self.save_path + '_label.json', mode='w') as fp:
            json.dump(label.tolist(), fp, indent=True)
        data_ploter.plot_stock_series(self.env.codes, y, label, self.save_path)
        date_index = self.env.dates[self.env.e_data_indices[0] +
                                    self.env.seq_length + 1:]
        dataframe_backtest = pd.DataFrame(
            {
                'label': label.flatten(),
                'y': y.flatten()
            }, index=date_index)
        original_frames = self.env.origin_frames[
            self.env.codes[0]][self.env.e_data_indices[0] +
                               self.env.seq_length:]
        dataframe_backtest = pd.concat([dataframe_backtest, original_frames],
                                       axis=1)
        return dataframe_backtest
Ejemplo n.º 6
0
 def eval_and_plot(self):
     x, label = self.env.get_stock_test_data()
     y = self.predict(x)
     data_ploter.plot_stock_series(self.env.codes, y, label, self.save_path)