def setup_plot(self, plot_title, kbars): import finplot as fplt kbars = kbars.reset_index().rename(columns={'ts': 'time'}) # adopt TWSE style fplt.display_timezone = gettz(self._exchange.brokerage.TIMEZONE) fplt.candle_bull_color = '#ef5350' fplt.candle_bull_body_color = fplt.candle_bull_color fplt.candle_bear_color = '#26a69a' fplt.volume_bull_color = '#f7a9a7' fplt.volume_bull_body_color = fplt.volume_bull_color fplt.volume_bear_color = '#92d2cc' main_ax, rsi_ax, macd_ax = fplt.create_plot(plot_title, rows=3) fplt.candlestick_ochl(kbars[['time', 'open', 'close', 'high', 'low']], ax=main_ax) fplt.volume_ocv(kbars[['time', 'open', 'close', 'volume']], ax=main_ax.overlay()) fplt.plot(kbars['time'], kbars['rsi'], ax=rsi_ax, legend='RSI') fplt.set_y_range(0, 100, ax=rsi_ax) fplt.add_band(self.MIN_RSI, self.MAX_RSI, ax=rsi_ax) fplt.volume_ocv(kbars[['time', 'open', 'close', 'macd_hist']], ax=macd_ax, colorfunc=fplt.strength_colorfilter) fplt.plot(kbars['time'], kbars['macd'], ax=macd_ax, legend='MACD') fplt.plot(kbars['time'], kbars['macd_signal'], ax=macd_ax, legend='Signal') return fplt
def change_asset(*args, **kwargs): '''Resets and recalculates everything, and plots for the first time.''' # save window zoom position before resetting fplt._savewindata(fplt.windows[0]) symbol = ctrl_panel.symbol.currentText() interval = ctrl_panel.interval.currentText() ws.df = None df = load_price_history(symbol, interval=interval) ws.reconnect(symbol, interval, df) # remove any previous plots ax.reset() axo.reset() ax_rsi.reset() # calculate plot data indicators = ctrl_panel.indicators.currentText().lower() data,price_data = calc_plot_data(df, indicators) # some space for legend ctrl_panel.move(100 if 'clean' in indicators else 200, 0) # plot data global plots plots = {} plots['price'] = fplt.candlestick_ochl(data['price'], ax=ax) plots['volume'] = fplt.volume_ocv(data['volume'], ax=axo) if data['ma50'] is not None: plots['ma50'] = fplt.plot(data['ma50'], legend='MA-50', ax=ax) plots['ma200'] = fplt.plot(data['ma200'], legend='MA-200', ax=ax) plots['vema24'] = fplt.plot(data['vema24'], color=4, legend='V-EMA-24', ax=axo) if data['rsi'] is not None: ax.set_visible(xaxis=False) ax_rsi.show() fplt.set_y_range(0, 100, ax=ax_rsi) fplt.add_band(30, 70, color='#6335', ax=ax_rsi) plots['sar'] = fplt.plot(data['sar'], color='#55a', style='+', width=0.6, legend='SAR', ax=ax) plots['rsi'] = fplt.plot(data['rsi'], legend='RSI', ax=ax_rsi) plots['stoch'] = fplt.plot(data['stoch'], color='#880', legend='Stoch', ax=ax_rsi) plots['stoch_s'] = fplt.plot(data['stoch_s'], color='#650', ax=ax_rsi) else: ax.set_visible(xaxis=True) ax_rsi.hide() # price line ax.price_line = pg.InfiniteLine(angle=0, movable=False, pen=fplt._makepen(fplt.candle_bull_body_color, style='.')) ax.price_line.setPos(price_data['last_close']) ax.price_line.pen.setColor(pg.mkColor(price_data['last_col'])) ax.addItem(ax.price_line, ignoreBounds=True) # restores saved zoom position, if in range fplt.refresh()
def plotRsi(cls, df, ax, key="Close", period=1): diff = df[key].diff().values gains = diff losses = -diff with np.errstate(invalid='ignore'): gains[(gains<0)|np.isnan(gains)] = 0.0 losses[(losses<=0)|np.isnan(losses)] = 1e-10 # we don't want divide by zero/NaN n = 14 m = (n-1) / n ni = 1 / n g = gains[n] = np.nanmean(gains[:n]) l = losses[n] = np.nanmean(losses[:n]) gains[:n] = losses[:n] = np.nan for i,v in enumerate(gains[n:],n): g = gains[i] = ni*v + m*g for i,v in enumerate(losses[n:],n): l = losses[i] = ni*v + m*l rs = gains / losses df['rsi'] = 100 - (100/(1+rs)) df.rsi.plot(ax=ax, legend='RSI') fplt.set_y_range(0, 100, ax=ax) fplt.add_band(30, 70, ax=ax)