def trains(self, corps:pd.DataFrame=None)->(pd.DataFrame, pd.DataFrame):
        if corps is None:
            corp = Corp(self._global_params)
            corps = corp.get_eval_corps_auto()
        no = 1
        invest_date = self._global_params.invest_start_date + "~" + self._global_params.invest_end_date
        results = []
        info_data = []
        for index, corp_data in corps.iterrows():
            corp_code = corp_data['종목코드']
            corp_name = corp_data['회사명']
            invest_value, index_value, infos = self.train(corp_code=corp_code, corp_name=corp_name)
            result = [no, corp_code, corp_name, invest_value, index_value, invest_date]
            results.append(result)
            info_data.append(infos)
            print(result)
            # if no % 10 == 0:
            #     df_results = pd.DataFrame(results, columns=self.RESULT_COLUMNS)
            #     DataUtils.save_excel(df_results, self._get_result_file_path())
            no += 1
        df_results = pd.DataFrame(results, columns=self.RESULT_COLUMNS)
        DataUtils.save_excel(df_results, self._get_result_file_path())
        chart_data = None
        try:
            visualizer = InvestVisualizer(self._global_params)
            chart_data = visualizer.draw_invest_4reinforcement(info_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)

        return df_results, chart_data
def recommend_corps(recommend_month: str, train_model: str = 'rnn') -> None:
    """하나의 세션으로 학습시키는 기본 모델 """

    month = DateUtils.to_date(recommend_month, '%Y.%m')
    params = GlobalParams(train_model=train_model)
    #params.remove_session_file = True
    before_month_start = DateUtils.to_month_str(month,
                                                -params.mock_period_months)
    before_month_end = DateUtils.to_month_str(month, -1)
    params.invest_start_date = before_month_start + '.01'
    params.invest_end_date = DateUtils.to_date_str(month -
                                                   datetime.timedelta(days=1))
    params.result_file_name = "MOCK_" + before_month_start + "-" + before_month_end
    corp = Corp(params)
    corps = corp.get_eval_corps_auto(params.invest_end_date)
    invests = LearningNMockInvestment(params)
    invests.train_n_invests(corps)
    before_result = pd.read_csv(invests.get_result_file_path())

    if params.rmse_max_recommend is not None:
        before_result = before_result.query("rmse<" +
                                            str(params.rmse_max_recommend))
    before_result = before_result.sort_values(by='invest_result',
                                              ascending=False)
    before_result.index = range(len(before_result.index))
    save_file_name = "recommend_months_" + recommend_month + ".xlsx"
    save_file_path = os.path.join('result', train_model, save_file_name)
    DataUtils.save_csv(before_result, save_file_path)
    print(before_result)
示例#3
0
    def train_n_invests_for_name(self,
                                 corp_names: list,
                                 invest_only: bool = False) -> None:
        """회사이름으로 검색하여 학습시킴 """
        corp = Corp()
        comp_rmses = []
        no = 1
        date = None
        for corp_name in corp_names:
            corp_code = corp.get_corp_code(corp_name)
            try:
                result, invest_daily = self.train_n_invest(
                    corp_code, corp_name, no, invest_only)
                date = result[1]
            except Exception:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_exception(exc_type,
                                          exc_value,
                                          exc_traceback,
                                          file=sys.stdout)
                no += 1
                continue

            #result = self.let_train_invest(corp_code, corp_name, no)
            comp_rmses.append(result)
            no += 1
        df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns)
        DataUtils.save_csv(df_comp_rmses, self.get_result_file_path(date))
def train_codes(corp_codes, train_model='rnn'):
    """하나의 세션으로 학습시키는 기본 모델 """
    corp = Corp(type)
    corps = corp.get_corps_for_codes(corp_codes)

    params = GlobalParams(train_model=train_model)
    invests = LearningNMockInvestment(params)
    invests.train_n_invests(corps)
