728x90
반응형
주식에서 매수 및 매도 시점을 plot 상에 Marking 해주고,
해당 위치에 종가를 보여주는 코드 입니다.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pykrx import stock
code = '069500'
name = 'KODEX200'
start = '2019-07-01'
end = '2022-04-30'
# 주식 데이터 받기
df = stock.get_market_ohlcv(start, end, code)
# 컬럼명 변경
df.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
# 이동편균선 데이터 만든기
df['ma5'] = df['Close'].rolling(window=5).mean()
df['ma20'] = df['Close'].rolling(window=20).mean()
df['ma60'] = df['Close'].rolling(window=60).mean()
df['ma120'] = df['Close'].rolling(window=120).mean()
# Null 데이터 삭제
df = df.dropna()
# 골든크로스(Golden cross) 및 데드크로스(Death cross) 확인(20일, 60일 이동편균선)
# 매수, 매도 지점 등록
buy = []
sell = []
def chkCross(df):
chk = 0
for i in range(len(df)):
buy.append(False)
sell.append(False)
if df['ma60'][i] < df['ma20'][i] and chk == 0:
print('Golden cross ', str(df.index[i])[:10])
chk = 1
buy[i] = True
elif df['ma60'][i] > df['ma20'][i] and chk == 1:
print('Death cross ', str(df.index[i])[:10])
chk = 0
sell[i] = True
# 골든코로스/데드코로스 함수 실행
chkCross(df)
df['buy'] = buy
df['sell'] = sell
%matplotlib Qt5
fig = plt.figure(figsize=(15, 8))
plt.plot(df['Close'],label='Close')
plt.plot(df['ma20'],label='ma20')
plt.plot(df['ma60'],label='ma60')
plt.plot(df.ma20[df.buy == True], '^') # 매수지점 Marking
plt.plot(df.ma20[df.sell == True], 'v') # 메도지점 Marking
# 매도, 매수 시점에 종가 Text 보여 주기. +500은 Text가 조금 높게 보이도록 함
for i in range(len(df)):
if df.buy[i] == True:
# (X, Y, Text), X는 index, Y는 ma20 값에 + 500
plt.text(df.index[i], int(df['ma20'][i]) + 500, str(df['Close'][i]))
if df.sell[i] == True:
plt.text(df.index[i], int(df['ma20'][i]) + 500, str(df['Close'][i]))
plt.legend()
plt.grid()
plt.tight_layout()
plt.show()
Golden cross 2019-12-20
Death cross 2020-03-04
Golden cross 2020-05-14
Death cross 2021-03-24
Golden cross 2021-04-13
Death cross 2021-08-10
Golden cross 2021-12-21
Death cross 2022-01-25

728x90
반응형
'프로그램' 카테고리의 다른 글
[파이썬] 날짜 (구하기, 지정하기, 더하기, 요일 찾기) (0) | 2022.08.12 |
---|---|
[파이썬] 주식 매수/매도 테스트 (백테스트) (0) | 2022.08.11 |
[파이썬] 주식 골든크로스/데드크로스 찾기 (0) | 2022.08.11 |
[파이썬] pandas, DataFrame에서 max, min 찾기 (0) | 2022.08.11 |
[파이썬] 로또 번호 발생기 실행파일 만들기 (pyinstall) (0) | 2022.08.08 |
댓글