Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pykrx import stock
- import numpy as np
- import pandas as pd
- from pandas import Series
- import seaborn as sns
- import matplotlib.pyplot as plt
- class 원숭이:
- def __init__(self, ticker_list, num):
- self.ticker_list = ticker_list
- self.num = num
- self.임의종목수익률()
- def 보유수익률(self, df):
- 기간수익률 = df['종가'][-1]/df['종가'][0]
- N = (df.index[-1] - df.index[0]).days / 365
- return (기간수익률 ** (1/N)) - 1
- def 임의종목수익률(self):
- target = np.random.choice(self.ticker_list, self.num)
- self.투자종목 = target
- result = [ ]
- for ticker in target:
- df = pd.read_excel(f"kospi-market/{ticker}.xlsx", index_col=0)
- if len(df) > 1 :
- CAGR = self.보유수익률(df)
- result.append(CAGR)
- self.수익률 = np.mean(result)
- # 원숭이 100마리 생성 후 수익률 계산
- ticker_list = stock.get_market_ticker_list()
- result = [ 원숭이(ticker_list, 3) for x in range(100)]
- 수익률 = [ x.수익률 for x in result ]
- # 4분위수 출력
- print(np.percentile(수익률, 25))
- print(np.percentile(수익률, 50))
- print(np.percentile(수익률, 75))
- # 시각화
- result = sorted(수익률)
- sns.barplot(x=np.arange(len(result)), y=result)
- plt.show()
- sns.distplot(수익률)
- plt.show()
- # 10% 이상의 수익을 얻은 원숭이의 수 출력
- s = Series(result)
- print(len(s[s > 0.1]))
Add Comment
Please, Sign In to add comment