def train_one(corp_name='카카오', train_model='rnn'):
    """하나의 세션으로 학습시키는 기본 모델 """
    corp = Corp(type)
    corps = corp.get_corp(corp_name)

    params = GlobalParams(train_model=train_model)
    invests = LearningNMockInvestment(params)
    invests.train_n_invests(corps)
def top10_model():
    corp = Corp()
    corps = corp.get_eval_corps()

    params = TrainParams()
    params.invest_type = 'top10'
    params.result_file_name = "top10_result"
    invests = LearningNMockTop10(params)
    invests.let_train_invests_top10(corps)
def train_all_corps(type='ALL_CORPS', start_no=1):
    """하나의 세션으로 모든 회사를 학습시킨다.  """
    corp = Corp()
    corps = corp.get_corps()

    params = TrainParams(type)
    params.result_file_name = "training_" + type.lower() + "_result"
    invests = LearningNMockInvestment(params)
    invests.let_train_invests(corps, start_no)
def twins(start_no=1):
    corp = Corp()
    corps = corp.get_eval_corps()

    params = TrainParams("EACH")
    params.result_file_name = 'twins_result'

    invests = LearningNMockInvestment(params)
    invests.let_train_invests_twins(corps, start_no)
def top10_model(train_model='rnn'):
    """상위10개를 가지고 투자하는 모델"""
    corp = Corp()
    corps = corp.get_eval_corps()

    params = GlobalParams(train_model=train_model)
    params.invest_type = 'top10'
    params.result_file_name = "top10_result"
    invests = LearningNMockTop10(params)
    invests.train_n_invests_top10(corps)
示例#10
0
    def train_months(self, start:str='2018.01', end:str='2018.11', invest_money:float=100000000)->None:

        train_model = self._global_params.train_model
        start_month = DateUtils.to_date(start, '%Y.%m')
        end_month = DateUtils.to_date(end, '%Y.%m')
        between = DateUtils.between_months(start_month, end_month)
        invest_months_result = []
        result_columns = ["month", "invest_money", "result_money"]
        MOCK_MONEY = 10000000
        chart_data = []
        for i in range(between + 1):
            # params.remove_session_file = True
            before_month_start = DateUtils.to_month_str(start_month, i - self._global_params.mock_period_months)
            before_month_end = DateUtils.to_month_str(start_month, i - 1)
            self._global_params.invest_start_date = before_month_start + '.01'
            self._global_params.invest_end_date = before_month_end + '.31'
            self._global_params.result_file_name = "MOCK_" + before_month_start + "-" + before_month_end
            self._global_params.invest_money = MOCK_MONEY
            corp = Corp(self._global_params)
            corps = corp.get_eval_corps_auto(self._global_params.invest_end_date)
            self._env.set_params(params=self._global_params)
            before_result, _ = self.trains(corps)
            now_month = DateUtils.to_month_str(start_month, i)
            before_result = corp.exclude_corps(before_result, now_month)
            before_result = before_result.sort_values(by='invest_result', ascending=False)
            before_result.index = range(len(before_result.index))
            corp10_codes = before_result.loc[:9, 'code']
            corp10_codes.index = range(len(corp10_codes.index))
            corp10 = corp.get_corps_for_codes(corp10_codes)
            corp10_len = len(corp10.index)

            self._global_params.invest_start_date = now_month + '.01'
            self._global_params.invest_end_date = now_month + '.31'
            self._global_params.result_file_name = "INVEST_" + now_month
            self._global_params.invest_money = invest_money / corp10_len
            self._env.set_params(params=self._global_params)
            now_result, invest_chart_data = self.trains(corp10)
            chart_data.append(invest_chart_data)
            invest_money = now_result['invest_result'].sum()
            result = [now_month, self._global_params.invest_money * corp10_len, invest_money]
            invest_months_result.append(result)
            print(result)

            df_imr = pd.DataFrame(invest_months_result, columns=result_columns)
            save_file_name = "recommend_months_" + start + "-" + end + ".xlsx"
            if "_" in train_model:
                save_file_path = os.path.join('result', 'reinforcement', train_model, self._global_params.ensemble_type, save_file_name)
            else:
                save_file_path = os.path.join('result', 'reinforcement', train_model, save_file_name)
            DataUtils.save_excel(df_imr, save_file_path)

            if len(chart_data) > 1:
                visualizer = InvestVisualizer(self._global_params)
                visualizer.draw_invest_months(chart_data, start, end)
                print()
