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 top10_model(train_model='rnn'):
    """상위10개를 가지고 투자하는 모델"""
    corp = Corp()
    corps = corp.get_eval_corps()

    params = GlobalParams(train_model=train_model)
    params.invest_type = 'top10'
    params.result_file_name = "top10_result"
    invests = LearningNMockTop10(params)
    invests.train_n_invests_top10(corps)
def train_all_corps(type='ALL_CORPS', start_no=1):
    """하나의 세션으로 모든 회사를 학습시킨다.  """
    corp = Corp(type)
    corps = corp.get_corps()

    params = GlobalParams(type)
    params.result_file_name = "training_" + type.lower() + "_result"
    invests = LearningNMockInvestment(params)
    #invests.train_n_invests(corps, start_no)
    invests.train_n_invests_for_all(corps)
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 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)
예제 #7
0
    def test_parameters(self):
        expected_parameters = ('base_dir',)
        parameters = GlobalParams.get_param_names()

        all_parameters_present = all(
            map(lambda param: param in parameters, expected_parameters)
        )
        equal_length = len(expected_parameters) == len(parameters)

        assert equal_length and all_parameters_present
예제 #8
0
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)
예제 #9
0
    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))
예제 #10
0
    def train_n_invest_twins(self, corp_code, corp_name, no) -> list:
        """겨별 세션과 통합세션에서 예측한 값의 평균을 예측값으로 한다."""
        stocks = Stocks(self.params)
        trains_data = TrainsData(self.params)
        learning = Learning(self.params)
        params_all = GlobalParams('ALL_CORPS')
        learning_all = Learning(params_all)
        invest = MockInvestment(self.params)

        stock_data = stocks.get_stock_data(corp_code)
        data_params, scaler_close, dataX_last = trains_data.get_train_test(
            stock_data)
        tp = learning.learn(corp_code, corp_name, data_params)
        tp_all = learning_all.learn(corp_code, corp_name, data_params)
        ip = invest.invest_n_all(corp_code, dataX_last, data_params,
                                 params_all, scaler_close)
        tp.test_rmse = np.mean([tp.test_rmse, tp_all.test_rmse])
        tp.train_count = tp.train_count + tp_all.train_count
        return self.get_train_invest_result(no, corp_code, corp_name,
                                            stock_data, tp, ip)
예제 #11
0
 def output(self):
     path = entry_id_2_output_path(self.entry_id, GlobalParams().base_dir)
     return luigi.LocalTarget(path)
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 get_corps(type='EACH'):
    params = GlobalParams(type)
    corp = Corp(params)
    return corp.get_eval_corps_auto()
예제 #14
0
def date_2_output_path (date: datetime.datetime) -> str:
    return path.join(GlobalParams().base_dir,
                     'diaries', 
                     f"boe_diary_processed_{date.strftime('%d_%m_%Y')}.json")
예제 #15
0
                self.params,
                'remove_stock_days') and self.params.remove_stock_days > 0:
            stock_data = stock_data[:-self.params.remove_stock_days]
        return stock_data

    def update_stocks_data(self):
        files = glob.glob(self.DIR_STOCKS + "/*.txt")
        for file_path in files:
            file_name = os.path.basename(file_path)
            stock_data = pd.read_csv(file_path)
            stock_data = stock_data.dropna()
            stock_data = stock_data[:-1]
            date_last = stock_data.tail(1)['date'].to_string(index=False)
            date_next = DateUtils.to_date(date_last) + datetime.timedelta(
                days=1)
            date_next = date_next.strftime("%Y.%m.%d")
            comp_code = file_name.replace(".txt", "")
            new_data = self.get_stock_web_data(comp_code, date_next)
            if len(new_data) > 0:
                stock_data = stock_data.append(new_data, ignore_index=True)
                stock_data = stock_data.dropna()
                stock_data.to_csv(file_path, index=False)


if __name__ == '__main__':
    params = GlobalParams()
    stocks = Stocks(params)
    #stocks.update_stocks_data()
    df = stocks.get_stock_daum_data('005930')
    #df = stocks.get_stock_data('005930')
    print(len(df))
예제 #16
0
def forcasts(corps_n_date: list) -> None:
    params = GlobalParams('FORCAST')
    invests = LearningNMockInvestment(params)
    invests.forcasts(corps_n_date)