def plot_bollinger_bands(df, ax): mean = df.close.rolling(20).mean() stddev = df.close.rolling(20).std() df['boll_hi'] = mean + 2.5 * stddev df['boll_lo'] = mean - 2.5 * stddev p0 = df.boll_hi.plot(ax=ax, color='#808080', legend='BB') p1 = df.boll_lo.plot(ax=ax, color='#808080') fplt.fill_between(p0, p1, color='#bbb')
def update_plot(): global orderbook calc_bollinger_bands(df) candlesticks = df['t o c h l'.split()] bollband_hi = df['t bbh'.split()] bollband_lo = df['t bbl'.split()] if not plots: # 1st time candlestick_plot = fplt.candlestick_ochl(candlesticks) plots.append(candlestick_plot) plots.append(fplt.plot(bollband_hi, color='#4e4ef1')) plots.append(fplt.plot(bollband_lo, color='#4e4ef1')) fplt.fill_between(plots[1], plots[2], color='#9999fa') # generate dummy orderbook plot, which we update next time x = len(candlesticks) + 0.5 y = candlesticks.c.iloc[-1] orderbook = [[x, [(y, 1)]]] orderbook_colorfunc = fplt.horizvol_colorfilter([(0, 'bull'), (10, 'bear')]) orderbook_plot = fplt.horiz_time_volume(orderbook, candle_width=1, draw_body=10, colorfunc=orderbook_colorfunc) plots.append(orderbook_plot) # use bitmex colors candlestick_plot.colors.update( dict(bull_shadow='#388d53', bull_frame='#205536', bull_body='#52b370', bear_shadow='#d56161', bear_frame='#5c1a10', bear_body='#e8704f')) orderbook_plot.colors.update( dict(bull_frame='#52b370', bull_body='#bae1c6', bear_frame='#e8704f', bear_body='#f6c6b9')) else: # update plots[0].update_data(candlesticks) plots[1].update_data(bollband_hi) plots[2].update_data(bollband_lo) plots[3].update_data(orderbook)
def update_plot(): calc_bollinger_bands(df) candlesticks = df['t o c h l'.split()] bollband_hi = df['t bbh'.split()] bollband_lo = df['t bbl'.split()] if not plots: candlestick_plot = fplt.candlestick_ochl(candlesticks) plots.append(candlestick_plot) plots.append(fplt.plot(bollband_hi, color='#4e4ef1')) plots.append(fplt.plot(bollband_lo, color='#4e4ef1')) fplt.fill_between(plots[1], plots[2], color='#9999fa') # redraw using bitmex colors candlestick_plot.colors.update( dict(bull_shadow='#388d53', bull_frame='#205536', bull_body='#52b370', bear_shadow='#d56161', bear_frame='#5c1a10', bear_body='#e8704f')) candlestick_plot.repaint() else: plots[0].update_data(candlesticks) plots[1].update_data(bollband_hi) plots[2].update_data(bollband_lo)