def plotCandleStickChart(cls, df, plot_ha=False, ema=[], ema_ha=[], snr=[], alignment="horizontal", log=None, status=None, init_zoom_periods=120): if status: status.info("updating candlestick charts ...") if not plot_ha: (gv, list_ax) = cls.blankGraphicsWidget(alignment=alignment, num_rows=1, init_zoom_periods=init_zoom_periods) ax_candle = list_ax else: (gv, list_ax) = cls.blankGraphicsWidget(alignment=alignment, num_rows=2, init_zoom_periods=init_zoom_periods) ax_candle = list_ax[0] ax_ha = list_ax[1] cls.plotHA(df, ax_ha) for ema_tf in ema_ha: cls.plotEma(df, ax_ha, ema_tf) print("---check :: --- ") print("gv " + str(sip.isdeleted(gv))) #print("flpt: " + str(sip.isdeleted(fplt))) cls.plotCandles(df, ax_candle) for ema_tf in ema: cls.plotEma(df, ax_candle, ema_tf) for price in snr: for i in range(-5, 5): fplt.add_line((0, price + i), (df.shape[0], price + i), interactive=True) if status: status.info("updated candlestick charts.") print("gv " + str(sip.isdeleted(gv))) #print("gv " + str(sip.isdeleted(fplt))) #fplt.show(qt_exec=False) ## c/c++ object error #print(gv) #print(list_ax) return (gv, list_ax)
def _redrawLine(self, priceText: str, fromTimestamp, toTimestamp, color: str): if priceText: price = float(priceText) finplot.add_line((fromTimestamp, price), (toTimestamp, price), color=color, interactive=False)
def _drawStopPriceIntersection(self, df, first_intersection, stopPrice, y_max, y_min): price = float(stopPrice) stop_df = df[df['DateTime'] > first_intersection['DateTime']] intersections = stop_df[self._ochlIntersectionMask(stop_df, price)] intersections.reset_index(inplace=True) size = len(intersections) if size > 0: candle = intersections.iloc[0] finplot.add_line((candle['DateTime'], y_min), (candle['DateTime'], y_max), color='f7ff00', interactive=False, width=3)
def _drawEntryPriceIntersection(self, df, entryPrice, first_intersection, int_step): price = float(entryPrice) intersections = df[self._ochlIntersectionMask(df, price)] intersections.reset_index(inplace=True) size = len(intersections) if size > 0: first_intersection = intersections.iloc[0] candle = intersections.iloc[0] finplot.add_line((candle['DateTime'], price + int_step), (candle['DateTime'], price - int_step), color='9900ff', interactive=False, width=3) return first_intersection
#!/usr/bin/env python3 import finplot as fplt import numpy as np import pandas as pd dates = pd.date_range('01:00', '01:00:01.200', freq='1ms') prices = pd.Series(np.random.random(len(dates))).rolling(30).mean() + 4 fplt.plot(dates, prices, width=3) line = fplt.add_line((dates[100], 4.4), (dates[1100], 4.6), color='#9900ff', interactive=True) ## fplt.remove_primitive(line) text = fplt.add_text((dates[500], 4.6), "I'm here alright!", color='#bb7700') ## fplt.remove_primitive(text) rect = fplt.add_rect((dates[700], 4.5), (dates[850], 4.4), color='#8c8', interactive=True) ## fplt.remove_primitive(rect) def save(): fplt.screenshot(open('screenshot.png', 'wb')) fplt.timer_callback(save, 0.5, single_shot=True) # wait some until we're rendered fplt.show()
daily_plot = fplt.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 fplt.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']] fplt.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]] fplt.add_line((linex[0], liney[0]), (linex[1], liney[1]), color='#993', ax=ax2) fplt.add_text((linex[1], liney[1]), 'k=%.2f' % line[0], color='#993', ax=ax2) fplt.add_legend('Alphabet vs. Microsft 90m correlation', ax=ax2) fplt.show()
import finplot as fplt import requests import pandas as pd now = date.today().strftime('%Y-%m-%d') r = requests.get( 'https://www.bitstamp.net/api-internal/tradeview/price-history/BTC/USD/?step=86400&start_datetime=2011-08-18T00:00:00.000Z&end_datetime=%sT00:00:00.000Z' % now) df = pd.DataFrame(r.json()['data']).astype({ 'timestamp': int, 'open': float, 'close': float, 'high': float, 'low': float }).set_index('timestamp') # plot price fplt.create_plot('Bitcoin 2011-%s' % now.split('-')[0], yscale='log') fplt.candlestick_ochl(df['open close high low'.split()]) # monthly separator lines months = pd.to_datetime(df.index, unit='s').strftime('%m') last_month = '' for x, (month, price) in enumerate(zip(months, df.close)): if month != last_month: fplt.add_line((x - 0.5, price * 0.5), (x - 0.5, price * 2), color='#bbb', style='--') last_month = month fplt.show()