def train(type='ALL_CORPS', start_no=1): """하나의 세션으로 학습시키는 기본 모델 """ corps = get_corps() params = TrainParams(type) invests = LearningNMockInvestment(params) invests.let_train_invests(corps, start_no)
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_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 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 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 train(type: str = 'EACH', start_no: int = 1, train_model: str = 'rnn', invest_only: bool = False) -> None: """하나의 세션으로 학습시키는 기본 모델 """ corps = get_corps(type) params = GlobalParams(type, train_model=train_model) invests = LearningNMockInvestment(params) invests.train_n_invests(corps, start_no, invest_only)
def main(corp_names: list = None, train_model: str = 'rnn', invest_only: bool = False) -> None: if corp_names is None: corp_names = ["에이치엘비", "동일철강", "키다리스튜디오"] params = GlobalParams('FORCAST', train_model=train_model) invests = LearningNMockInvestment(params) invests.train_n_invests_for_name(corp_names, invest_only)
def main(corp_names=None): if corp_names is None: corp_names = [ "삼성중공업", "기아자동차", "게임빌", "루트로닉", "영진약품", "대아티아이", "한국타이어" ] #corp_names = ["JYP Ent.", "KTH"] params = TrainParams('FORCAST') invests = LearningNMockInvestment(params) invests.let_train_invests_for_name(corp_names)
def train_n_invests_top10(self, corps): """ 상위 10개를 사는 방법으로 모의투자를 실행한다. """ learning_invest = LearningNMockInvestment(self.params) invest_count = self.params.invest_count invest_data = [] for i in range(invest_count): j = 0 for index, corp_data in corps.iterrows(): if i == 0: invest_row = self.train_top10(i, j, corp_data) invest_data.append(invest_row) else: invest_row = invest_data[j] self.train_top10(i, j, corp_data, invest_row) #print(i, j, invest_row) j += 1 self.invest_top10(i, invest_data) print(invest_data) self.sell_all_stock(invest_data) print("final", invest_data) df_invest_data = pd.DataFrame(invest_data, columns=self.RESULT_COLUMNS) DataUtils.save_excel(df_invest_data, learning_invest.get_result_file_path())
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
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 forcasts(corps_n_date: list) -> None: params = GlobalParams('FORCAST') invests = LearningNMockInvestment(params) invests.forcasts(corps_n_date)