示例#11
0
 def get_kospi_kosdaq(self, market):
     invest_start_date = self.params.invest_start_date
     invest_end_date = self.params.invest_end_date
     corp = Corp(self.params)
     if market == 'KOSPI':
         data = corp.get_kospi()
     elif market == 'KOSDAQ':
         data = corp.get_kosdaq()
     else:
         data = pd.DataFrame()
     return data.query("date>='{}' & date<='{}'".format(invest_start_date, invest_end_date))
示例#12
0
 def let_train_invests_for_name(self, corp_names):
     """회사이름으로 검색하여 학습시킴 """
     corp = Corp()
     comp_rmses = []
     no = 1
     for corp_name in corp_names:
         corp_code = corp.get_corp_code(corp_name)
         result = self.let_train_invest(corp_code, corp_name, no)
         comp_rmses.append(result)
         no += 1
     df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns)
     DataUtils.save_excel(df_comp_rmses, self.get_result_file_path())
def get_corps(type='EACH'):
    params = GlobalParams(type)
    corp = Corp(params)
    return corp.get_eval_corps_auto()
示例#14
0
 def train_codes(self, corp_codes, train_model='rnn'):
     """하나의 세션으로 학습시키는 기본 모델 """
     corp = Corp()
     corps = corp.get_corps_for_codes(corp_codes)
     self.trains(corps)
                        verbose_eval=10)

        print('Plotting metrics recorded during training...')
        ax = lgb.plot_metric(evals_result, metric='l1')
        plt.show()

        print('Plotting feature importances...')
        ax = lgb.plot_importance(gbm, max_num_features=10)
        plt.show()

        print('Plotting 84th tree...')  # one tree use categorical feature to split
        ax = lgb.plot_tree(gbm, tree_index=83, figsize=(20, 8), show_info=['split_gain'])
        plt.show()

        print('Plotting 84th tree with graphviz...')
        graph = lgb.create_tree_digraph(gbm, tree_index=83, name='Tree84')
        graph.render(view=True)

if __name__ == '__main__':
    corp_name = "카카오"
    corp = Corp()
    corp_code = corp.get_corp_code(corp_name)
    learning = LearningPlainModels('light_gbm')
    learning.train(corp_code, corp_name, 1)

    # from sklearn.datasets import load_iris
    #
    # iris = load_iris()
    # X = iris.data[:, [2, 3]]
    # y = iris.target
    # print(X, y)
示例#16
0
    def draw_invest_seaborn(self, pridects_data:pd.DataFrame, dir:str, title:str, start:str=None, end:str=None)->None:
        file_path = self.get_file_path(dir, title, 'png', start, end)
        pridects_data = pridects_data.copy()
        pridects_data['date'] = pd.to_datetime(pridects_data['date'], format='%Y.%m.%d')
        pridects_data.set_index('date', inplace=True)

        plt = self.main.get_plt()
        fig, ax = plt.subplots(figsize=self.main.FIG_SIZE)
        sns.lineplot(data=pridects_data).set_title(title)
        ax.xaxis.set_major_locator(mdates.AutoDateLocator())
        ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y.%m.%d'))
        ax.set(xlabel='Date', ylabel='Margin(%)')
        #fig.autofmt_xdate()
        plt.grid(color='k', linestyle='dotted', linewidth=1, alpha=0.4)
        fig.savefig(file_path)
        plt.close()


    def save_csv(self, df_rmses, dir, corp_name, start=None, end=None)->None:
        """차트 테이터를 저장한다."""
        file_path = self.get_file_path(dir, corp_name, "csv", start, end)
        DataUtils.save_csv(df_rmses, file_path)


