def plot_quote_table(quotes, ax): '''Plot quote table (in millions). We're using lables on top of a heatmap to create sort of a table.''' ax.set_visible(yaxis=False) # Y axis is useless on our table def skip_y_crosshair_info(x, y, xt, yt): # we don't want any Y crosshair info on the table return xt, '' fplt.add_crosshair_info(skip_y_crosshair_info, ax=ax) fplt.set_y_range(0, 2, ax) # 0-1 for bid row, 1-2 for ask row # add two columns for table cell colors quotes[1] = -quotes['askSize'] * 0.5 / quotes['askSize'].max() + 0.5 quotes[0] = +quotes['bidSize'] * 0.5 / quotes['bidSize'].max() + 0.5 ts = [int(t.timestamp()) for t in quotes.index] colmap = fplt.ColorMap([0.0, 0.5, 1.0], [[200, 80, 60], [200, 190, 100], [40, 170, 30]]) # traffic light colors fplt.heatmap(quotes[[1, 0]], colmap=colmap, colcurve=lambda x: x, ax=ax) # linear color mapping fplt.labels(ts, [1.5]*count, ['%.1f'%(v/1e6) for v in quotes['askSize']], ax=ax2, anchor=(0.5, 0.5)) fplt.labels(ts, [0.5]*count, ['%.1f'%(v/1e6) for v in quotes['bidSize']], ax=ax2, anchor=(0.5, 0.5))
def plot(df, x, y, kind, **kwargs): _x = df.index if y is None else df[x] try: _y = df[x].reset_index(drop=True) if y is None else df[y] except: _y = df.reset_index(drop=True) kwargs = dict(kwargs) if 'by' in kwargs: del kwargs['by'] if kind in ('candle', 'candle_ochl', 'candlestick', 'candlestick_ochl', 'volume', 'volume_ocv', 'renko'): if 'candle' in kind: return finplot.candlestick_ochl(df, **kwargs) elif 'volume' in kind: return finplot.volume_ocv(df, **kwargs) elif 'renko' in kind: return finplot.renko(df, **kwargs) elif kind == 'scatter': if 'style' not in kwargs: kwargs['style'] = 'o' return finplot.plot(_x, _y, **kwargs) elif kind == 'bar': return finplot.bar(_x, _y, **kwargs) elif kind in ('barh', 'horiz_time_volume'): return finplot.horiz_time_volume(df, **kwargs) elif kind in ('heatmap'): return finplot.heatmap(df, **kwargs) elif kind in ('labels'): return finplot.labels(df, **kwargs) elif kind in ('hist', 'histogram'): return finplot.hist(df, **kwargs) else: if x is None: _x = df _y = None if 'style' not in kwargs: kwargs['style'] = None return finplot.plot(_x, _y, **kwargs)
daily_ret = btc.Close.pct_change() * 100 fplt.plot(daily_ret, width=3, color='#000', legend='Daily returns %', ax=ax2) fplt.add_legend('Daily % returns histogram', ax=ax3) fplt.hist(daily_ret, bins=60, ax=ax3) fplt.add_legend('Yearly returns in %', ax=ax4) fplt.bar(btc.Close.resample('Y').last().pct_change().dropna() * 100, ax=ax4) # calculate monthly returns, display as a 4x3 heatmap months = btc['Adj Close'].resample( 'M').last().pct_change().dropna().to_frame() * 100 months.index = mnames = months.index.month_name().to_list() mnames = mnames[mnames.index('January'):][:12] mrets = [months.loc[mname].mean()[0] for mname in mnames] hmap = pd.DataFrame(columns=[2, 1, 0], data=np.array(mrets).reshape((3, 4)).T) hmap = hmap.reset_index( ) # use the range index as X-coordinates (if no DateTimeIndex is found, the first column is used as X) colmap = fplt.ColorMap( [0.3, 0.5, 0.7], [[255, 110, 90], [255, 247, 0], [60, 255, 50]]) # traffic light fplt.heatmap(hmap, rect_size=1, colmap=colmap, colcurve=lambda x: x, ax=ax5) for j, mrow in enumerate(np.array(mnames).reshape((3, 4))): for i, month in enumerate(mrow): s = month + ' %+.2f%%' % hmap.loc[i, 2 - j] fplt.add_text((i, 2.5 - j), s, anchor=(0.5, 0.5), ax=ax5) ax5.set_visible(crosshair=False, xaxis=False, yaxis_labels=False) # hide junk for a more pleasing look fplt.show()
#!/usr/bin/env python3 import finplot as fplt import pandas as pd import requests # download, extract times and candles url = 'https://www.tensorcharts.com/tensor/bitmex/XBTUSD/heatmapCandles/15min' data = requests.get(url).json() times = pd.to_datetime([e['T'] for e in data]) candles = [(e['open'],e['close'],e['high'],e['low']) for e in data] df_candles = pd.DataFrame(index=times, data=candles) # extract volume heatmap as a PRICE x VOLUME matrix orderbooks = [(e['heatmapOrderBook'] or []) for e in data] prices = sorted(set(prc for ob in orderbooks for prc in ob[::2])) vol_matrix = [[0]*len(prices) for _ in range(len(times))] for i,orderbook in enumerate(orderbooks): for price,volume in zip(orderbook[::2],orderbook[1::2]): j = prices.index(price) vol_matrix[i][j] = volume df_volume_heatmap = pd.DataFrame(index=times, columns=prices, data=vol_matrix) # plot fplt.create_plot('BitMEX BTC 15m orderbook heatmap') fplt.candlestick_ochl(df_candles) fplt.heatmap(df_volume_heatmap, filter_limit=0.1, whiteout=0.1) fplt.show()