def get_eval_corps_auto(self, date_maket_cap=None) -> pd.DataFrame: """100개의 주식 종목을 정해진 방법에 의해 가져온다""" if hasattr(self.params, 'invest_start_date' ) == False or self.params.invest_start_date is None: invest_start_date_str = DateUtils.today_str('%Y.%m.%d') else: invest_start_date_str = self.params.invest_start_date invest_start_date = DateUtils.to_date(invest_start_date_str) if hasattr(self.params, 'max_listing_period_years' ) == False or self.params.max_listing_period_years is None: max_listing_period_years = 20 else: max_listing_period_years = self.params.max_listing_period_years max_listing_date = DateUtils.add_years(invest_start_date, -max_listing_period_years) max_listing_date = DateUtils.to_date_str(max_listing_date, '%Y-%m-%d') corps = self.get_corps_all() corps = corps.query("상장일<'{}'".format(max_listing_date)) corps.loc[:, '종목코드'] = corps['종목코드'].astype(str).str.zfill(6) if date_maket_cap is None: date_maket_cap = invest_start_date_str #corps_cap = self.get_corps_maket_cap(date_maket_cap) corps_cap = self.get_now_corps_maket_cap() corps = corps.merge(corps_cap, on='종목코드') corps = corps.sort_values(by=["시가총액"], ascending=False) selected_corps_first = corps[:50] selected_corps_last = corps[len(corps) - 60:-10] return selected_corps_first.append(selected_corps_last, ignore_index=True)
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 get_train_test(self, data, scaler_close=None): """train, test 데이터로 만든다.""" data = data.copy() data = data[(data[['close', 'open', 'high', 'low', 'volume']] != 0).all(1)] data.index = pd.RangeIndex(len(data.index)) #data = self.add_mean_line(data) if self.params.invest_end_date is not None: data = data.query("date<='{}'".format(self.params.invest_end_date)) if self.params.invest_start_date is not None: invest_data = data.query("date>='{}'".format( self.params.invest_start_date)) invest_count = len(invest_data.index) - 1 self.params.invest_count = invest_count invest_start_date_str = self.params.invest_start_date else: invest_count = 0 self.params.invest_count = 0 invest_start_date_str = data.tail(1)['date'].to_string(index=False) invest_start_date = DateUtils.to_date(invest_start_date_str) if hasattr(self.params, 'stock_training_period_years'): period = self.params.stock_training_period_years 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