def train(self, corp_code, corp_name, no):
        """입력한 회사에 대해서 학습시키고 모의투자를 실행한다."""

        stocks = Stocks(self.params)
        trains_data = TrainsData(self.params)
        stock_data = stocks.get_stock_data(corp_code)
        dts = trains_data.get_train_test_plain(stock_data)

        # if self.type == 'light_gbm':
        #     self.train_light_gbm(dts)
        # else:
        clf = self.get_model()
        clf.fit(dts.trainX, dts.trainY)
        if len(dts.testX) > 0:
            test_pred = clf.predict(dts.testX)
            rmse = metrics.mean_squared_error(dts.testY, test_pred)
        else:
            rmse = -1

        invest = MockInvestment(self.params)
        invest_money, all_invest_money = invest.invest_plain(dts.investX, clf)
        print(no, corp_code, corp_name, invest_money, all_invest_money, rmse)

        visualizer = PlainVisualizer(self.params)
        visualizer.draw_chart(clf, corp_name, dts)
        return [no, corp_code, corp_name, invest_money, all_invest_money, rmse]
示例#2
0
    def let_train_invest_twins(self, corp_code, corp_name, no):
        """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다."""
        stocks = Stocks(self.params)
        trains_data = TrainsData(self.params)
        learning = Learning(self.params)
        params_all = TrainParams('ALL_CORPS')
        learning_all = Learning(params_all)
        invest = MockInvestment(self.params)

        stock_data = stocks.get_stock_data(corp_code)
        data_params, scaler_close, dataX_last = trains_data.get_train_test(
            stock_data)
        rmse_val, train_cnt, rmse_vals, test_predict = learning.let_learning(
            corp_code, data_params)
        rmse_val_all, train_cnt_all, rmse_vals_all, test_predict_all = learning_all.let_learning(
            corp_code, data_params)
        last_money, last_predict, invest_predicts, all_invest_money = \
            invest.let_invest_and_all(corp_code, dataX_last, data_params, params_all)
        rmse_val = np.mean([rmse_val, rmse_val_all])
        train_cnt = train_cnt + train_cnt_all

        print(no, corp_code, corp_name, rmse_val, last_money, all_invest_money,
              train_cnt)
        return [
            no, corp_code, corp_name, rmse_val, last_money, all_invest_money,
            train_cnt
        ]
示例#3
0
    def train_n_invest(self,
                       corp_code: str,
                       corp_name: str,
                       no: int,
                       invest_only: bool = False) -> (list, list):
        """입력한 회사에 대해서 학습시키고 모의투자를 실행한다."""

        stocks = Stocks(self.params)
        stock_data = stocks.get_stock_data(corp_code)

        ip = InvestParams()
        if self.params.invest_start_date is None:
            tps = self.trains(corp_code, corp_name, stock_data, invest_only)
            ip.last_money = self.params.invest_money
            ip.index_money = ip.last_money
            if self.params.result_type == 'forcast':
                invest = MockInvestment(self.params)
                ip = invest.invest(corp_code, corp_name, tps, invest_only)
        else:
            invest = MockInvestment(self.params)
            tps = self.trains(corp_code, corp_name, stock_data, invest_only)
            ip = invest.invest(corp_code, corp_name, tps, invest_only)

        if invest_only:
            tp = self.get_train_params_4rmse(tps)
            test_predict = None
        else:
            tp = self.select_train_params(tps)
            test_predict = tp.test_predict
        if ip.test_predict is not None:
            test_predict = ip.test_predict

        try:
            if self.params.save_train_graph and (test_predict is not None
                                                 or ip.predict_list
                                                 is not None) == True:
                visual = LearningVisualizer(self.params)
                visual.draw_predictions(corp_name, tp.scaler_close,
                                        tp.data_params, test_predict,
                                        ip.predict_list)
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            traceback.print_exception(exc_type,
                                      exc_value,
                                      exc_traceback,
                                      file=sys.stdout)

        return self.get_train_invest_result(no, corp_code, corp_name,
                                            stock_data, tp, ip), ip.daily_data
