import os
import pandas as pd
file_save = "data/boillingeSkHy.csv"
if os.path.isfile(file_save):
data_df = pd.read_csv(file_save)
data_df['Date'] = pd.to_datetime(data_df['Date']) # datetime 포맷 변경
data_df = data_df.set_index('Date')
else:
from FinanceDataReader import DataReader as fdr
data_df = fdr("000660", "2019-01-01")
data_df.to_csv(file_save)
data_df.columns = [_.lower() for _ in data_df.columns]
data_raw = data_df.copy()
data_df.tail(3)
open | high | low | close | volume | change | |
---|---|---|---|---|---|---|
Date | ||||||
2021-07-30 | 114000 | 114000 | 112500 | 112500 | 2888566 | -0.013158 |
2021-08-02 | 113500 | 117000 | 113000 | 116000 | 4276730 | 0.031111 |
2021-08-03 | 118000 | 120500 | 117000 | 120000 | 4375622 | 0.034483 |
거래 범위 내에서 종가의 위치 를 토대로 종목내 자금 흐름 을 설명한다
$$ 일중강도 = {\frac{2 \times close-high-low}{high-low}} \times volume $$$$ 일중 강도율 = {\frac{ 일중강도 (21일) 합계 }{ 거래량 (21일) 합계 }} \times 100 $$# 볼린저 밴드 계산하기
data_df['MA20'] = data_df['close'].rolling(window=20).mean()
data_df['stddev'] = data_df['close'].rolling(window=20).std()
data_df['upper'] = data_df['MA20'] + (data_df['stddev'] * 2)
data_df['lower'] = data_df['MA20'] - (data_df['stddev'] * 2)
data_df['PB'] = (data_df['close'] - data_df['lower'])\
/ (data_df['upper'] - data_df['lower']) # %b 를 계산한다
# 일중강도 : 일중 강도율 계산하기
data_df['II'] = (2 * data_df['close'] - data_df['high'] - data_df['low'])\
/ (data_df['high'] - data_df['low']) * data_df['volume']
# 일중강도율 : 21간의 일중 강도율 합계
data_df['IIP21'] = data_df['II'].rolling(window=21).sum()\
/ data_df['volume'].rolling(window=21).sum()*100
data_df = data_df.dropna()
data_df.tail(3)
open | high | low | close | volume | change | MA20 | stddev | upper | lower | PB | II | IIP21 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | |||||||||||||
2021-07-30 | 114000 | 114000 | 112500 | 112500 | 2888566 | -0.013158 | 119500.0 | 3609.198774 | 126718.397548 | 112281.602452 | 0.015128 | -2.888566e+06 | -16.204695 |
2021-08-02 | 113500 | 117000 | 113000 | 116000 | 4276730 | 0.031111 | 119150.0 | 3591.290634 | 126332.581269 | 111967.418731 | 0.280720 | 2.138365e+06 | -12.422887 |
2021-08-03 | 118000 | 120500 | 117000 | 120000 | 4375622 | 0.034483 | 118900.0 | 3326.923662 | 125553.847324 | 112246.152676 | 0.582659 | 3.125444e+06 | -5.349208 |
결과값 시각화 작업
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
# Loading ... Visualization Modules
import matplotlib.pyplot as plt
import warnings
plt.style.use('seaborn')
warnings.simplefilter(action='ignore', category=FutureWarning)
from matplotlib.font_manager import fontManager as fm # 한글폰트 확인 및 추가
font_list_check = ['D2Coding', 'NanumGothicCoding', 'NanumGothic']
for _ in font_list_check:
font_counts = [f for f in font_list_check if _ in [_.name for _ in fm.ttflist]]
if len(font_counts) > 1: print(f"found : {_}"); plt.rc('font', family=_); break
found : D2Coding
plt.figure(figsize=(20, 8))
plt.subplot(3, 1, 1) # 볼린저 밴드 그리기
plt.title('SK Hynix Bollinger Band(20 day, 2 std) - Reversals')
plt.plot(data_df.index, data_df['close'], 'b', label='Close')
plt.plot(data_df.index, data_df['upper'], 'r--', label ='Upper band')
plt.plot(data_df.index, data_df['MA20'], 'k--', label='Moving average 20')
plt.plot(data_df.index, data_df['lower'], 'c--', label ='Lower band')
plt.fill_between(data_df.index, data_df['upper'], data_df['lower'], color='0.9')
plt.legend(loc='best')
plt.subplot(3, 1, 2) # 볼린저밴드 간격 변화값
plt.plot(data_df.index, data_df['PB'], 'b', label='%b')
plt.grid(True)
plt.legend(loc='best')
plt.subplot(3, 1, 3) # 일중 강도율
plt.bar(data_df.index, data_df['IIP21'], color='g', label='II% 21day') # ④
plt.grid(True)
plt.legend(loc='best')
plt.show()
plt.figure(figsize=(20, 8))
plt.subplot(3, 1, 1) # 볼린저 밴드 그리기
plt.title('SK Hynix Bollinger Band(20 day, 2 std) - Reversals')
plt.plot(data_df.index, data_df['close'], label='Close', color="orange")
plt.plot(data_df.index, data_df['upper'], 'r--', label ='Upper band')
plt.plot(data_df.index, data_df['MA20'], 'k--', label='Moving average 20')
plt.plot(data_df.index, data_df['lower'], 'c--', label ='Lower band')
plt.fill_between(data_df.index, data_df['upper'], data_df['lower'], color='0.9')
for i in range(0, len(data_df.close)):
if data_df.PB.values[i] < 0.05 and data_df.IIP21.values[i] > 0: # 매수시점
plt.plot(data_df.index.values[i], data_df.close.values[i], 'r^')
elif data_df.PB.values[i] > 0.95 and data_df.IIP21.values[i] < 0: # 매도시점
plt.plot(data_df.index.values[i], data_df.close.values[i], 'bv')
plt.legend(loc='best')
plt.subplot(3, 1, 2) # 볼린저 밴드 변동률
plt.plot(data_df.index, data_df['PB'], 'b', label='%b')
plt.grid(True)
plt.legend(loc='best')
plt.subplot(3, 1, 3) # 일중 강도율
plt.bar(data_df.index, data_df['IIP21'], color='g', label='II% 21day')
for i in range(0, len(data_df.close)):
if data_df.PB.values[i] < 0.05 and data_df.IIP21.values[i] > 0:
plt.plot(data_df.index.values[i], 0, 'r^')
elif data_df.PB.values[i] > 0.95 and data_df.IIP21.values[i] < 0:
plt.plot(data_df.index.values[i], 0, 'bv')
plt.grid(True)
plt.legend(loc='best')
plt.show()