Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created December 8, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highfestiva/9b49832d78651fb653af61238d08391d to your computer and use it in GitHub Desktop.
Save highfestiva/9b49832d78651fb653af61238d08391d to your computer and use it in GitHub Desktop.
Plot some candlesticks based on crypto currency data. Prepped for use in real webserver.
#!/usr/bin/env python3
from bokeh.embed import components as plot_html
from bokeh.plotting import figure, show, output_file
import pandas as pd
import requests
import webbrowser
url = 'https://www.binance.com/api/v1/klines?interval=15m&symbol=ARNBTC&limit=100'
data = requests.get(url).json()
df = pd.DataFrame(data, columns='time open hi lo close vol time_close a b c d e'.split())
up = df.close > df.open
dn = df.open > df.close
p = figure(x_axis_type='datetime', plot_width=1000, plot_height=500, sizing_mode='scale_width', title = 'ARN/BTC')
p.background_fill_alpha = 0
p.border_fill_alpha = 0
p.grid.grid_line_alpha = 0.3
p.outline_line_alpha = 0.3
p.xaxis.axis_line_color = 'whitesmoke'
p.yaxis.axis_line_color = 'whitesmoke'
p.xaxis.axis_line_alpha = 0
p.yaxis.axis_line_alpha = 0
p.xaxis.major_tick_line_alpha = 0
p.yaxis.major_tick_line_alpha = 0
p.xaxis.minor_tick_line_alpha = 0
p.yaxis.minor_tick_line_alpha = 0
w = 8*60*1000
p.segment(df.time[up], df.hi[up], df.time[up], df.lo[up], color='#33dd99')
p.segment(df.time[dn], df.hi[dn], df.time[dn], df.lo[dn], color='#eeaa99')
p.vbar(df.time[up], w, df.open[up], df.close[up], fill_color='#779988', line_color='#33dd99')
p.vbar(df.time[dn], w, df.open[dn], df.close[dn], fill_color='#cc9988', line_color='#eeaa99')
script,div = plot_html(p)
html = '''
<html>
<head>
<style>
body { background: #222; }
</style>
<link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.12.13.min.css" type="text/css">
<script src="https://cdn.pydata.org/bokeh/release/bokeh-0.12.13.min.js"></script>
%s
</head>
<body>
%s
</body>
<html>
''' % (script, div)
open('graph.html', 'w').write(html)
webbrowser.open('graph.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment