polakowo/vectorbt

Plots missing "close" curve

daviddwlee84 opened this issue · 0 comments

I found a weird phenomenon that the "close" curve is missing on the plot when selecting a single column from a multi-asset Portfolio (no matter whether it is grouped or not).
That is, even though the "close" curve cannot display normally, hovering the mouse on the Plotly plot can show the "close" data points.
image

After some investigation, I am guessing that might be because some nan were in the "close" data.

But this can happen easily when batch backtesting among multiple instruments, which might involve tick missing while joining them (their close price ticks) into a single DataFrame.

Here is the reproduction code and attachment:

import vectorbt as vbt

pf = vbt.Portfolio.load('debug.pkl')
fig = pf.orders.plot(column=pf.total_return(group_by=False).index[0])
fig.write_image('debug.png')
print(fig)

debug

FigureWidget({
    'data': [{'line': {'color': '#1f77b4'},
              'name': 'Close',
              'showlegend': True,
              'type': 'scatter',
              'uid': '8dd28145-6613-4427-8324-7e76a1870202',
              'x': array([datetime.time(9, 35), datetime.time(9, 35, 1), datetime.time(9, 35, 2),
                          ..., datetime.time(14, 48, 57), datetime.time(14, 48, 58),
                          datetime.time(14, 48, 59)], dtype=object),
              'y': array([    nan, 142.672,     nan, ...,     nan, 141.098,     nan])},
             {'customdata': array([[2.25000000e+02, 2.00000000e+00, 1.27705600e-02],
                                   [2.27000000e+02, 1.00000000e+00, 6.37885600e-03],
                                   [2.28000000e+02, 1.00000000e+01, 6.39051600e-02],
                                   [2.29000000e+02, 3.00000000e+00, 1.91607240e-02],
                                   [2.30000000e+02, 2.00000000e+00, 1.27618480e-02],
                                   [2.31000000e+02, 3.00000000e+00, 1.91611200e-02],
                                   [2.32000000e+02, 4.00000000e+00, 2.55138400e-02],
                                   [2.33000000e+02, 5.00000000e+00, 3.18997800e-02],
                                   [2.34000000e+02, 3.40000000e+01, 2.17003776e-01],
                                   [2.35000000e+02, 1.40000000e+01, 8.93354000e-02],
                                   [2.37000000e+02, 3.40000000e+01, 2.16994800e-01],
                                   [2.42000000e+02, 5.60000000e+01, 3.57656992e-01],
                                   [2.55000000e+02, 1.00000000e+00, 6.37964800e-03]]),
              'hovertemplate': ('Order Id: %{customdata[0]}<br>' ... '<br>Fees: %{customdata[2]:.6f}'),
              'marker': {'color': '#37B13F',
                         'line': {'color': 'rgb(38,123,44)', 'width': 1},
                         'size': 8,
                         'symbol': 'triangle-up'},
              'mode': 'markers',
              'name': 'Buy',
              'type': 'scatter',
              'uid': 'a3677859-fbd4-4a3d-b5b7-8997c6f2a921',
              'x': array([datetime.time(9, 55, 19), datetime.time(9, 55, 22),
                          datetime.time(9, 55, 25), datetime.time(9, 55, 28),
                          datetime.time(9, 55, 31), datetime.time(9, 55, 34),
                          datetime.time(9, 55, 37), datetime.time(9, 55, 40),
                          datetime.time(9, 55, 43), datetime.time(9, 55, 46),
                          datetime.time(9, 56, 1), datetime.time(9, 56, 19),
                          datetime.time(9, 57, 4)], dtype=object),
              'y': array([145.12 , 144.974, 145.239, 145.157, 145.021, 145.16 , 144.965, 144.999,
                          145.056, 145.025, 145.05 , 145.153, 144.992])},
             {'customdata': array([[2.61000000e+02, 3.00000000e+00, 1.92424320e-02],
                                   [2.62000000e+02, 9.00000000e+00, 5.77063080e-02],
                                   [2.63000000e+02, 7.00000000e+00, 4.49242640e-02],
                                   [2.64000000e+02, 8.00000000e+00, 5.13571520e-02],
                                   [2.65000000e+02, 3.00000000e+00, 1.92467880e-02],
                                   [2.67000000e+02, 1.20000000e+01, 7.70309760e-02],
                                   [2.69000000e+02, 1.00000000e+00, 6.41295600e-03],
                                   [2.71000000e+02, 1.00000000e+00, 6.41405600e-03],
                                   [2.73000000e+02, 2.00000000e+00, 1.28259120e-02],
                                   [2.75000000e+02, 3.00000000e+01, 1.92407160e-01],
                                   [2.76000000e+02, 1.80000000e+01, 1.15422912e-01],
                                   [2.77000000e+02, 1.20000000e+01, 7.68995040e-02],
                                   [2.78000000e+02, 1.00000000e+00, 6.40389200e-03],
                                   [3.13000000e+02, 5.00000000e+00, 3.20872200e-02],
                                   [3.19000000e+02, 5.70000000e+01, 3.66712236e-01]]),
              'hovertemplate': ('Order Id: %{customdata[0]}<br>' ... '<br>Fees: %{customdata[2]:.6f}'),
              'marker': {'color': '#EA4335',
                         'line': {'color': 'rgb(181,31,18)', 'width': 1},
                         'size': 8,
                         'symbol': 'triangle-down'},
              'mode': 'markers',
              'name': 'Sell',
              'type': 'scatter',
              'uid': '5e08fd46-f2b6-4349-a1e4-59e1d2ab19d1',
              'x': array([datetime.time(9, 57, 25), datetime.time(9, 57, 28),
                          datetime.time(9, 57, 31), datetime.time(9, 57, 34),
                          datetime.time(9, 57, 37), datetime.time(9, 57, 40),
                          datetime.time(9, 57, 43), datetime.time(9, 57, 46),
                          datetime.time(9, 57, 49), datetime.time(9, 57, 52),
                          datetime.time(9, 57, 55), datetime.time(9, 57, 58),
                          datetime.time(9, 58, 1), datetime.time(10, 1, 16),
                          datetime.time(10, 3, 19)], dtype=object),
              'y': array([145.776, 145.723, 145.858, 145.901, 145.809, 145.892, 145.749, 145.774,
                          145.749, 145.763, 145.736, 145.643, 145.543, 145.851, 146.217])}],
    'layout': {'height': 350,
               'legend': {'orientation': 'h',
                          'traceorder': 'normal',
                          'x': 1,
                          'xanchor': 'right',
                          'y': 1.02,
                          'yanchor': 'bottom'},
               'margin': {'b': 30, 'l': 30, 'r': 30, 't': 30},
               'template': '...',
               'width': 700}
})

Version:

  • Python: used 3.8.13 and 3.10.11 and works the same
  • vectorbt: 0.26.1
  • pandas: 1.5.3