안녕하세요
현재 주식차트 그래프의 추세선을 이용해서 코인 예약 매매 시스템을 만들고 있습니다.
그래프 라이브러리 중에 plotly 를 찾아서 해보고 있는데 제가 원하는 그래프는 아닙니다..
더 좋은 그래프 라이브러리를 찾는다면 바꿔서 진행 할 예정입니다.
python plotly 와 dash 를 이용해서 아래 이미지 처럼 candlestick(stockchart) 나타나게 만들어 보았습니다.

import plotly.graph_objects as go | |
import dash | |
import pymysql | |
import dash_core_components as dcc | |
import dash_html_components as html | |
# DB connect | |
def db_con(): | |
db = pymysql.connect( | |
host='localhost', | |
user='id', | |
password='pw', | |
charset='utf8mb4', | |
database='db_name', | |
) | |
return db | |
# call data function | |
def graph(sbl, rod): | |
data = dict() | |
data['Open'] = list() | |
data['Close'] = list() | |
data['High'] = list() | |
data['Low'] = list() | |
name_list = list() | |
db = db_con() | |
sql = "select open, close, high, low, dt from chart where symbol = %s and rods = %s" | |
curs = db.cursor() | |
curs.execute(sql, (sbl, rod)) | |
rows = curs.fetchall() | |
# +--------+------+---------------------+----------+----------+----------+----------+ | |
# | symbol | rods | dt | open | close | high | low | | |
# +--------+------+---------------------+----------+----------+----------+----------+ | |
# | ETHBTC | day1 | 2020-05-25 00:00:00 | 0.022568 | 0.022682 | 0.022755 | 0.02249 | | |
# | ETHBTC | day1 | 2020-05-26 00:00:00 | 0.022691 | 0.023111 | 0.023236 | 0.022631 | | |
# | ETHBTC | day1 | 2020-05-27 00:00:00 | 0.023111 | 0.022818 | 0.023136 | 0.02265 | | |
# | ETHBTC | day1 | 2020-05-28 00:00:00 | 0.022814 | 0.022463 | 0.023018 | 0.02246 | | |
# | ETHBTC | day1 | 2020-05-29 00:00:00 | 0.022466 | 0.02239 | 0.022675 | 0.022137 | | |
# | ETHBTC | day1 | 2020-05-30 00:00:00 | 0.022414 | 0.023276 | 0.023428 | 0.022414 | | |
# | ETHBTC | day1 | 2020-05-31 00:00:00 | 0.02328 | 0.024915 | 0.024955 | 0.02327 | | |
# | ETHBTC | day1 | 2020-06-01 00:00:00 | 0.024915 | 0.024686 | 0.025576 | 0.024597 | | |
# | ETHBTC | day1 | 2020-06-02 00:00:00 | 0.024689 | 0.025044 | 0.02525 | 0.024462 | | |
# | ETHBTC | day1 | 2020-06-03 00:00:00 | 0.025049 | 0.024446 | 0.025219 | 0.024 | | |
# +--------+------+---------------------+----------+----------+----------+----------+ | |
for row in rows: | |
# y축의 값이지만 ochl 의 값을 표시 해야 하기 때문에 아래와 같이 만들어 준다 | |
data['Open'].append(row[0]) | |
data['Close'].append(row[1]) | |
data['High'].append(row[2]) | |
data['Low'].append(row[3]) | |
# x축 | |
name_list.append(row[4]) | |
db.close() | |
return data, name_list | |
# call data | |
data, name_list = graph('ETHBTC', 'min5') | |
# make graph | |
fig = go.Figure(data=[go.Candlestick(x=name_list, | |
open=data['Open'], high=data['High'], | |
low=data['Low'], close=data['Close'], | |
name = 'ETHBTC')]) | |
# css import | |
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] | |
# dash | |
app = dash.Dash(__name__, external_stylesheets=external_stylesheets) | |
# layout | |
app.layout = html.Div([ | |
# dcc.Graph(figure=fig), | |
html.Div( | |
[ | |
# 그래프 표시되기 전 로딩 표시 | |
dcc.Loading( | |
id="loading-1", | |
# figure 의 값에 위에 만들어 놓은 그래프 fig | |
children=[html.Div(id='figure-container', children=[dcc.Graph(figure=fig)])], | |
type="circle" | |
), | |
], | |
), | |
]) | |
# web server init | |
if __name__ == '__main__': | |
app.run_server( | |
port=8050, | |
host='0.0.0.0', | |
debug=False | |
) |
'개발 > Python' 카테고리의 다른 글
E: Unable to locate package python3-pip 오류 해결 (0) | 2020.05.29 |
---|---|
[python] deribit 거래소 api v2 활용하여 거래하기 (4) | 2020.04.01 |
python requests.exceptions.ConnectionError | Max retries exceeded with url 오류 (0) | 2020.03.25 |
python multiple file upload 파이썬 파일 다중 업로드 (0) | 2020.03.10 |
python 죽은 프로그램 다시 실행 시키는 os.execl 사용법 (0) | 2020.02.27 |