import numpy as np import pandas as pd import matplotlib.pyplot as plt from Practice.Investar import Analyzer mk = Analyzer.MarketDB() stocks = ['삼성전자', 'SK하이닉스', '현대자동차', 'NAVER'] df = pd.DataFrame() for s in stocks: df[s] = mk.get_daily_price(s, '2016-01-04', '2018-04-27')['close'] daily_ret = df.pct_change() annual_ret = daily_ret.mean() * 252 daily_cov = daily_ret.cov() annual_cov = daily_cov * 252 port_ret = [] port_risk = [] port_weights = [] sharpe_ratio = [] for _ in range(20000): weights = np.random.random(len(stocks)) weights /= np.sum(weights) returns = np.dot(weights, annual_ret) risk = np.sqrt(np.dot(weights.T, np.dot(annual_cov, weights))) port_ret.append(returns) port_risk.append(risk) port_weights.append(weights)
def __init__(self): """생성자: KRX 종목코드(codes)를 구하기 위한 MarkgetDB 객체 생성""" self.mk = Analyzer.MarketDB()