import plotly.graph_objs as go
# specific function
def draw_dob_size_box_plots(df_dob):
_draw_dob_box_plots(df_dob, 'size')
# generic "private" function
def _draw_dob_box_plots(df_dob, size_or_price):
trace_asks0 = go.Box(
y=df_dob['asks[0].' + size_or_price],
name='asks[0]' + size_or_price,
marker=dict(
color='#3D9970'
)
)
trace_bids0 = go.Box(
y=df_dob['bids[0].' + size_or_price],
name='bids[0].' + size_or_price,
marker=dict(
color='#3D9970'
)
)
trace_asks1 = go.Box(
y=df_dob['asks[1].' + size_or_price],
name='asks[1].' + size_or_price,
marker=dict(
color='#6D9970'
)
)
trace_bids1 = go.Box(
y=df_dob['bids[1].' + size_or_price],
name='bids[1].' + size_or_price,
marker=dict(
color='#6D9970'
)
)
trace_asks2 = go.Box(
y=df_dob['asks[2].' + size_or_price],
name='asks[2].' + size_or_price,
marker=dict(
color='#9D9970'
)
)
trace_bids2 = go.Box(
y=df_dob['bids[2].' + size_or_price],
name='bids[2].' + size_or_price,
marker=dict(
color='#9D9970'
)
)
trace_asks3 = go.Box(
y=df_dob['asks[3].' + size_or_price],
name='asks[3].' + size_or_price,
marker=dict(
color='#BD9970'
)
)
trace_bids3 = go.Box(
y=df_dob['bids[3].' + size_or_price],
name='bids[3].' + size_or_price,
marker=dict(
color='#BD9970'
)
)
trace_asks4 = go.Box(
y=df_dob['asks[4].' + size_or_price],
name='asks[4].' + size_or_price,
marker=dict(
color='#ED9970'
)
)
trace_bids4 = go.Box(
y=df_dob['bids[4].' + size_or_price],
name='bids[4].' + size_or_price,
marker=dict(
color='#ED9970'
)
)
data = [trace_asks0, trace_bids0, trace_asks1, trace_bids1, trace_asks2, trace_bids2, \
trace_asks3, trace_bids3, trace_asks4, trace_bids4]
layout = go.Layout(
yaxis=dict(
title=size_or_price + 'Boxplot',
zeroline=False
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df_dob, x=”date_time_coinapi”)
fig.show()
import plotly.graph_objs as go
def draw_dob_price_timeseries(df_dob):
elements0 = ['asks[0].price','bids[0].price']
elements1 = ['asks[1].price','bids[1].price']
elements2 = ['asks[2].price','bids[2].price']
elements3 = ['asks[3].price','bids[3].price']
elements4 = ['asks[4].price','bids[4].price']
elements = [elements0, elements1, elements2, elements3, elements4]
for el in elements:
_draw_dob_timeseries(df_dob, el, 'Price timeseries')
def _draw_dob_timeseries(df_dob, elements, title):
trace_asks0 = go.Scatter(
x = df_dob.date_time_exchange,
y=df_dob[elements[0]],
name=elements[0],
line = dict(color = '#17BECF'),
opacity = 0.8
)
trace_bids0 = go.Scatter(
x = df_dob.date_time_exchange,
y=df_dob[elements[1]],
name=elements[1],
line = dict(color = '#7F7F7F'),
opacity = 0.8
)
data = [trace_asks0, trace_bids0]
layout = go.Layout(
yaxis=dict(
title=title,
zeroline=False
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()
def _draw_dob_box_plots(df_dob, size_or_price):
trace_asks0 = go.Box(
y=df_dob['asks[0].' + size_or_price],
name='asks[0]' + size_or_price,
marker=dict(
color='#3D9970'
)
)
trace_bids0 = go.Box(
y=df_dob['bids[0].' + size_or_price],
name='bids[0].' + size_or_price,
marker=dict(
color='#3D9970'
)
)
trace_asks1 = go.Box(
y=df_dob['asks[1].' + size_or_price],
name='asks[1].' + size_or_price,
marker=dict(
color='#6D9970'
)
)
trace_bids1 = go.Box(
y=df_dob['bids[1].' + size_or_price],
name='bids[1].' + size_or_price,
marker=dict(
color='#6D9970'
)
)
trace_asks2 = go.Box(
y=df_dob['asks[2].' + size_or_price],
name='asks[2].' + size_or_price,
marker=dict(
color='#9D9970'
)
)
trace_bids2 = go.Box(
y=df_dob['bids[2].' + size_or_price],
name='bids[2].' + size_or_price,
marker=dict(
color='#9D9970'
)
)
trace_asks3 = go.Box(
y=df_dob['asks[3].' + size_or_price],
name='asks[3].' + size_or_price,
marker=dict(
color='#BD9970'
)
)
trace_bids3 = go.Box(
y=df_dob['bids[3].' + size_or_price],
name='bids[3].' + size_or_price,
marker=dict(
color='#BD9970'
)
)
trace_asks4 = go.Box(
y=df_dob['asks[4].' + size_or_price],
name='asks[4].' + size_or_price,
marker=dict(
color='#ED9970'
)
)
trace_bids4 = go.Box(
y=df_dob['bids[4].' + size_or_price],
name='bids[4].' + size_or_price,
marker=dict(
color='#ED9970'
)
)
data = [trace_asks0, trace_bids0, trace_asks1, trace_bids1, trace_asks2, trace_bids2, \
trace_asks3, trace_bids3, trace_asks4, trace_bids4]
layout = go.Layout(
yaxis=dict(
title=size_or_price + 'Boxplot',
zeroline=False
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()
import plotly.graph_objs as go
def draw_trades_bars(df_trades):
trace0 = go.Bar(
x = np.array(df_trades[df_trades.taker_side == 'BUY'].price.count()),
name = 'Number of buy',
marker=dict(
color='#009970')
)
trace1 = go.Bar(
x = np.array(df_trades[df_trades.taker_side == 'SELL'].price.count()),
name = 'Number of sell',
marker=dict(
color='#DD0000')
)
data = [trace0, trace1]
layout = go.Layout(
yaxis=dict(
title='Trades Bar',
zeroline=False
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()
RESAMPLE_TIME = '1H'
df_dob_resampled = df_dob.copy()
df_dob_resampled.index = df_dob_resampled['date_time_exchange']
df_dob_resampled = df_dob_resampled.resample(RESAMPLE_TIME).max()
df_dob_resampled.drop(columns=['date_time_exchange','date_time_coinapi'], inplace=True)
df_dob_resampled['max_asks_size'] = df_dob_resampled[['asks[0].size','asks[1].size', 'asks[2].size', 'asks[3].size', 'asks[4].size']].max(axis=1)
df_dob_resampled['max_bids_size'] = df_dob_resampled[['bids[0].size','bids[1].size', 'bids[2].size', 'bids[3].size', 'bids[4].size']].max(axis=1)
df_dob_resampled['max_asks_price'] = df_dob_resampled[['asks[0].price','asks[1].price', 'asks[2].price', 'asks[3].price', 'asks[4].price']].max(axis=1)
df_dob_resampled['max_bids_price'] = df_dob_resampled[['bids[0].price','bids[1].price', 'bids[2].price', 'bids[3].price', 'bids[4].price']].max(axis=1)
df_dob_resampled.drop(columns=[
'asks[0].size','asks[1].size', 'asks[2].size', 'asks[3].size', 'asks[4].size', \
'bids[0].size','bids[1].size', 'bids[2].size', 'bids[3].size', 'bids[4].size', \
'asks[0].price','asks[1].price', 'asks[2].price', 'asks[3].price', 'asks[4].price', \
'bids[0].price','bids[1].price', 'bids[2].price', 'bids[3].price', 'bids[4].price'], inplace=True)
import plotly.graph_objs as go
def draw_max_timeseries(df_dob_resampled, df_trades_resampled_buy, df_trades_resampled_sell, title):
trace0 = go.Scatter(
x = df_dob_resampled.index,
y = df_dob_resampled['max_asks_price'],
mode = 'lines+markers',
name='max_asks_price',
line = dict(color = '#dd0000', shape = 'linear'),
opacity = 0.3,
connectgaps=True
)
trace1 = go.Scatter(
x = df_dob_resampled.index,
y = df_dob_resampled['max_bids_price'],
name='max_bids_price',
mode = 'lines+markers',
marker = dict(
size = 10,
color = '#44dd00'),
opacity = 0.3
)
trace2 = go.Scatter(
x = df_trades_resampled_buy.index,
y = df_trades_resampled_buy.price,
name='trades BUY price',
mode = 'markers',
marker = dict(
size = 10,
color = '#00dd00'),
opacity = 0.8
)
trace3 = go.Scatter(
x = df_trades_resampled_sell.index,
y = df_trades_resampled_sell.price,
name='trades SELL price',
mode = 'markers',
marker = dict(
size = 10,
color = '#dd0000'),
opacity = 0.8
)
data = [trace0, trace1, trace2, trace3]
layout = go.Layout(
yaxis=dict(
title=title,
zeroline=True
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()
draw_max_timeseries(df_dob_resampled, df_trades_resampled_buy, df_trades_resampled_sell, 'Max aggregated data timeseries')
RESAMPLE_TIME = '60s'
START_INTERVAL = '2018-01-15 22:00:00'
END_INTERVAL = '2018-01-16 02:00:00'
df_dob_resampled_interval = df_dob.copy()
df_dob_resampled_interval.index = df_dob_resampled_interval['date_time_exchange']
df_dob_resampled_interval = df_dob_resampled_interval[START_INTERVAL:END_INTERVAL]
df_dob_resampled_interval = df_dob_resampled_interval.resample(RESAMPLE_TIME).max()
df_dob_resampled_interval.drop(columns=['date_time_exchange','date_time_coinapi'], inplace=True)
df_dob_resampled_interval['max_asks_size'] = df_dob_resampled_interval[['asks[0].size','asks[1].size', 'asks[2].size', 'asks[3].size', 'asks[4].size']].max(axis=1)
df_dob_resampled_interval['max_bids_size'] = df_dob_resampled_interval[['bids[0].size','bids[1].size', 'bids[2].size', 'bids[3].size', 'bids[4].size']].max(axis=1)
df_dob_resampled_interval['max_asks_price'] = df_dob_resampled_interval[['asks[0].price','asks[1].price', 'asks[2].price', 'asks[3].price', 'asks[4].price']].max(axis=1)
df_dob_resampled_interval['max_bids_price'] = df_dob_resampled_interval[['bids[0].price','bids[1].price', 'bids[2].price', 'bids[3].price', 'bids[4].price']].max(axis=1)
df_dob_resampled_interval.drop(columns=[
'asks[0].size','asks[1].size', 'asks[2].size', 'asks[3].size', 'asks[4].size', \
'bids[0].size','bids[1].size', 'bids[2].size', 'bids[3].size', 'bids[4].size', \
'asks[0].price','asks[1].price', 'asks[2].price', 'asks[3].price', 'asks[4].price', \
'bids[0].price','bids[1].price', 'bids[2].price', 'bids[3].price', 'bids[4].price'], inplace=True)
df_dob_resampled_interval.head()
df_trades_resampled_interval = df_trades.copy()
df_trades_resampled_interval.index = df_trades_resampled_interval['time_exchange']
df_trades_resampled_interval = df_trades_resampled_interval[START_INTERVAL:END_INTERVAL]
df_trades_resampled_interval_buy = df_trades_resampled_interval[df_trades_resampled_interval.taker_side == 'BUY'].resample(RESAMPLE_TIME).max()
df_trades_resampled_interval_buy.drop(columns=['time_exchange', 'time_coinapi','guid'], inplace=True)
df_trades_resampled_interval_buy.head()
df_trades_resampled_interval = df_trades.copy()
df_trades_resampled_interval.index = df_trades_resampled_interval['time_exchange']
df_trades_resampled_interval = df_trades_resampled_interval[START_INTERVAL:END_INTERVAL]
df_trades_resampled_interval_sell = df_trades_resampled_interval[df_trades_resampled_interval.taker_side == 'SELL'].resample(RESAMPLE_TIME).max()
df_trades_resampled_interval_sell.drop(columns=['time_exchange', 'time_coinapi', 'guid'], inplace=True)
df_trades_resampled_interval_sell.head()