in src/backend/domain/services/charts.py [0:0]
def visualize_trading_nodes(data_by_symbol, trading_stats_by_symbol, long_alert, short_alert, title, num_cols, periodicity):
num_plots = len(trading_stats_by_symbol)
figs = []
for title in trading_stats_by_symbol:
thickness = get_thickness(3)
vertical_spacing = get_vertical_spacing(thickness)
fig = make_subplots(
rows=3,
cols=1,
subplot_titles=[title + ".close", title + ".pnl", title + ".market_value"],
horizontal_spacing=0.1,
vertical_spacing=vertical_spacing,
shared_xaxes=False,
)
k = 0
value = data_by_symbol[title]["close"]
fig.add_trace(
go.Scatter(x=value.index, y=value.values, name=title),
row=1,
col=1,
)
fig.add_trace(
go.Scatter(
x=value[long_alert[title]].index,
y=value[long_alert[title]].values,
name=title + ".buy_alert",
mode="markers",
marker=dict(color="green", size=6, symbol="arrow-up", line_width=1),
),
row=1,
col=1,
)
fig.add_trace(
go.Scatter(
x=value[short_alert[title]].index,
y=value[short_alert[title]].values,
name=title + ".sell_alert",
mode="markers",
marker=dict(color="red", size=6, symbol="arrow-down", line_width=1),
),
row=1,
col=1,
)
pnl_value = trading_stats_by_symbol[title]["acct_ccy_pnl"]
fig.add_trace(
go.Scatter(x=pnl_value.index, y=pnl_value.values, name=title),
row=2,
col=1,
)
acc_value = trading_stats_by_symbol[title]["acct_ccy_value"]
fig.add_trace(
go.Scatter(x=acc_value.index, y=acc_value.values, name=title),
row=3,
col=1,
)
k += 1
simple_updatemenus = [
dict(
buttons=list(
[
dict(
args=[
{
"type": ["scatter", "scatter", "scatter"],
"x": [fig.data[0]["x"], fig.data[1]["x"], fig.data[2]["x"]],
},
{"yaxis.type": "linear", "xaxis.type": "date"},
[0, 1, 2],
],
label="Line plot",
method="update",
),
dict(
args=[
{
"type": ["scatter", "scatter", "scatter"],
"x": [fig.data[0]["x"], fig.data[1]["x"], fig.data[2]["x"]],
},
{"yaxis.type": "log", "xaxis.type": "date"},
[0, 1, 2],
],
label="Log plot",
method="update",
),
dict(
args=[
{"type": ["histogram", "histogram", "histogram"], "x": [None, None, None]},
{"yaxis.type": "linear", "xaxis.type": "category"},
[0, 1, 2],
],
label="Histogram plot",
method="update",
),
]
),
direction="down",
showactive=True,
xanchor="left",
y=get_update_menus_position(vertical_spacing, 3, 0),
x=0,
),
]
simple_updatemenus.append(
dict(
buttons=list(
[
dict(
args=[
{
"type": ["scatter"],
"x": [fig.data[3]["x"]],
},
{"yaxis2.type": "linear", "xaxis2.type": "date"},
[3],
],
label="Line plot",
method="update",
),
dict(
args=[
{
"type": ["scatter"],
"x": [fig.data[3]["x"]],
},
{"yaxis2.type": "log", "xaxis2.type": "date"},
[3],
],
label="Log plot",
method="update",
),
dict(
args=[
{"type": ["histogram"], "x": [None]},
{"yaxis2.type": "linear", "xaxis2.type": "category"},
[3],
],
label="Histogram plot",
method="update",
),
]
),
direction="down",
showactive=True,
xanchor="left",
y=get_update_menus_position(vertical_spacing, 3, 1),
x=0,
),
)
simple_updatemenus.append(
dict(
buttons=list(
[
dict(
args=[
{
"type": ["scatter"],
"x": [fig.data[4]["x"]],
},
{"yaxis3.type": "linear", "xaxis3.type": "date"},
[4],
],
label="Line plot",
method="update",
),
dict(
args=[
{
"type": ["scatter"],
"x": [fig.data[4]["x"]],
},
{"yaxis3.type": "log", "xaxis3.type": "date"},
[4],
],
label="Log plot",
method="update",
),
dict(
args=[
{"type": ["histogram"], "x": [None]},
{"yaxis3.type": "linear", "xaxis3.type": "category"},
[4],
],
label="Histogram plot",
method="update",
),
]
),
direction="down",
showactive=True,
xanchor="left",
y=get_update_menus_position(vertical_spacing, 3, 2),
x=0,
),
)
fig.update_layout(
updatemenus=simple_updatemenus,
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
margin=dict(l=0, r=30, t=20, b=0),
# title=title,
width=650 * 1 / num_cols,
height=600,
)
n_iter = (value.index.shape[0] - 1) // 5 + 1
ttext = value.index[::n_iter].tolist()
if periodicity == "1day":
for index, date in enumerate(ttext):
ttext[index] = date.strftime("%Y-%m-%d")
fig.for_each_xaxis(lambda x: x.update(type="date", rangeslider={"thickness": thickness, "yaxis": {"rangemode": "auto"}}))
figs.append(fig)
return figs