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
Beispiel #2
0
    def let_train_invests(self, corps, start_no=1):
        """입력한 회사들에 대해서 학습시키고 모의투자를 실행한다."""

        if self.params.is_all_corps_model == True and self.params.remove_session_file == True:
            learning = Learning(self.params)
            learning.delete_learning_image()

        comp_rmses = []
        no = 1
        for index, corp_data in corps.iterrows():
            if no < start_no:
                no += 1
                continue
            corp_code = corp_data['종목코드']
            corp_name = corp_data['회사명']
            try:
                result = self.let_train_invest(corp_code, corp_name, no)
            except Exception as inst:
                print(inst)
                no += 1
                continue

            comp_rmses.append(result)
            if no % 10 == 0:
                df_comp_rmses = pd.DataFrame(comp_rmses,
                                             columns=self.result_columns)
                DataUtils.save_excel(df_comp_rmses,
                                     self.get_result_file_path())
            no += 1
    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()
Beispiel #4
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 forcasts(self, corps_n_date:list):
        results = []
        no = 1
        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]

            result = self.forcast(corp_name=corp_name, corp_code=corp_code, forcast_date=forcast_date)
            result.insert(0, no)
            print(result)
            results.append(result)
            no += 1
        df_comp_rmses = pd.DataFrame(results, columns=self.FORCAST_COLUMNS)
        DataUtils.save_excel(df_comp_rmses, self._get_result_file_path())
    def trains(self, corps):
        """ 선형회귀 모형 학습  """
        comp_rmses = []
        no = 1
        for index, corp_data in corps.iterrows():
            corp_code = corp_data['종목코드']
            corp_name = corp_data['회사명']
            try :
                result = self.train(corp_code, corp_name, no)
            except Exception as inst:
                print(inst)
                no += 1
                continue

            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())
Beispiel #7
0
 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 k_means(self):
        corps = self.get_corps()[[
            "회사명", "시가총액\n(억)", "자사주\n(만주)", "자사주\n비중\n(%)", "18년\nOPM\n(%)",
            "5년평균\nPBR", "5년평균\nPER", "부채\n(억)", "자본\n(억)", "총자산\n(억)",
            "영업이익\n(억)", "6개월\n외인(%)", "6개월\n기관(%)"
        ]]
        corps = corps.dropna()
        corps.set_index("회사명", inplace=True)

        kmeans = KMeans(n_clusters=10)
        # Fitting the input data
        kmeans.fit(corps)
        clust_labels = kmeans.predict(corps)
        #print(corps.index[clust_labels])
        # Centroid values
        #centroids = kmeans.cluster_centers_

        corps.insert(0, 'kmeans', clust_labels)
        result = corps[['kmeans']]
        print(result)
        result.insert(0, '회사명', corps.index)
        file_path = os.path.join(self.DIR_RESULT, 'K-Means.xlsx')
        DataUtils.save_excel(result, file_path)
Beispiel #9
0
    def let_train_invests_twins(self, corps, start_no=1):
        """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다."""
        comp_rmses = []
        no = 1
        for index, corp_data in corps.iterrows():
            if no < start_no:
                no += 1
                continue
            corp_code = corp_data['종목코드']
            corp_name = corp_data['회사명']
            try:
                result = self.let_train_invest_twins(corp_code, corp_name, no)
            except Exception as inst:
                print(inst)
                no += 1
                continue

            comp_rmses.append(result)
            if no % 10 == 0:
                df_comp_rmses = pd.DataFrame(comp_rmses,
                                             columns=self.result_columns)
                DataUtils.save_excel(df_comp_rmses,
                                     self.get_result_file_path())
            no += 1
Beispiel #10
0
 def _save_corps(self):
     """ 주식회사 정보를 가져와서 엑셀로 저장한다. """
     url = 'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13'
     code_df = pd.read_html(url, header=0)[0]
     DataUtils.save_excel(code_df, self._FILE_PATH)
Beispiel #11
0
 def save_corps(self):
     """ 주식회사 정보를 가져와서 엑셀로 저장한다. """
     code_df = pd.read_html(self.URL_CORPS_KRX, header=0)[0]
     DataUtils.save_excel(code_df, self.CORPS_FILE_PATH)