def load_required_df(): """ beta 업데이트에 필요한 df 추출 """ # 가장 최근 업데이트 날짜 추출 beta_latest_date = exec_query(f'select max(date) from stock_db.d_beta') if len(beta_latest_date) != 0: beta_latest_date = beta_latest_date[0][0] else: beta_latest_date = '20000101' # 가장 최근으로부터 2년 전 시점부터 데이터 추출 from_date = str( datetime.strptime(beta_latest_date, '%Y%m%d').date() - timedelta(days=365 * 2 + 30)).replace("-", "") # 필요한 기간의 데이터 추출 price = pd.DataFrame( exec_query( f'select `stock_cd`, `date`, `price` from stock_db.d_stock_price where date > {from_date}' )) index = pd.DataFrame( exec_query( f'select* from stock_db.d_kospi_kosdaq where date > {from_date}')) market_info = pd.DataFrame( exec_query( f'select `date`, `stock_cd`, `market` from stock_db.stock_market_sector where date > {from_date}' )) price.columns = ['stock_cd', 'date', 'price'] index.columns = ['date', 'KOSPI', 'KOSDAQ'] market_info.columns = ['date', 'stock_cd', 'market'] # market_info의 date는 int로 되어있어서 chr로 변환 필요 market_info['date'] = market_info['date'].astype('str') return price, index, market_info
def update_beta_table(): """ 서버db 업데이트(beta table의 가장 최근 시점 이후의 beta만을 기존 db에 append) """ beta_latest_date = exec_query( f'select max(date) from stock_db.d_beta')[0][0] price, index, market_info = load_required_df() if ((max(price.date) == max(index.date) == max(market_info.date)) != True): max_date = min(max(price.date), max(index.date), max(market_info.date)) price = price.loc[price.date <= max_date, :] index = index.loc[index.date <= max_date, :] market_info = market_info.loc[market_info.date <= max_date, :] updated_beta_1y, updated_beta_2y = return_beta_set(price, index, market_info) updated_beta_1y = updated_beta_1y.reset_index().melt(id_vars="date", var_name="stock_cd", value_name="beta_1y") updated_beta_2y = updated_beta_2y.reset_index().melt(id_vars="date", var_name="stock_cd", value_name="beta_2y") updated_beta = pd.merge(updated_beta_1y, updated_beta_2y, on=['date', 'stock_cd'], how='left').dropna() updated_beta = updated_beta[['stock_cd', 'date', 'beta_1y', 'beta_2y']] updated_beta = updated_beta.loc[updated_beta.date > beta_latest_date, :] return updated_beta
def update_idio_momt_table(): """ 서버db 업데이트(idio momt table의 가장 최근 시점 이후의 idiosyncratic momentum만을 기존 db에 append) """ idio_momt_latest_date = exec_query(f'select max(date) from stock_db.d_idio_momt')[0][0] idio_rt, idio_momt_latest_date = load_required_df() updated_idio_momt = return_idio_momt_set(idio_rt, idio_momt_latest_date) updated_idio_momt = updated_idio_momt.loc[updated_idio_momt.date > idio_momt_latest_date, :] return updated_idio_momt
def load_required_df(): """ mdd 업데이트에 필요한 df 추출 """ # 가장 최근 업데이트 날짜 추출 mdd_latest_date = exec_query(f'select max(date) from stock_db.d_mdd') if len(mdd_latest_date) != 0: mdd_latest_date = mdd_latest_date[0][0] else: mdd_latest_date = '20000101' # 가장 최근으로부터 2년 전 시점부터 데이터 추출 from_date = str(datetime.strptime(mdd_latest_date, '%Y%m%d').date() - timedelta(days=365 * 2 + 30)).replace("-", "") # 필요한 기간의 데이터 추출 price = pd.DataFrame( exec_query(f'select `stock_cd`, `date`, `price` from stock_db.d_stock_price where date > {from_date}')) price.columns = ['stock_cd', 'date', 'price'] return price, mdd_latest_date
def load_required_df(): """ idiosyncratic momentum 업데이트에 필요한 df 추출 """ # 가장 최근 업데이트 날짜 추출 idio_momt_latest_date = exec_query(f'select max(date) from stock_db.d_idio_momt') if len(idio_momt_latest_date) != 0: idio_momt_latest_date = idio_momt_latest_date[0][0] else: idio_momt_latest_date = '20000101' # 가장 최근으로부터 2년 전 시점부터 데이터 추출 from_date = str(datetime.strptime(idio_momt_latest_date, '%Y%m%d').date() - timedelta(days=365 * 2 + 30)).replace( "-", "") # 필요한 기간의 데이터 추출 idio_rt = pd.DataFrame( exec_query(f'select `stock_cd`, `date`, `idio_rt` from stock_db.d_idio_rt where date > {from_date}')) idio_rt.columns = ['stock_cd', 'date', 'idio_rt'] return idio_rt, idio_momt_latest_date
def load_required_df(): """ daily volatility 업데이트에 필요한 df 추출 """ # 가장 최근 업데이트 날짜 추출 daily_volatility_latest_date = exec_query( f'select max(date) from stock_db.d_daily_vol') idio_daily_volatility_latest_date = exec_query( f'select max(date) from stock_db.d_idio_daily_vol') if len(daily_volatility_latest_date) != 0: daily_volatility_latest_date = daily_volatility_latest_date[0][0] else: daily_volatility_latest_date = '20000101' if len(idio_daily_volatility_latest_date) != 0: idio_daily_volatility_latest_date = idio_daily_volatility_latest_date[ 0][0] else: idio_daily_volatility_latest_date = '20000101' # 가장 최근으로부터 2년 전 시점부터 데이터 추출 from_date = str( datetime.strptime(daily_volatility_latest_date, '%Y%m%d').date() - timedelta(days=365 * 2 + 30)).replace("-", "") from_date2 = str( datetime.strptime(idio_daily_volatility_latest_date, '%Y%m%d').date() - timedelta(days=365 * 2 + 30)).replace("-", "") # 필요한 기간의 데이터 추출 price = pd.DataFrame( exec_query( f'select `stock_cd`, `date`, `price` from stock_db.d_stock_price where date > {from_date}' )) idio_rt = pd.DataFrame( exec_query( f'select `stock_cd`, `date`, `idio_rt` from stock_db.d_idio_rt where date > {from_date2}' )) price.columns = ['stock_cd', 'date', 'price'] idio_rt.columns = ['stock_cd', 'date', 'idio_rt'] return price, idio_rt, daily_volatility_latest_date, idio_daily_volatility_latest_date