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_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_n_invests(self, corps, start_no=1, invest_only: bool = False) -> pd.DataFrame: """입력한 회사들에 대해서 학습시키고 모의투자를 실행한다.""" comp_rmses = [] no = 1 invest_daily_data = [] date: str = None for index, corp_data in corps.iterrows(): if no < start_no: no += 1 continue corp_code = corp_data['종목코드'] corp_name = corp_data['회사명'] try: result, invest_daily = self.train_n_invest( corp_code, corp_name, no, invest_only) date = result[7] if invest_daily is not None: invest_daily_data.append(invest_daily) 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 comp_rmses.append(result) if no % 10 == 0 and self.params.debug == True: df_comp_rmses = pd.DataFrame(comp_rmses, columns=self.result_columns) DataUtils.save_csv(df_comp_rmses, self.get_result_file_path(date)) 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)) #DataUtils.save_csv(df_comp_rmses, self.get_result_file_path(date)) 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)
def get_now_corps_maket_cap(self): date = DateUtils.today_str('%Y%m%d') # 오늘 날짜 year = date[0:4] file_path = os.path.join(self.DIR, "files", "corps_market_cap", year, "market_cap_" + date + ".txt") if not os.path.isfile(file_path): master_data = self.get_now_coprs_master_price_from_krx() market_cap = master_data[['종목코드', '자본금(원)']] market_cap.rename(columns={'자본금(원)': '시가총액'}, inplace=True) DataUtils.save_csv(market_cap, file_path) else: market_cap = pd.read_csv(file_path) market_cap.loc[:, '종목코드'] = market_cap['종목코드'].astype(str).str.zfill(6) return market_cap
def get_corps_maket_cap(self, date=None): if date == None: date = DateUtils.today_str() # 오늘 날짜 else: date = date.replace(".", "").replace("-", "") for i in range(30): try: year = date[0:4] file_path = os.path.join(self.DIR, "files", "corps_market_cap", year, "market_cap_" + date + ".txt") if not os.path.isfile(file_path): master_data = self.get_coprs_master_price_from_krx(date) market_cap = master_data[['종목코드', '시가총액']] DataUtils.save_csv(market_cap, file_path) else: market_cap = pd.read_csv(file_path) break except: print(date, "에 시가총액 데이터를 가져오는데 에러 발생하여 이전 데이터 사용") date = DateUtils.add_days(date, -i, '%Y%m%d') return market_cap
def forcasts(self, corps_n_date: list) -> None: """ 각 종목의 날짜로 예측을 수행한다. """ comp_rmses = [] no = 1 date = None for corp_n_date in corps_n_date: corp_code = corp_n_date[0].replace("A", "") corp_name = corp_n_date[1] forcast_date = corp_n_date[2] params = GlobalParams('FORCAST') params.forcast_date = forcast_date invests = LearningNMockInvestment(params) result, invest_daily = invests.train_n_invest( corp_code, corp_name, no, False) comp_rmses.append(result) date = result[1] 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_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 save_csv(self, df_rmses, dir, corp_name): """Seaborn 차트로 그래프를 그린다.""" file_path = self.get_file_path(dir, corp_name, "csv") DataUtils.save_csv(df_rmses, file_path)
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)
def save_corps_csv(self, file_path): """ 주식회사 정보를 가져와서 csv로 저장한다. """ corps = self.get_corps_master() corps = corps[['종목코드', '회사명', '상장일']] DataUtils.save_csv(corps, file_path)
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)