엠피엘파이낸스 패키지를 이용한 캔들 차트 표시

일거래 데이터는 증권사로 부터 내려받아서 RSIStrategy.db에 종목별로 보관하고 있고, 

테이블 조회 후 캐들 차트를 표시함.

 

일자를 datetime으로 변화해줘야 함.

 

엠피엘파이낸스 패키지 링크: https://github.com/matplotlib/mplfinance

 

import sqlite3
import pandas as pd
import mplfinance as mpf


def execute_sql(sql, param={}):
    with sqlite3.connect('RSIStrategy.db') as con:
        cur = con.cursor()
        cur.execute(sql, param)
        return cur

code = '010060' # OCI
sql = '''SELECT Date, Open, High, Low, Close, Volume
  FROM (
		SELECT `index` as Date, Open, High, Low, Close, Volumn as Volume
		 , ROW_NUMBER() over(order by "index" desc ) as rownum 
		  FROM `{}`
       )
where rownum <= 150  
'''.format(code)

cur = execute_sql(sql)
cols = [column[0] for column in cur.description]
df = pd.DataFrame.from_records(data=cur.fetchall(), columns=cols)
df = df.sort_values(by='Date')
df.index = pd.to_datetime(df['Date'], format='%Y%m%d')
df = df[['Open', 'High', 'Low', 'Close', 'Volume']]
print(df)

kwargs = dict(title='OCI candle chart', type='candle', mav=(5, 10, 20), volume=True, ylabel='ohlc candles')
mc = mpf.make_marketcolors(up='r', down='b', inherit=True)
s = mpf.make_mpf_style(marketcolors=mc)
# mpf.plot(df, title='OCI candle chart', type='candle')
mpf.plot(df, **kwargs, style=s)

 

728x90

+ Recent posts