본문 바로가기
데이터 분석/시각화

[Matplotlib] 분봉 차트(Candle Chart, 캔들 차트) 그리기

by 부자 꽁냥이 2023. 1. 22.

반갑습니다~ 꽁냥이입니다. 주가를 보다 보면 분봉 차트(Candle Chart, 캔들 차트)를 많이 보게 되는데요. 꽁냥이는 Matplotlib으로 분봉 차트(Candle Chart, 캔들 차트)를 그려보고 싶다는 생각이 들어서 이번 기회에 도전해 보았습니다. 

 

이번 포스팅에서는 분봉 차트(Candle Chart, 캔들 차트)를 Matplotlib만을 이용하여 그리는 방법과 mpl_finance 모듈을 이용하여 그리는 방법을 알아보겠습니다.

 

- 목차 -

1. Matplotlib으로 그리기

2. mpl_finance 이용하기


   1. Matplotlib으로 그리기

먼저 데이터를 불러옵니다. 

 

import FinanceDataReader as fdr
import matplotlib.pyplot as plt

from matplotlib.gridspec import GridSpec
from datetime import datetime 

start_date = datetime(2021,11,1).strftime('%Y-%m-%d')
end_date = datetime(2021,12,31).strftime('%Y-%m-%d')
    
df = fdr.DataReader('068270', start_date, end_date) ## 셀트리온 데이터
df.head()

 


이때 FinanceDataReader 모듈이 없으신 분들은 아래 코드를 복사하셔서 설치하시기 바랍니다.

 

pip install -U finance-datareader

분봉 차트(Candle Chart, 캔들 차트)를 그리는 방법은 다음과 같습니다.

 

1) 시가와 종가를 이용하여 박스를 만든다. 이때 시가가 종가보다 낮으면 시가가 박스 밑변이되고 종가가 낮으면 종가가 밑변이 된다. 박스의 가로길이는 임의로 적당히 지정하며 세로 길이는 시가와 종가의 차이가 된다.

 

2) 저가와 고가를 y축 방향으로 평행하게 그리고 앞에서 만든 박스 중앙에 오도록 한다.

3) 시가가 종가보다 낮으면 색상은 빨간색, 종가가 낮으면 파란색으로 설정한다.

 

이제 이러한 아이디어를 바탕으로 분봉 차트(Candle Chart, 캔들 차트)를 그리는 함수를 만들었습니다. 함수에 대한 설명은 주석을 참고하시면 됩니다.

 

from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection, LineCollection

def draw_candle_chart(xcoords, df, ax, width_ratio=0.8):
    def create_candle(xcoord, o, h, l, c, width, color, width_ratio):
        bottom_y = min([o, c]) ## 시가가 종가 중 낮은 값이 박스 좌하단 y좌표
        bottom_x = xcoord-0.5*width_ratio*width ## 박스 좌하단 x좌표
        bottom_left = (bottom_x, bottom_y) ## 박스 좌하단 좌표
        height = abs(c-o) ## 저가에서 고가를 나타내는 직선

        rect = Rectangle(bottom_left, width_ratio*width, height, facecolor=color, edgecolor=color) ## 박스 객체
        line = [(xcoord, l), (xcoord, h)] ## 선분 좌표 리스트
        return rect, line
    
    width = xcoords[1]-xcoords[0] ## 박스 폭 Offset
     
    ## x좌표를 돌면서 캔들(분봉)을 하나씩 만들어 리스트에 담는다.
    rect_list = []
    line_list = []
    line_color = []
    for i, xcoord in enumerate(xcoords):
        row = df.iloc[i]
        o = row['Open']
        h = row['High']
        l = row['Low']
        c = row['Close']
        if o<=c:
            color='r'
        else:
            color='b'
        rect, line = create_candle(xcoord, o, h, l, c, width, color, width_ratio)
        rect_list.append(rect)
        line_list.append(line)
        line_color.append(color)
        
    ## Rectanlge과 Line을 각각 PatchCollection과 LineCollection에 담아준다.
    patch_collection = PatchCollection(rect_list, match_original=True)
    line_collection = LineCollection(line_list, edgecolor=line_color, linewidths=1)
    
    ## 위에서 정의한 2개의 collection을 추가한다.
    ax.add_collection(patch_collection)
    ax.add_collection(line_collection)

 

