-
카테고리
-
세부 분야
금융 · 재테크
-
해결 여부
미해결
분산투자 중 다수종목수익률계산방법 질문
22.11.25 03:17 작성 조회수 151
0
섹션1 분산투자 : 다수종목수익률계산방법 수업 중 뒤쪽 그래프 작성에서 이해가 안가는 부분이 있어 질문 드립니다.
plt.figure(figsize=(20, 8))
cumReturn[stock].plot()
portCumReturn.plot(label = "porfolio", linestyle="dotted", linewidth=3)
이렇게만 하면 표가 제대로 안나오더라구요..
plt.figure(figsize=(20, 8))
for stock in cumReturn.columns:
cumReturn[stock].plot()
portCumReturn.plot(label = "porfolio", linestyle="dotted", linewidth=3)
저기서 왜 for 구문을 사용하는 것에 대해서 잘 모르겠습니다ㅠㅠㅠ
답변을 작성해보세요.
0
Jiwoon Jeong
지식공유자2022.11.25
안녕하세요.
우선 그래프만 표현하는게 목적이라면, 이런식으로 더 짧은 코드로 작성이 가능한데요.
plt.figure(figsize=(20, 8))
cumReturn.plot()
portCumReturn.plot(label = "porfolio", linestyle="dotted", linewidth=3)
plt.legend()
plt.show()
수강생분이 작성하신 코드는 stock이라는 변수가 지정이 안된 상태에서 호출했기에 표현이 안되는 것이라고 추측합니다.
plt.figure(figsize=(20, 8))
############# cumReturn[stock].plot() ################## stock이 정의가 안됌
portCumReturn.plot(label = "porfolio", linestyle="dotted", linewidth=3)
제가 반복문을 활용해 코드를 작성한 이유는,
각 종목별로 cagr, mdd를 계산하고 출력하기 위함이었습니다.
for stock in cumReturn.columns:
# 그래프
cumReturn[stock].plot(label=stock)
# cagr
cagr = cumReturn[stock].iloc[-1] ** (252/len(cumReturn[stock]))
# mdd
dd = (cumReturn[stock].cummax() - cumReturn[stock]) / cumReturn[stock].cummax() * 100
mdd= dd.max()
print(stock)
print(f"cagr: {cagr}\nmdd: {mdd}")
print("=======")
그래프만 나타내는 것이 목적이라면 더욱 간단한 코드로 작성할 수도 있었겠죠!
물론 코드를 작성하는 방식은 다양하고 정답이란 없습니다.
저도 다시 확인해보니 for문에서는 cagr, mdd 계산만하고 표현해도 되겠다는 생각이 드네요!
감사합니다.
답변 1