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 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 fplt.candlestick_ochl(price) fplt.plot(ma20, legend='MA-20') fplt.plot(ma50, legend='MA-50') fplt.volume_ocv(volume, ax=axo) fplt.refresh() # refresh autoscaling when all plots complete Thread(target=lambda: info.setText(get_name(txt))).start( ) # slow, so use thread
def plot(txt): # Get data df = download(txt) if len(df) < 20: # symbol does not exist return info.setText('Loading symbol name...') # Cleanup ax.reset() # remove previous plots axo.reset() # remove previous plots # Process result price = df['Open Close High Low'.split()] volume = df['Open Close Volume'.split()] # Indicators emaShort = df.Close.ewm(span=10, min_periods=0, adjust=False, ignore_na=False).mean() emaMedium = df.Close.ewm(span=20, min_periods=0, adjust=False, ignore_na=False).mean() emaLong = df.Close.ewm(span=50, min_periods=0, adjust=False, ignore_na=False).mean() # Plot data fplt.candlestick_ochl(price) fplt.plot(emaShort, legend='EMA-10', color='#00FFFF', width=2) fplt.plot(emaMedium, legend='EMA-20', width=2) fplt.plot(emaLong, legend='EMA-50', color='#FF0000', width=2) fplt.volume_ocv(volume, ax=axo) fplt.refresh() # refresh autoscaling when all plots complete Thread(target=lambda: info.setText(get_name(txt))).start( ) # slow, so use thread
def updateCandlePane(self, quotes): self.ax.reset() finplot.candlestick_ochl(quotes) finplot.refresh() self.hoverLabel = finplot.add_legend('', ax=self.ax) finplot.set_time_inspector(self.updateLegend, ax=self.ax, when='hover')