이제 꽁냥이가 구현한 함수를 이용하여 분봉 차트(Candle Chart, 캔들 차트)를 그려보겠습니다. 꽁냥이는 분봉 차트 아래에 거래량을 바 차트로 표시했어요.

 

fig = plt.figure(figsize=(10, 6))
fig.set_facecolor('white')

num_row = 2
gs = GridSpec(num_row, 1, height_ratios=(3.5, 1.5))

ax_top = fig.add_subplot(gs[0, :])
draw_candle_chart(range(len(df)), df, ax_top) ## 분봉(캔들) 차트
ax_top.set_xmargin(0.05) ## 좌우 여백 비율
ax_top.set_ymargin(0.05) ## 위아래 여백 비율
ax_top.xaxis.set_visible(False)

## 거래량 바 차트
ax_bottom = fig.add_subplot(gs[1, :])
ax_bottom.bar(range(len(df)), df['Volume'], color='k')
ax_bottom.set_yticklabels(['{:.0f}'.format(x) for x in ax_bottom.get_yticks()])

xticks = range(len(df))[::5]
xticklabels = [x.strftime('%Y-%m-%d') for x in df.index[::5]]
ax_bottom.set_xticks(xticks)
ax_bottom.set_xticklabels(xticklabels)
ax_bottom.tick_params(axis='x', rotation=90)
plt.show()

 

 

코드를 실행하면 분봉 차트(Candle Chart, 캔들 차트)가 잘 그려진 것을 알 수 있습니다.


   2. mpl_finance 이용하기

이번엔 mpl_finance 모듈을 사용하여 분봉 차트(Candle Chart, 캔들 차트)를 그려보겠습니다. 먼저 아래 코드를 복사하여 mpl_finace 모듈을 설치합니다.

 

pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

 

mpl_finace에서 candlestick2_ohlc 함수를 이용하면 분봉 차트(Candle Chart, 캔들 차트)를 그릴 수 있습니다. candlestick2_ohlc는 첫 번째 인자로 그림을 그리고자할 좌표축을 지정하고 차례대로 시가, 고가, 저가, 종가 데이터를 넣어줍니다. 나머지 인자는 주석을 달아놓았으니 참고하시면 됩니다.

 

from mpl_finance import candlestick2_ohlc

fig = plt.figure(figsize=(10, 6))
fig.set_facecolor('white')

num_row = 2
gs = GridSpec(num_row, 1, height_ratios=(3.5, 1.5))

ax_top = fig.add_subplot(gs[0, :])
data = df[['Open', 'High', 'Low', 'Close']]

## 분봉(캔들) 차트
candlestick2_ohlc(ax_top, df['Open'], df['High'], df['Low'], df['Close'], 
                  width=0.8, ## 막대 폭 비율 조절
                  colorup='r', ## 종가가 시가보다 높은 경우에 색상
                  colordown='b' ## 종가가 시가보다 낮은 경우에 색상
                 )
ax_top.xaxis.set_visible(False)

ax_bottom = fig.add_subplot(gs[1, :])
ax_bottom.bar(range(len(df)), df['Volume'], color='k')
ax_bottom.set_yticklabels(['{:.0f}'.format(x) for x in ax_bottom.get_yticks()])


## 거래량 바 차트
xticks = range(len(df))[::5]
xticklabels = [x.strftime('%Y-%m-%d') for x in df.index[::5]]
ax_bottom.set_xticks(xticks)
ax_bottom.set_xticklabels(xticklabels)
ax_bottom.tick_params(axis='x', rotation=90)
plt.show()

 

코드를 실행하면 분봉 차트가 잘 그려진 것을 알 수 있으며 꽁냥이가 직접 구현한 것과 동일한 것을 알 수 있습니다.


이번 포스팅에서는 Matplotlib과 mpl_finance를 이용하여 분봉 차트(Candle Chart, 캔들 차트)를 그리는 방법을 알아보았습니다. 주가 데이터를 분석하시는 분들이 사용하면 좋을 듯합니다. 지금까지 꽁냥이 글 읽어주셔서 감사합니다. 


댓글


맨 위로