-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinancial_data.py
More file actions
37 lines (30 loc) · 1.41 KB
/
financial_data.py
File metadata and controls
37 lines (30 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import FinanceDataReader as fdr
import pandas as pd
import time
import matplotlib.pyplot as plt
# https://blog.naver.com/sssrew1/221873065187
# 연습
df = fdr.DataReader('005930', '2020')
chart = df
ma5 = pd.DataFrame(chart['Close'].rolling(window=5).mean())
ma20 = pd.DataFrame(chart['Close'].rolling(window=20).mean())
ma60 = pd.DataFrame(chart['Close'].rolling(window=60).mean())
ma120 = pd.DataFrame(chart['Close'].rolling(window=120).mean())
# 5, 20, 60, 120 평선 추가
chart.insert(len(chart.columns), '5일', ma5)
chart.insert(len(chart.columns), '20일', ma20)
chart.insert(len(chart.columns), '60일', ma60)
chart.insert(len(chart.columns), '120일', ma120)
#차트 그리기
fig = plt.figure(figsize=(20, 10))
pr_line = plt.subplot2grid((4, 4), (0, 0), rowspan=3, colspan=4,)
vol_bar = plt.subplot2grid((4, 4), (3, 0), rowspan=1, colspan=4,)
x=chart.index.strftime('%m.%d')
pr_line.plot(chart.index, chart['5일'], label='MA5')
pr_line.plot(chart.index, chart['20일'], label='MA20')
pr_line.plot(chart.index, chart['60일'], label='MA60')
pr_line.plot(chart.index, chart['120일'], label='MA120')
pr_line.bar(chart.index, height=chart['Close']-chart['Open'], bottom=chart['Open'], width=1, color=list(map(lambda c: 'red' if c>0 else 'blue', chart['Change'])))
pr_line.vlines(chart.index, chart['Low'], chart['High'], color=list(map(lambda c: 'red' if c>0 else 'blue', chart['Change'])))
vol_bar.bar(x, df['Volume'])
plt.show()