feat: 13장 모멘텀 포트폴리오 - k-ratio 추가
This commit is contained in:
		
							parent
							
								
									e6939a57ae
								
							
						
					
					
						commit
						0091fed41a
					
				| @ -1,6 +1,8 @@ | ||||
| import pandas as pd | ||||
| import matplotlib.pyplot as plt | ||||
| import seaborn as sns | ||||
| import statsmodels.api as sm | ||||
| import numpy as np | ||||
| import quantcommon | ||||
| 
 | ||||
| # 모멘텀 포트폴리오. 최근 12개월 수익률이 높은 주식 | ||||
| @ -51,4 +53,60 @@ g.set(ylabel=None) | ||||
| g.fig.set_figwidth(15) | ||||
| g.fig.set_figheight(8) | ||||
| plt.subplots_adjust(wspace=0.5, hspace=0.2) | ||||
| # plt.show() | ||||
| 
 | ||||
| # k-ratio(모멘텀의 꾸준함 지표) | ||||
| ret = price_pivot.pct_change().iloc[1:] | ||||
| ret_cum = np.log(1 + ret).cumsum() | ||||
| 
 | ||||
| x = np.array(range(len(ret))) | ||||
| y = ret_cum.iloc[:, 0].values | ||||
| 
 | ||||
| reg = sm.OLS(y, x).fit() | ||||
| reg.summary() | ||||
| 
 | ||||
| x = np.array(range(len(ret))) | ||||
| k_ratio = {} | ||||
| 
 | ||||
| for i in range(0, len(ticker_list)): | ||||
| 
 | ||||
|     ticker = data_bind.loc[i, '종목코드'] | ||||
| 
 | ||||
|     try: | ||||
|         y = ret_cum.loc[:, price_pivot.columns == ticker] | ||||
|         reg = sm.OLS(y, x).fit() | ||||
|         res = float(reg.params / reg.bse) | ||||
|     except: | ||||
|         res = np.nan | ||||
| 
 | ||||
|     k_ratio[ticker] = res | ||||
| 
 | ||||
| k_ratio_bind = pd.DataFrame.from_dict(k_ratio, orient='index').reset_index() | ||||
| k_ratio_bind.columns = ['종목코드', 'K_ratio'] | ||||
| 
 | ||||
| k_ratio_bind.head() | ||||
| 
 | ||||
| data_bind = data_bind.merge(k_ratio_bind, how='left', on='종목코드') | ||||
| k_ratio_rank = data_bind['K_ratio'].rank(axis=0, ascending=False) | ||||
| print(data_bind[k_ratio_rank <= 20]) | ||||
| 
 | ||||
| k_ratio_momentum = price_list[price_list['종목코드'].isin(data_bind.loc[k_ratio_rank <= 20, '종목코드'])] | ||||
| 
 | ||||
| plt.rc('font', family='Malgun Gothic') | ||||
| g = sns.relplot(data=k_ratio_momentum, | ||||
|                 x='날짜', | ||||
|                 y='종가', | ||||
|                 col='종목코드', | ||||
|                 col_wrap=5, | ||||
|                 kind='line', | ||||
|                 facet_kws={ | ||||
|                     'sharey': False, | ||||
|                     'sharex': True | ||||
|                 }) | ||||
| g.set(xticklabels=[]) | ||||
| g.set(xlabel=None) | ||||
| g.set(ylabel=None) | ||||
| g.fig.set_figwidth(15) | ||||
| g.fig.set_figheight(8) | ||||
| plt.subplots_adjust(wspace=0.5, hspace=0.2) | ||||
| plt.show() | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user