if __name__ == '__main__':
    params = GlobalParams()
    corp = Corp(params)
    kospi = corp.get_kospi()
    kospi = kospi.query("date>='{}' & date<='{}'".format(params.invest_start_date, params.invest_end_date))
    print(kospi[0:1]['close'].values[0])
def get_corps():
    corp = Corp()
    return corp.get_eval_corps()
def train_months(start: str = '2018.01',
                 end: str = '2018.09',
                 invest_money: float = 100000000,
                 train_model: str = 'rnn') -> None:
    """하나의 세션으로 학습시키는 기본 모델 """
    start_month = DateUtils.to_date(start, '%Y.%m')
    end_month = DateUtils.to_date(end, '%Y.%m')
    between = DateUtils.between_months(start_month, end_month)
    invest_months_result = []
    result_columns = ["month", "invest_money", "result_money"]
    MOCK_MONEY = 10000000
    chart_data = []
    params = None
    index_money = None
    for i in range(between + 1):

        params = GlobalParams(train_model=train_model)
        #params.remove_session_file = True
        before_month_start = DateUtils.to_month_str(
            start_month, i - params.mock_period_months)
        before_month_end = DateUtils.to_month_str(start_month, i - 1)
        params.invest_start_date = before_month_start + '.01'
        params.invest_end_date = before_month_end + '.31'
        params.result_file_name = "MOCK_" + before_month_start + "-" + before_month_end
        params.invest_money = MOCK_MONEY
        corp = Corp(params)
        corps = corp.get_eval_corps_auto(params.invest_end_date)
        invests = LearningNMockInvestment(params)
        invests.train_n_invests(corps)
        before_result = pd.read_csv(invests.get_result_file_path())

        now_month = DateUtils.to_month_str(start_month, i)
        if params.rmse_max_recommend is not None:
            before_result = before_result.query("rmse<" +
                                                str(params.rmse_max_recommend))
        before_result = corp.exclude_corps(before_result, now_month)
        before_result = before_result.sort_values(by='invest_result',
                                                  ascending=False)
        before_result.index = range(len(before_result.index))
        corp10_codes = before_result.loc[:9, 'code']
        corp10_codes.index = range(len(corp10_codes.index))
        corp10 = corp.get_corps_for_codes(corp10_codes)
        corp10_len = len(corp10_codes.index)

        params = GlobalParams(train_model=train_model)
        #params.remove_session_file = False

        params.invest_start_date = now_month + '.01'
        params.invest_end_date = now_month + '.31'
        params.result_file_name = "INVEST_" + now_month
        params.invest_money = invest_money / corp10_len
        if index_money is not None:
            params.index_money = index_money / corp10_len
        invests = LearningNMockInvestment(params)
        invest_chart_data = invests.train_n_invests(corp10, invest_only=False)
        chart_data.append(invest_chart_data)
        now_result = pd.read_csv(invests.get_result_file_path())
        invest_money = now_result['invest_result'].sum()
        index_money = now_result['all_invest_result'].sum()
        invest_months_result.append(
            [now_month, params.invest_money * corp10_len, invest_money])
        print(now_month, params.invest_money * corp10_len, invest_money)

    df_imr = pd.DataFrame(invest_months_result, columns=result_columns)
    save_file_name = "recommend_months_" + start + "-" + end + ".xlsx"
    if "_" in train_model:
        save_file_path = os.path.join('result', train_model,
                                      params.ensemble_type, save_file_name)
    else:
        save_file_path = os.path.join('result', train_model, save_file_name)
    DataUtils.save_csv(df_imr, save_file_path)

    if len(chart_data) > 1 and params is not None:
        visualizer = InvestVisualizer(params)
        visualizer.draw_invest_months(chart_data, start, end)
        print()
 def get_corps(self):
     corp = Corp()
     return corp.get_corps3()