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 get_corps_all(self): today = DateUtils.today_str('%Y.%m.%d') year = today[0:4] file_path = os.path.join(self.DIR, 'files', 'corps', year, 'corps_' + today + '.txt') if not os.path.isfile(file_path): self.save_corps_csv(file_path) return pd.read_csv(file_path)
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 get_coprs_master_price_from_krx(self, date=None): if date is None: date = DateUtils.today_str('%Y%m%d') else: date = date.replace(".", "").replace("-", "") df = None for i in range(30): date = DateUtils.add_days(date, -i, '%Y%m%d') # STEP 01: Generate OTP gen_otp_url = 'https://marketdata.krx.co.kr/contents/COM/GenerateOTP.jspx' gen_otp_data = { 'name': 'fileDown', 'filetype': 'csv', 'url': 'MKD/04/0404/04040200/mkd04060200_01', 'market_gubun': 'ALL', # 시장구분: ALL=전체 'indx_ind_cd': '', 'sect_tp_cd': '', 'schdate': date, 'pagePath': '/contents/MKD/04/0404/04040200/MKD04040200.jsp', } r = requests.post(gen_otp_url, gen_otp_data, headers=self.KTX_HEADER) code = r.content # 리턴받은 값을 아래 요청의 입력으로 사용. #STEP 02: download down_url = 'http://file.krx.co.kr/download.jspx' down_data = {'code': code} r = requests.post(down_url, down_data, headers=self.KTX_HEADER) df = pd.read_csv(BytesIO(r.content), header=0, thousands=',') if len(df) > 0: break return df