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)
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()
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()