def update(txt): df = download(txt) if len(df) < 20: # symbol does not exist return #info.setText('Loading symbol name...') price = df['Open Close High Low'.split()] ma20 = df.Close.rolling(20).mean() ma50 = df.Close.rolling(50).mean() volume = df['Open Close Volume'.split()] ax.reset() # remove previous plots axo.reset() # remove previous plots qplt.candlestick_ochl(price) qplt.plot(ma20, legend='MA-20') qplt.plot(ma50, legend='MA-50') qplt.volume_ocv(volume, ax=axo) qplt.refresh() # refresh autoscaling when all plots complete Thread(target=lambda: info.setText(get_name(txt))).start( ) # slow, so use thread
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 quantlplot.candlestick_ochl(df, **kwargs) elif 'volume' in kind: return quantlplot.volume_ocv(df, **kwargs) elif 'renko' in kind: return quantlplot.renko(df, **kwargs) elif kind == 'scatter': if 'style' not in kwargs: kwargs['style'] = 'o' return quantlplot.plot(_x, _y, **kwargs) elif kind == 'bar': return quantlplot.bar(_x, _y, **kwargs) elif kind in ('barh', 'horiz_time_volume'): return quantlplot.horiz_time_volume(df, **kwargs) elif kind in ('heatmap'): return quantlplot.heatmap(df, **kwargs) elif kind in ('labels'): return quantlplot.labels(df, **kwargs) elif kind in ('hist', 'histogram'): return quantlplot.hist(df, **kwargs) else: if x is None: _x = df _y = None if 'style' not in kwargs: kwargs['style'] = None return quantlplot.plot(_x, _y, **kwargs)
dfd['High'] = df.High.resample('D').max() dfd['Low'] = df.Low.resample('D').min() ax,ax2 = qplt.create_plot('Alphabet Inc.', rows=2, maximize=False) ax2.disable_x_index() # second plot is not timebased # plot down-sampled daily candles first daily_plot = qplt.candlestick_ochl(dfd.dropna(), candle_width=5) daily_plot.colors.update(dict(bull_body='#bfb', bull_shadow='#ada', bear_body='#fbc', bear_shadow='#dab')) daily_plot.x_offset = 3.1 # resample() gets us start of day, offset +1.1 (gap+off center wick) # plot high resolution on top qplt.candlestick_ochl(df[['Open','Close','High','Low']]) # scatter plot correlation between Google and Microsoft stock df['ret_alphabet'] = df.Close.pct_change() df['ret_microsoft'] = dfms.Close.pct_change() dfc = df.dropna().reset_index(drop=True)[['ret_alphabet', 'ret_microsoft']] qplt.plot(dfc, style='o', color=1, ax=ax2) # draw least-square line errfun = lambda arr: [y-arr[0]*x+arr[1] for x,y in zip(dfc.ret_alphabet, dfc.ret_microsoft)] line = scipy.optimize.least_squares(errfun, [0.01, 0.01]).x linex = [dfc.ret_alphabet.min(), dfc.ret_alphabet.max()] liney = [linex[0]*line[0]+line[1], linex[1]*line[0]+line[1]] qplt.add_line((linex[0],liney[0]), (linex[1],liney[1]), color='#993', ax=ax2) qplt.add_text((linex[1],liney[1]), 'k=%.2f'%line[0], color='#993', ax=ax2) qplt.add_legend('Alphabet vs. Microsft 90m correlation', ax=ax2) qplt.show()
for iv, vol in zip(volbins, g.volume): price2vol[iv.left] += vol data.append([t, sorted(price2vol.items())]) return data def calc_vwap(period): vwap = pd.Series([], dtype='float64') df['hlc3v'] = df['hlc3'] * df.volume for _, g in df.groupby(pd.Grouper(key='time', freq=period)): i0, i1 = g.index[0], g.index[-1] vwap = vwap.append(g.hlc3v.loc[i0:i1].cumsum() / df.volume.loc[i0:i1].cumsum()) return vwap # download and calculate indicators df = download_price_history( interval_mins=30) # reduce to [15, 5, 1] minutes to increase accuracy time_volume_profile = calc_volume_profile( df, period='W', bins=100) # try fewer/more horizontal bars (graphical resolution only) vwap = calc_vwap(period='W') # try period='D' # plot qplt.create_plot('Binance BTC futures weekly volume profile') qplt.plot(df.time, df.close, legend='Price') qplt.plot(df.time, vwap, style='--', legend='VWAP') qplt.horiz_time_volume(time_volume_profile, draw_va=0.7, draw_poc=1.0) qplt.show()