示例#4
0
    def let_train_invest(self, corp_code, corp_name, no):
        """입력한 회사에 대해서 학습시키고 모의투자를 실행한다."""

        stocks = Stocks(self.params)
        stock_data = stocks.get_stock_data(corp_code)

        invest_count = self.params.invest_count

        if invest_count == 0:
            rmse_val, train_cnt, data_params, dataX_last, scaler_close = self.let_train_only(
                corp_code, stock_data)
            last_money = self.params.invest_money
            all_invest_money = last_money
        else:
            if self.params.is_all_corps_model == False and self.params.remove_session_file == True:
                learning = Learning(self.params)
                learning.delete_learning_image(corp_code)

            invest = MockInvestment(self.params)
            rmse_val, train_cnt, data_params, dataX_last, scaler_close = self.let_train_only(
                corp_code, stock_data)
            last_money, last_predict, invest_predicts, all_invest_money = invest.let_invest(
                corp_code, dataX_last, data_params)

        if self.params.result_type == 'forcast':
            invest = MockInvestment(self.params)
            last_money, last_predict, invest_predicts, all_invest_money = \
                invest.let_invest(corp_code, dataX_last, data_params)
            last_date = stock_data.tail(1)['date'].to_string(index=False)
            last_close_money, last_pred_money = invest.get_real_money(
                data_params, scaler_close, last_predict)
            last_pred_ratio = (last_pred_money -
                               last_close_money) / last_close_money * 100
            last_pred_ratio = "{:.2f}".format(last_pred_ratio) + "%"
            print(no, last_date, corp_code, corp_name, rmse_val, train_cnt,
                  last_close_money, last_pred_money, last_pred_ratio)
            return [
                no, last_date, corp_code, corp_name, rmse_val, train_cnt,
                last_close_money, last_pred_money, last_pred_ratio
            ]
        else:
            print(no, corp_code, corp_name, rmse_val, last_money,
                  all_invest_money, train_cnt)
            return [
                no, corp_code, corp_name, rmse_val, last_money,
                all_invest_money, train_cnt
            ]
示例#5
0
    def train_n_invest_twins(self, corp_code, corp_name, no) -> list:
        """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다."""
        stocks = Stocks(self.params)
        trains_data = TrainsData(self.params)
        learning = Learning(self.params)
        params_all = GlobalParams('ALL_CORPS')
        learning_all = Learning(params_all)
        invest = MockInvestment(self.params)

        stock_data = stocks.get_stock_data(corp_code)
        data_params, scaler_close, dataX_last = trains_data.get_train_test(
            stock_data)
        tp = learning.learn(corp_code, corp_name, data_params)
        tp_all = learning_all.learn(corp_code, corp_name, data_params)
        ip = invest.invest_n_all(corp_code, dataX_last, data_params,
                                 params_all, scaler_close)
        tp.test_rmse = np.mean([tp.test_rmse, tp_all.test_rmse])
        tp.train_count = tp.train_count + tp_all.train_count
        return self.get_train_invest_result(no, corp_code, corp_name,
                                            stock_data, tp, ip)
示例#6
0
    def train_top10(self, i, j, corp_data, invest_row=None):
        """ top10 모의투자 방법을 위하여 학습을 시킨다."""
        learning_invest = LearningNMockInvestment(self.params)
        invest = MockInvestment(self.params)

        invest_count = self.params.invest_count
        if invest_row is None:
            corp_code = corp_data['종목코드']
        else:
            corp_code = invest_row[1]
        stocks = Stocks(self.params)
        stock_data = stocks.get_stock_data(corp_code)
        stock_data_now = stock_data[:i - invest_count]
        rmse_val, train_cnt, data_params, dataX_last, scaler_close = learning_invest.train(
            corp_code, stock_data_now)

        last_money, last_predict, invest_predicts, all_invest_money = invest.invest(
            corp_code, dataX_last, data_params)
        last_close_money, last_pred_money = invest.get_real_money(
            data_params, scaler_close, last_predict)
        last_pred_ratio = (last_pred_money -
                           last_close_money) / last_close_money * 100

        if invest_row is None:
            corp_name = corp_data['회사명']
            all_invest_money, all_stock_count = invest.buy_stock(
                self.params.invest_money, last_close_money, 0)
            invest_row = [
                j, corp_code, corp_name, last_pred_ratio, last_close_money, 0,
                0, all_invest_money, all_stock_count, rmse_val
            ]
        else:
            #print(invest_row)
            invest_row[3] = last_pred_ratio
            invest_row[4] = last_close_money
        return invest_row
