def run(pickle_file=PICKLE_FILE): mc = mpf.make_marketcolors(up='cyan', down='fuchsia', inherit=True) style = mpf.make_mpf_style(base_mpf_style='nightclouds', gridstyle='', marketcolors=mc) fig = mpf.figure(style=style, figsize=(19, 20)) plt.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.95, 0.95) df1 = utils.try_to_get_df(pickle_file) ntickets = len(df1.groupby('Papel').first().index.to_list()) positions = [] j1 = 0 for i in range(ntickets): if ntickets % 2 != 0 and i == ntickets - 1: positions.append((j1 + 1, j1 + 68)) j1 += 80 elif i % 2 == 0: positions.append((j1 + 1, j1 + 68)) positions.append((j1 + 5, j1 + 72)) j1 += 80 dimension = [j1 / 8, 8] global axes axes = [] for pos in positions: ax = fig.add_subplot(dimension[0], dimension[1], pos) axes.append(ax) a = ScrollableWindow(fig, analysis, pickle_file)
def main(): global client key, priv = loadConfig() client = BybitAcct(key, priv) fig = mpf.figure() ax = fig.add_subplot() return fig, ax, client
def candle_stick(ticker, start_date=(datetime.now() - timedelta(days=1)).strftime("%Y%m%d"), end_date=datetime.now().strftime("%Y%m%d")): start_date = datetime.strptime(str(start_date), '%Y%m%d').strftime('%Y-%m-%d') end_date = datetime.strptime(str(end_date), '%Y%m%d').strftime('%Y-%m-%d') tkr = yf.Ticker(ticker) stock = tkr.history(interval="1m", start=start_date, end=end_date) stock.drop(columns=['Dividends', 'Stock Splits'], inplace=True) print(f"plotting chart for {ticker} ...") # print(stock.head(5)) fig_d = mpf.figure(style="yahoo") ax1_d = fig_d.add_subplot(1, 1, 1) mpf.plot(stock, ax=ax1_d, type='candle') mpf.show() pass
def plot(self): for s in self.symbols: _wlines, _wcolors,_llines, _lcolors, rewards = self.transaction_line(s) _wseq = dict(alines=_wlines, colors=_wcolors) _lseq = dict(alines=_llines, colors=_lcolors, linestyle='--') _ohlc = self.ohlc.query(f'symbol=="{s}"') _style = mpf.make_mpf_style(base_mpl_style='seaborn',rc={'axes.grid':True}) fig = mpf.figure(style=_style,figsize=(40,20)) ax1 = fig.subplot() ax2 = ax1.twinx() mpf.plot(_ohlc, alines=_lseq ,mav=(10,20), ax=ax1,type='ohlc',style='default') mpf.plot(_ohlc,alines=_wseq, ax=ax2,type='candle',style='yahoo',axtitle=f'{s} reward: {rewards}') fig.savefig(f'./data/log/{s}-{datetime.datetime.now().strftime("%Y%m%d%H%M%S")}')
def make_single_plot(cls, ticker_name, start_t, end_t): data_to_plot = cls.search_ticker(ticker_to_search=ticker_name, time_start=start_t, time_end=end_t) if (isinstance(data_to_plot, str)): return None #breakpoint() mpf.figure() fig, axs = plt.subplots(figsize=(12, 3)) #axs = axs.flatten() fig.subplots_adjust(hspace=0.4) axs.plot(data_to_plot['Adj Close'], label='Adj Close') axs.plot(data_to_plot['Open'], label='Open', alpha=0.8) axs.plot(data_to_plot['Low'], label='Low', alpha=0.6) axs.plot(data_to_plot['High'], label='High', alpha=0.5) axs.legend() axs.set_title(ticker_name) return mpld3.fig_to_html(fig, figid="plotly")
def plotSpread(ticker1, ticker2, years): fig = mpf.figure(figsize=(15, 10)) axs = fig.add_subplot(1, 1, 1, style='yahoo') endDate = date.today() startDate = endDate - datetime.timedelta(days=years * 365) data1 = pdr.get_data_yahoo(ticker1, start=startDate, end=endDate) data2 = pdr.get_data_yahoo(ticker2, start=startDate, end=endDate) firstPrice = data1['Adj Close'] secondPrice = data2['Adj Close'] df1 = pd.DataFrame({'secondPrice': secondPrice, 'firstPrice': firstPrice}) df1.index = pd.to_datetime(df1.index) state_means = regression(firstPrice, secondPrice) df1['hr'] = -state_means[:, 0] df1['spread'] = df1.secondPrice + (df1.firstPrice * df1.hr) df2 = pd.DataFrame({ 'Open': df1['spread'].shift(periods=1), 'High': df1['spread'], 'Low': df1['spread'], 'Close': df1['spread'], 'Volume': df1['spread'] }) df2 = df2[1:] dt = 1 mu = 0 theta = 1 sigma = np.std(df1['spread']) ts = np.arange(0, len(df1['spread'].values), dt) var = np.array( [sigma**2 / (2 * theta) * (1 - np.exp(-2 * theta * t)) for t in ts]) std = 2 * np.sqrt(var) std = std[-1] upper = mu + std lower = mu - std mpf.plot(df2, type='candle', ax=axs, axtitle=f"Spread for {ticker2} / {ticker1}", hlines=dict(hlines=[mu, upper, lower], linestyle='-.', colors=['b', 'b', 'b']), xrotation=0) mpf.show()
def CreateAndSaveCharts(ohlc): tenRowDataArrays = [] i = 0 for x in range(len(ohlc)): if x > 24: data = ohlc.iloc[x - 24:x] fig = mpf.figure(style='charles', figsize=(7, 8)) ax1 = fig.add_subplot(2, 1, 1) plt.ioff() print(x) if (ohlc.iloc[x + 2, 3] < ohlc.iloc[x, 3]): #save = dict(fname='train2/sell/'+str(x)+'.png',dpi=30,pad_inches=0.25) mpf.plot(data, ax=ax1, type='candle', style='charles', mav=(3, 6, 9)) ax1.set_xticks([]) ax1.set_yticks([]) fig.savefig('train2/sell/' + str(x) + '.png') else: #save = dict(fname='train2/buy/'+str(x)+'.png',dpi=30,pad_inches=0.25) mpf.plot(data, ax=ax1, type='candle', style='charles', mav=(3, 6, 9), axisoff=True) ax1.set_xticks([]) ax1.set_yticks([]) fig.savefig('train2/buy/' + str(x) + '.png') fig.clf() fig.clear() plt.close(fig) ax1.clear() gc.collect() del (fig) del (ax1) del (data)
def mplfinance_plot(ticker, chart_type, syear, smonth, sday, eyear, emonth, eday): start = "{}-{}-{}".format(syear, smonth, sday) end = "{}-{}-{}".format(eyear, emonth, eday) try: df = pd.read_csv(CSV_PATH + ticker + ".csv") except FileNotFoundError: print("Specified CSV File Doesn't Exist") else: df.index = pd.DatetimeIndex(df['Date']) df_sub = df.loc[start:end] #mpf.plot(df_sub, type='candle') #mpf.plot(df_sub, type='line') mpf.plot(df_sub, type='ohlc', mav=4) s = mpf.make_mpf_style(base_mpf_style='charles', rc={'font.size': 10}) fig = mpf.figure(figsize=(12, 8), style=s) ax = fig.add_subplot(2, 1, 2) av = fig.add_subplot(2, 1, 2, sharex=ax) mpf.plot(df_sub, type=chart_type, mav=(3, 5, 7), ax=ax, volume=av, show_nontrading=True)
def show_graphic_from_finance(archivo, numero_velas, moneda): df = pd.read_csv(archivo, index_col='Datetime') df.index = pd.to_datetime(df.index, utc=True) fig = mpf.figure(style='charles', figsize=(7, 7)) ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(3, 1, 3) def animate(ival): data = update_data_from_finance(archivo, moneda, "5m") print("UPDATED") print(data.index.max()) data_to_print = data.iloc[(len(data) - numero_velas):len(data)] ax1.clear() ax2.clear() mpf.plot(data_to_print, ax=ax1, volume=ax2, type='candle') ani = animation.FuncAnimation(fig, animate, interval=300000) mpf.show()
def plot_df(df, datesAth=None, datesLow=None, log=True): #mpf.plot(df, hlines=[100,1000], type='candle') if datesAth is not None and datesLow is not None: fig = mpf.figure() ax1 = fig.add_subplot(1, 2, 1, style='yahoo') ax2 = fig.add_subplot(1, 2, 2, style='yahoo') mpf.plot(df, ax=ax1, vlines=dict(vlines=datesAth), type='candle') mpf.plot(df, ax=ax2, vlines=dict(vlines=datesLow), type='candle') plt.show() else: if log: fig, axlist = mpf.plot(df, type='candle', style='yahoo', returnfig=True) ax = axlist[0] ax.set_yscale('log') plt.show() else: mpf.plot(df, type='candle', style='yahoo', returnfig=True) plt.show()
def show_graphic_from_finance_and_predict(cuda): df = pd.read_csv("data/ETH-USD-5m.csv", index_col='Datetime') df.index = pd.to_datetime(df.index, utc=True) fig = mpf.figure(style='charles', figsize=(7, 7)) ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(3, 1, 3) list_models_load = [] list_models = get_list_models() # Cargamos los modelos print('Cargando modelos...') for name, pretrained in list_models: model = AdaptativeNet(pretrained) model.load_state_dict(torch.load('trained_nets/' + name + '.pth')) list_models_load.append((name, model)) print('Modelos cargados') def animate(ival): data = update_data_from_finance("data/ETH-USD-5m.csv", "ETH-USD", "5m") print("UPDATED") print(data.index.max()) data_to_print = data.iloc[(len(data) - 150):len(data)] net_dataset = Net_dataset_show(data_to_print) loader = torch.utils.data.DataLoader(net_dataset, shuffle=True, num_workers=2) with torch.no_grad(): prediction = predict(list_models_load, loader, cuda) print('La prediccion de la proxima vela sera: ' + prediction) ax1.clear() ax2.clear() mpf.plot(data_to_print, ax=ax1, volume=ax2, type='candle') ani = animation.FuncAnimation(fig, animate, interval=300000) mpf.show()
def candle_stick_daily(ticker, start_date=(datetime.now() - timedelta(days=60)).strftime("%Y%m%d"), end_date=datetime.now().strftime("%Y%m%d")): start_date = datetime.strptime(str(start_date), '%Y%m%d').strftime('%Y-%m-%d') end_date = datetime.strptime(str(end_date), '%Y%m%d').strftime('%Y-%m-%d') tkr = yf.Ticker(ticker) stock = tkr.history(interval="1d", start=start_date, end=end_date) stock.drop(columns=['Dividends', 'Stock Splits'], inplace=True) """ Open High Low Close Volume Date 2021-01-20 858.739990 859.500000 837.280029 850.450012 25665900 2021-01-21 855.000000 855.719971 841.419983 844.989990 20521100 2021-01-22 834.309998 848.000000 828.619995 846.640015 20066500 2021-01-25 855.000000 900.400024 838.820007 880.799988 41173400 2021-01-26 891.380005 895.900024 871.599976 883.090027 23131600 """ print(f"plotting chart for {ticker} ...") # print(stock.head(5)) fig_d = mpf.figure(style="yahoo") ax1_d = fig_d.add_subplot(1,1,1) mpf.plot(stock, ax=ax1_d, type='candle') mpf.show() pass
def plot(cls, df_in_major_timeframe: pd.DataFrame, major_timeframe_in_minutes: int, df_in_minor_timeframe: pd.DataFrame, minor_timeframe_in_minutes: int, zones: list[Zone] = [], levels: list[Level] = [], title: str = '', fig=None, ax_major_timeframe=None, ax_minor_timeframe=None): if fig is None: fig = mpf.figure(figsize=(10, 5)) fig.suptitle(title) plt.subplots_adjust(wspace=0, hspace=0) major_timeframe_style = mpf.make_mpf_style() minor_timeframe_style = mpf.make_mpf_style(y_on_right=True) ax_major_timeframe = fig.add_subplot(1, 2, 1, style=major_timeframe_style) ax_minor_timeframe = fig.add_subplot(1, 2, 2, style=minor_timeframe_style, sharey=ax_major_timeframe) major_timeframe_bar_nums = len(df_in_major_timeframe.index) minor_timeframe_bar_nums = len(df_in_minor_timeframe.index) for zone in zones: kwargs = dict(y1=zone.low, y2=zone.high, color=cls.ZONE_COLOR[zone.role], alpha=0.4) ax_major_timeframe.fill_between(x=(0, major_timeframe_bar_nums), **kwargs) ax_minor_timeframe.fill_between(x=(0, minor_timeframe_bar_nums), **kwargs) # TODO: draw vertical lines adapted to major timeframe # currently only draw hourly separator timestamps_on_the_hour = [ t for t in map(pd.Timestamp, df_in_minor_timeframe.index.values) if t.minute == 0 ] vlines = dict(vlines=timestamps_on_the_hour, colors='c', linestyle='-.', linewidths=2, alpha=0.4) # To draw levels or not #hlines = cls.get_levels_plot_config(levels) hlines = [] kwargs = dict(type='candle', ylabel='', xrotation=15, hlines=hlines) mpf.plot(df_in_major_timeframe, **kwargs, ax=ax_major_timeframe, axtitle=f'{major_timeframe_in_minutes}K') mpf.plot(df_in_minor_timeframe, **kwargs, ax=ax_minor_timeframe, axtitle=f'{minor_timeframe_in_minutes}K', vlines=vlines) return (fig, ax_major_timeframe, ax_minor_timeframe)
def plot_ohlc_all(self, data, block=True, save=False): """ :param data: [{code:code0, df:dataframe0(ohlc)}, {code:code1, df:dataframe1(ohlc)}, ...] :param block: if block is False when plot shows, plot run backend :param save: if save is True, plot is saved, don't show """ if save: if os.path.exists(self._save_path): save_path_names = [] for i in range(len(data)): save_path_name = self._save_path + '\\chart' + f"_{i}" + self._suffix save_path_names.append(save_path_name) mpf.plot(data[i]['df'], style='yahoo', type='candle', title=data[i]['code'], savefig=save_path_name) return save_path_names else: raise Exception('Save path {} does not exist.'.format( self._save_path)) else: page, limit = 0, min(8, len(data)) # page starts 0 max_page = math.ceil(len(data) / limit) fig = mpf.figure(figsize=(12, 9), style='yahoo') fig.subplots_adjust(left=0.03, bottom=0.2, right=0.95, top=0.95, hspace=0.2) axes = [] for i in range(limit): ax = fig.add_subplot(2, 4, i + 1) axes.append(ax) mpf.plot(data[i]['df'], ax=ax, type='candle', axtitle=data[i]['code']) def button_prev(event): nonlocal page, limit, max_page nonlocal data nonlocal axes, axtext page = (page - 1) if page > 0 else max_page - 1 replot_ohlc(data, axes, page, limit, axtext) def button_next(event): nonlocal page, limit, max_page nonlocal data nonlocal axes, axtext page = (page + 1) % max_page replot_ohlc(data, axes, page, limit, axtext) def replot_ohlc(data, axes, page, limit, axtext): axtext.clear() axtext.text(0.4, 0.2, '{:2d} / {:2d}'.format(page + 1, max_page)) for ax in axes: ax.clear() for i, ax in enumerate(axes): idx = i + (page * limit) if idx >= len(data): break mpf.plot(data[idx]['df'], ax=ax, type='candle', axtitle=data[idx]['code']) # make prev or next button axprev = fig.add_axes([0.7, 0.05, 0.1, 0.075]) axnext = fig.add_axes([0.81, 0.05, 0.1, 0.075]) bprev = Button(axprev, '◀') bnext = Button(axnext, '▶') bnext.on_clicked(button_next) bprev.on_clicked(button_prev) # make current page representation axtext = fig.add_axes([0.5, 0.05, 0.1, 0.075]) axtext.text(0.4, 0.2, '{:2d} / {:2d}'.format(page + 1, max_page)) mpf.show(block=block)
def chart(df): fig = mpf.figure(figsize=(10, 7)) ax = fig.add_subplot(2,1,1) av = fig.add_subplot(2,1,2, sharex=ax) mpf.plot(df, type='candle', ax=ax, volume=av)
def Grafico(indicadorTec, numSubplots1, numSubplots2, vr=None, pi=None, pf=None): """ Permite hacer la gráfica del activo en un periodo de tiempo """ fig = mpf.figure(figsize=(12, 9)) s = mpf.make_mpf_style(base_mpf_style='yahoo', y_on_right=False) if numSubplots1 == 1 and numSubplots2 == 0: ax1 = fig.add_subplot(2, 2, 2, style=s) ax11 = ax1.twinx() ap = mpf.make_addplot(indicadorTec.DataStock['RSI'], ax=ax11, ylabel='RSI') vol = fig.add_subplot(2, 2, 4, sharex=ax1, style=s) mpf.plot(indicadorTec.DataStock, volume=vol, ax=ax1, addplot=ap, xrotation=10, ylabel='Precio', type='candle', axtitle='Gráfica de valor con indicador RSI') elif numSubplots1 == 0 and numSubplots2 == 1: ax2 = fig.add_subplot(2, 2, 2, style=s) ax22 = ax2.twinx() ap = mpf.make_addplot(indicadorTec.DataStock['SMA'], ax=ax22, ylabel='SMA') vol = fig.add_subplot(2, 2, 4, sharex=ax2, style=s) mpf.plot(indicadorTec.DataStock, volume=vol, ax=ax2, addplot=ap, xrotation=10, ylabel='Precio', type='candle', axtitle='Gráfica de valor con indicador SMA') elif numSubplots1 == 1 and numSubplots2 == 1: ax1 = fig.add_subplot(2, 2, 2, style=s) ax11 = ax1.twinx() ap = mpf.make_addplot(indicadorTec.DataStock['RSI'], ax=ax11, ylabel='RSI') mpf.plot(indicadorTec.DataStock, ax=ax1, addplot=ap, xrotation=10, ylabel='Precio', type='candle', axtitle='Gráfica de valor con indicador RSI') ax2 = fig.add_subplot(2, 2, 4, style=s) ax22 = ax2.twinx() ap = mpf.make_addplot(indicadorTec.DataStock['SMA'], ax=ax22, ylabel='SMA') mpf.plot(indicadorTec.DataStock, ax=ax2, addplot=ap, xrotation=10, ylabel='Precio', type='candle', axtitle='Gráfica de valor con indicador SMA') if pi == None and pf == None: axp = fig.add_subplot(2, 2, 1, style=s) volp = fig.add_subplot(2, 2, 3, sharex=axp, style=s) mpf.plot(indicadorTec.DataStock, ax=axp, volume=volp, xrotation=10, ylabel='Precio', type='candle', axtitle='Gráfica del valor (últimos 30 días) ') mpf.show() else: axp = fig.add_subplot(2, 2, 1, style=s) volp = fig.add_subplot(2, 2, 3, sharex=axp, style=s) mpf.plot(indicadorTec.DataStock, ax=axp, volume=volp, xrotation=10, ylabel='Precio', type='candle', axtitle=f'Gráfica del valor (De {pi} a {pf}) ') mpf.show()
#print(df) m.plot(pred[HorizonPeriod:]) plt.title('Prophet: Forecasted Daily Closing Price', fontsize=25) #exponential smoothing VAMA a = pd.DataFrame(TA.EVWMA(subset)) #ATR b = pd.DataFrame(TA.ATR(subset)) fit3 = SimpleExpSmoothing(a, initialization_method="estimated").fit() fcast3 = fit3.forecast(1).rename(r'$\alpha=%s$'%fit3.model.params['smoothing_level']) #weighted moving averages s = mpf.make_mpf_style(base_mpf_style='charles', rc={'font.size': 6}) # add your own style here fig = mpf.figure(figsize=(10, 7), style=s) ax = fig.add_subplot(2,1,1) av = fig.add_subplot(2,1,2, sharex=ax) #az = fig.add_subplot(3,1,1) mpf.plot(subset[HorizonPeriod:],type='candle',mav=(3,6,9),volume=av,show_nontrading=True, ax=ax) my_dpi = 50 fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20, 20), dpi=my_dpi) # title for entire figure fig.suptitle(i, fontsize=20) #other technical axes[0, 0].plot(stock[HorizonPeriod:]["macds"], color="m", label="Signal Line") #axes[0, 1].set_title('Subplot 1', fontsize=14) axes[0, 0].plot(stock[HorizonPeriod:]["macd"], color="y", label="MACD")
import pandas as pd import mplfinance as mpf import matplotlib.animation as animation import datetime from datetime import date, datetime fig = mpf.figure(style="charles",figsize=(7,8)) ax1 = fig.add_subplot(1,1,1) def animate(ival): idf = pd.read_csv("bitcoin_data_tut.csv", index_col=0) idf['minute'] = pd.to_datetime(idf['minute'], format="%m/%d/%Y %H:%M") idf.set_index('minute', inplace=True) ax1.clear mpf.plot(idf, ax=ax1, type='candle', ylabel='Price US$') ani = animation.FuncAnimation(fig, animate, interval=250) mpf.show()
def plot_data(self): #generated_data = [] #for i in range(int(self.n_windows / self.batch_size)): Z_ = next(self.random_series) d = self.synthetic_data(Z_) R_ = next(self.real_series_iter) print('Este es el real') print(R_) real_data = np.array(R_) print(real_data.shape) generated_data = np.array(d) print(generated_data.shape) print(len(generated_data)) generated_data = (self.scaler.inverse_transform( generated_data.reshape(-1, self.n_seq)).reshape( -1, self.seq_len, self.n_seq)) real_data = (self.scaler.inverse_transform( real_data.reshape(-1, self.n_seq)).reshape(-1, self.seq_len, self.n_seq)) for i in range(3): # print(' sequence:{}'.format(i)) generated = generated_data[i, :, :] real = real_data[i, :, :] # print(generated) # numpy_data = np.array([[1, 2], [3, 4]]) base = datetime.datetime(2020, 11, 20) arr = np.array( [base + datetime.timedelta(minutes=j) for j in range(20)]) didx = pd.DatetimeIndex(data=arr, tz='America/New_York') gen_df = pd.DataFrame( data=generated, index=didx, columns=["Open", "High", "Low", "Close", "Volume"]) print(gen_df) real_df = pd.DataFrame( data=real, index=didx, columns=["Open", "High", "Low", "Close", "Volume"]) # mpf.plot(real_dat, type='candle',mav=(3,6,9),volume=True) fig = mpf.figure(style='yahoo', figsize=(14, 16)) ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) av1 = fig.add_subplot(3, 2, 5, sharex=ax1) av2 = fig.add_subplot(3, 2, 6, sharex=ax2) mpf.plot(gen_df, type='candle', ax=ax1, volume=av1, mav=(10, 20), axtitle='generated data') mpf.plot(real_df, type='candle', ax=ax2, volume=av2, mav=(10, 20), axtitle='real data') mpf.show()
def main(pipeline: str, dataset: str, symbol: str, window: int): ds = DatasetService() ms = ModelService() ts = TradingService() ohlcv_ds = ds.get_dataset('ohlcv', symbol=symbol) asset = ts.get_asset(pipeline=pipeline, dataset=dataset, target='class', symbol=symbol, window=window, create=False) if not asset: print( f"Asset {pipeline}.{dataset}.class for {symbol} on window {window} not found!" ) return test = ms.get_test(pipeline=pipeline, dataset=dataset, target='class', symbol=symbol, window=window) if not test: print( f"Test {pipeline}.{dataset}.class for {symbol} on window {window} not found!" ) # ohlcv = ohlcv.loc[test.test_interval.begin:test.test_interval.end] ohlcv = ds.get_dataset_features(ohlcv_ds, begin=test.test_interval.begin, end=test.test_interval.end) test_results = ModelService.parse_test_results(test).iloc[:-1] enc_label = onehot_target(test_results.label, labels=["is_sell", "is_hold", "is_buy"], fill=False) enc_pred = onehot_target(test_results.predicted, labels=["is_sell", "is_hold", "is_buy"], fill=False) # Mask predictions with low value minus a certain amount signals_level_diff = ohlcv.low * 10 / 100 signals_level = ohlcv.low - signals_level_diff #signals_level = ohlcv.low enc_pred.is_sell.mask(enc_pred.is_sell > 0, other=signals_level, inplace=True) enc_pred.is_hold.mask(enc_pred.is_hold > 0, other=signals_level, inplace=True) enc_pred.is_buy.mask(enc_pred.is_buy > 0, other=signals_level, inplace=True) # Get unique years in index to split plots in smaller scale unique_years = ohlcv.index.year.unique() for year in unique_years: year_pred = enc_pred[enc_pred.index.year == year] year_ohlcv = ohlcv[ohlcv.index.year == year] # Set up xticks daysToIndex = { ts.to_pydatetime(): i for i, ts in enumerate(year_ohlcv.index) } days = [i for i in daysToIndex.values()] labels = [ ts.to_pydatetime().strftime("%Y-%m-%d") for ts in year_ohlcv.index ] # Setup matplotfinance styles and figure s = mpf.make_mpf_style( base_mpf_style='binance') # , rc={'font.size': 6} fig = mpf.figure( figsize=(16, 8), style=s) # pass in the self defined style to the whole canvas fig.suptitle(f"{ohlcv_ds.symbol}, {year}, 1D") ax = fig.add_subplot(3, 1, (1, 2)) # main candle stick chart subplot av = fig.add_subplot(3, 1, 3, sharex=ax) # volume candles subplot # Setup horizontal grids ax.grid(axis='x', color='0.5', linestyle='--') av.grid(axis='x', color='0.5', linestyle='--') # for a in [ax, av]: # a.set_xticks(ticks=days) # a.set_xticklabels(labels=labels) # a.tick_params(axis='x', labelrotation=90) apds = [ # mpf.make_addplot(tcdf) # Predictions mpf.make_addplot(year_ohlcv.close, ax=ax, type='line', color=(0.5, 0.5, 0.5, 0.05)), mpf.make_addplot(year_pred.is_sell, ax=ax, type='scatter', marker='v', color='red'), mpf.make_addplot(year_pred.is_hold, ax=ax, type='scatter', marker='_', color='silver'), mpf.make_addplot(year_pred.is_buy, ax=ax, type='scatter', marker='^', color='lime'), ] mpf.plot( year_ohlcv, type='candle', style=s, #ylabel='Price ($)', ax=ax, volume=av, #ylabel_lower='Volume', show_nontrading=True, addplot=apds, returnfig=True) fig.autofmt_xdate() fig.tight_layout() plt.show() print("Done")
didx = pd.DatetimeIndex(data=arr, tz='America/New_York') gen_df = pd.DataFrame( data=generated, index=didx, columns=["Open", "High", "Low", "Close", "Volume"]) gen2_df = pd.DataFrame( data=generated, index=didx, columns=["Open", "High", "Low", "Close", "Volume"]) #print(gen_df) real_df = pd.DataFrame( data=real, index=didx, columns=["Open", "High", "Low", "Close", "Volume"]) #mpf.plot(real_dat, type='candle',mav=(3,6,9),volume=True) fig = mpf.figure(style='yahoo', figsize=(14, 16)) ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) av1 = fig.add_subplot(3, 2, 5, sharex=ax1) av2 = fig.add_subplot(3, 2, 6, sharex=ax2) mpf.plot(gen_df, type='candle', ax=ax1, volume=av1, mav=(10, 20), axtitle='generated data') mpf.plot(gen2_df, type='candle', ax=ax2, volume=av2,
import pandas as pd import mplfinance as mpf import matplotlib.animation as animation import datetime from datetime import date, datetime fig = mpf.figure(style="checkers", figsize=(6, 7)) ax1 = fig.add_subplot(1, 1, 1) def animate(ival): idf = pd.read_csv("/tmp/bitcoin_data_tut.csv", index_col=0) idf['minute'] = pd.to_datetime(idf['minute'], format="%m/%d/%Y %H:%M") idf.set_index('minute', inplace=True) ax1.clear mpf.plot(idf, ax=ax1, type='candle', style='checkers', mav=(3, 6, 9), ylabel='Price US$') ani = animation.FuncAnimation(fig, animate, interval=10000) mpf.show()
import datetime import pandas_market_calendars as mcal from tqdm.notebook import tqdm import mplfinance as mpf from preprocessor import add_tech_ind import io import matplotlib import matplotlib.pyplot as plt matplotlib.use('TKAgg') NY = 'America/New_York' START = pd.Timestamp('2009-01-02 09:30', tz=NY) END = pd.Timestamp('2020-12-31 15:00', tz=NY) ERASE_LINE = '\x1b[2K' fig = mpf.figure(figsize=(12, 9)) class DataGenerator: def __init__(self, start=None, end=None): all_minute_loc = glob.glob(f'/home/dewe/samgame/datasets/minute/*') self.sym_dict = { s.split('\\')[-1].split('_')[0]: s for s in all_minute_loc } if start and end: nyse = mcal.get_calendar('NYSE') early = nyse.schedule(start_date=start, end_date=end) full_date_range = mcal.date_range(early, frequency='1min').tz_convert(NY) self.full_date_range = full_date_range
acq_window = 500 reg_window = 250 reg_mean = 75 deviation = 0.8/ 100 rn = 200 path = './data/csv/EURUSD.s60.csv' rates = load_frames(path) rates = rates[-acq_window:] rates.tail() sample = rates[-144:-72] fig = mpf.figure(style='charles',figsize=(7,8)) ax1 = fig.add_subplot(2,1,1) ax2 = fig.add_subplot(3,1,3) def animate(ival): if ival+rn < calc_window: ival = calc_window - rn if (ival+rn) > len(rates): print('no more data to plot') ani.event_source.interval *= 3 if ani.event_source.interval > 12000: exit() return data = rates.iloc[ival:ival+rn] ax1.clear()
print(matplotlib.__version__) print(matplotlib.get_backend()) import mplfinance as mpf print(mpf.__version__) #idf = pd.read_csv('abc.csv',index_col=0,parse_dates=True) idf = pd.read_csv('../data/SPY_20110701_20120630_Bollinger.csv', index_col=0, parse_dates=True) idf.shape idf.head(3) idf.tail(3) df = idf.loc['2011-07-01':'2011-12-30', :] fig = mpf.figure(style='yahoo', figsize=(7, 8)) ax1 = fig.add_subplot(2, 1, 1) ax2 = fig.add_subplot(3, 1, 3) def animate(ival): if (20 + ival) > len(df): print('no more data to plot') ani.event_source.interval *= 3 if ani.event_source.interval > 12000: exit() return #return data = df.iloc[0:(20 + ival)] ax1.clear() ax2.clear()
def predictions(X): # Makes the prediciton based on the most recent data # Creates a plot comparing the predicted and actual prices # Loads saved model so retraining isn't needed # json_file = open('saved_models/MLP.json', 'r') # loaded_model_json = json_file.read() # json_file.close() # model = model_from_json(loaded_model_json) # model.load_weights('saved_models/MLP.h5') #X = currency[X] # Creates arrays to be used to specify each column open_col, high_col, low_col, clos_col, raw_seq = [], [], [], [], array( []) # Read input from CSV directo = os.path.dirname(__file__) path1 = os.path.join(directo, 'Finance_Data/Raw_Data/') with open(path1 + X, 'r') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) # Assignes each column within CSV to appropriate Array for lines in csv_reader: if 'null' in lines: continue else: # Index is +1 due to CSV indexing if lines[1] != 'null': open_col.append(float(lines[1])) if lines[2] != 'null': high_col.append(float(lines[2])) if lines[3] != 'null': low_col.append(float(lines[3])) if lines[4] != 'null': clos_col.append(float(lines[4])) # Converts list to a Numpy array open_col = array(open_col) high_col = array(high_col) low_col = array(low_col) clos_col = array(clos_col) # Reshape the array to columns and rows open_col = open_col.reshape((len(open_col), 1)) high_col = high_col.reshape((len(high_col), 1)) low_col = low_col.reshape((len(low_col), 1)) clos_col = clos_col.reshape((len(clos_col), 1)) raw_seq = hstack((open_col, high_col, low_col, clos_col)) directo = os.path.dirname(__file__) path1 = os.path.join(directo, '../saved_models/KNN_file') loaded_model = pickle.load(open(path1, 'rb')) res = [] #res.append([1.1212,1.12218,1.12106,1.12188]) #res.append([0.0, 0.0, 0.0, 0.0]) for i in raw_seq: i = [i] result = loaded_model.predict(i) result = result.flatten() result = result.tolist() res.append(result) print(raw_seq[-1]) print(res[-1]) reslen = len(res) rawlen = len(raw_seq) print('\n') no = [NaN, NaN, NaN, NaN] res.insert(0, no) result = pd.DataFrame(res) directo = os.path.dirname(__file__) path1 = os.path.join(directo, 'Finance_Data/Raw_Data/') file_loc = path1 + X data = pd.read_csv(file_loc, parse_dates=False, usecols=['Date']) data = data.values.tolist() data = np.array(data) data = data.flatten() raw_seq = pd.DataFrame(raw_seq) raw_seq['Date'] = data raw_seq.index = pd.DatetimeIndex(raw_seq['Date']) raw_seq.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] raw_seq['Volume'] = 0 print(raw_seq) # data = np.delete(data, 0) data = np.append(data, '2021-05-10') result['Date'] = data result.index = pd.DatetimeIndex(result['Date']) result.columns = ['Open', 'High', 'Low', 'Close', 'Volume'] result['Volume'] = 0 print(result) #print(len(result)) s = mpf.make_mpf_style(base_mpl_style='seaborn', rc={'axes.grid': False}) fig = mpf.figure(style=s, figsize=(7.5, 5.75)) ax1 = fig.subplot() ax2 = ax1.twinx() mpf.plot(result[reslen - 50:reslen], ax=ax1, type='candle') mpf.plot(raw_seq[rawlen - 50:rawlen], ax=ax2, type='candle', style='yahoo') plt.show()
def get_tickets(df): tickets = [] for value in df.index.values: if not value[1] in tickets: tickets.append(value[1]) return tickets mc = mpf.make_marketcolors(up='cyan', down='fuchsia', inherit=True) style = mpf.make_mpf_style(base_mpf_style='nightclouds', gridstyle='', marketcolors=mc) fig = mpf.figure(style=style, figsize=(20, 20)) plt.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.95, 0.95) dimension = [29, 8] positions = [[(1, 52), (57, 68)], [(5, 56), (61, 72)], [(81, 132), (137, 148)], [(85, 136), (141, 152)], [(163, 214), (219, 230)]] axes = [] for pos in positions: ax = fig.add_subplot(dimension[0], dimension[1], pos[0]) ar = fig.add_subplot(dimension[0], dimension[1], pos[1], sharex=ax) axes.append([ax, ar]) def analysis(ival, fargs):
import mplfinance as mpf import pandas as pd #daily = pd.read_csv('/Users/jsb/Desktop/mplfinance-master/examples/data/SP500_NOV2019_Hist.csv', # index_col=0, parse_dates=True) daily = pd.read_csv('../../data/SP500_NOV2019_Hist.csv', index_col=0, parse_dates=True) daily.index.name = 'Date' # print(daily.shape) print(daily) # daily = daily.loc[:, ['Open', 'High', 'Low', 'Close', 'Volume']] mpf.figure(figsize=(20, 8), dpi=100) mpf.plot(daily, type='candle', tight_layout=True)