示例#7
0
    def train_n_invests_for_all(self, corps):
        """입력한 회사들에 대해서 학습시키고 모의투자를 실행한다."""
        stocks = Stocks(self.params)
        trains_data = TrainsData(self.params)
        learning = Learning(self.params)
        invest = MockInvestment(self.params)

        list = []
        for index, corp_data in corps.iterrows():
            corp_code = corp_data['종목코드']
            corp_name = corp_data['회사명']
            try:
                stock_data = stocks.get_stock_data(corp_code)
                data_params, scaler_close, dataX_last = trains_data.get_train_test(
                    stock_data)
            except Exception:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type,
                                          exc_value,
                                          exc_traceback,
                                          file=sys.stdout)
                continue
            list.append({
                'corp_code': corp_code,
                'corp_name': corp_name,
                'scaler_close': scaler_close,
                'dataX_last': dataX_last,
                'data_params': data_params,
                'stock_data': stock_data
            })

        all_data_params = self.gather_data_params(list)
        tp = learning.learn("ALL_CORPS", "ALL_CORPS", all_data_params)

        invest_daily_data = []
        comp_rmses = []
        index = 1
        for item in list:
            corp_code = item['corp_code']
            corp_name = item['corp_data']
            dataX_last = item['dataX_last']
            data_params = item['data_params']
            scaler_close = item['scaler_close']
            stock_data = item['stock_data']
            ip = invest.invest(corp_code, corp_name, tp)

            visualizer = LearningVisualizer(self.params)
            visualizer.draw_predictions(corp_name, scaler_close, data_params,
                                        tp.test_predict, ip.predict_list)

            result, invest_daily = self.get_train_invest_result(
                index, corp_code, corp_name, tp.test_rmse, ip.last_money,
                ip.index_money, tp.train_count, stock_data, data_params,
                scaler_close, ip.last_predict)
            if invest_daily != None:
                invest_daily_data.append(invest_daily)
            comp_rmses.append(result)
            index += 1

        df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns)
        DataUtils.save_csv(df_comp_rmses, self.get_result_file_path())

        if len(invest_daily_data) > 1:
            try:
                visualizer = InvestVisualizer(self.params)
                return visualizer.draw_invest_daily(invest_daily_data, corps)
            except Exception:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type,
                                          exc_value,
                                          exc_traceback,
                                          file=sys.stdout)
            stock_start_date = DateUtils.add_years(invest_start_date, -period)
            stock_start_date = stock_start_date.strftime("%Y.%m.%d")
            data = data.query("date>='{}'".format(stock_start_date))

        test_count = None
        if hasattr(self.params, 'stock_test_period_years'
                   ) and self.params.stock_test_period_years is not None:
            period = self.params.stock_test_period_years
            test_start_date = DateUtils.add_years(invest_start_date, -period)
            test_start_date = DateUtils.to_date_str(test_start_date)
            test_data = data.query("date>='{}'".format(test_start_date))
            test_count = len(test_data.index) - invest_count

        scaled_data, scaler_close = self.get_scaled_data(data, scaler_close)
        dataX, dataY, dataX_last, y_date = self.get_dataXY(scaled_data)
        data_params = self.split_train_test(dataX, dataY, invest_count,
                                            test_count, y_date)
        return data_params, scaler_close, dataX_last


if __name__ == '__main__':
    params = GlobalParams()
    stock = Stocks(params)
    stock_data = stock.get_stock_data(35720)
    trains_data = TrainsData(params)
    data = trains_data.add_mean_line(stock_data)
    scaled_data, scaler_close = trains_data.get_scaled_data(data)
    print(scaled_data.tail())
    # stock_start_date = DateUtils.to_date('2018.01.01') - relativedelta(years=22)
    # stock_start_date = stock_start_date.strftime("%Y.%m.%d")
    # print(stock_start_date)