Skip to content
New: Star-import, signal unraveling, Python 3.12 support, and more

#
Persistence

Any Python object can be serialized and saved to disk as a pickle file with save.

Save a dict to a file

    cache = dict(data=data, indicator=indicator, pf=pf)
    vbt.save(cache, "cache.pickle")

Important

If a file with the same name already exists, it will be overridden.

A pickle file can then be loaded back and deserialized with load.

Load the dict back

    cache = vbt.load("cache.pickle")

Note

The file can be read in another Python environment and even on another machine (such as in cloud), just make sure that the Python and package versions on both ends are approximately the same.

Pickle files usually take a considerable amount of space, to reduce it compression can be used. The most recommended compression algorithm for binary files is blosc. To later load the compressed file, pass the `compression` argument in the exact same way to the loader, or simply append the ".blosc" extension to the filename for the loader to recognize it automatically. The supported algorithms and their possible extensions are listed under `extensions` in settings.pickling.

Specify the compression explicitly

    vbt.save(cache, "cache.pickle", compression="blosc")
    cache = vbt.load("cache.pickle", compression="blosc")

Specify the compression implicitly

    vbt.save(cache, "cache.pickle.blosc")
    cache = vbt.load("cache.pickle.blosc")

Those VBT objects that subclass Pickleable can also be saved individually. Benefit: the name of the class and optionally the compression algorithm will be packed into the filename by default to simplify loading. The object can be loaded back using the `load()` method of the object's class.

Save a portfolio under 'Portfolio.pickle.blosc' and load it back

    pf.save(compression="blosc")
    pf = vbt.PF.load()

If a VBT object was saved with an older package version and upon loading with a newer version an error is thrown (for example, due to a different order of the arguments), the object can still be reconstructed by creating and registering a RecInfo instance before loading.

Reconstruct an older BinanceData instance

    def modify_state(rec_state):  # (1)!
        return vbt.RecState(
            init_args=rec_state.init_args,
            init_kwargs=rec_state.init_kwargs,
            attr_dct=rec_state.attr_dct,
        )

    rec_info = vbt.RecInfo(
        vbt.get_id_from_class(vbt.BinanceData),
        vbt.BinanceData,
        modify_state
    )
    rec_info.register()
    data = vbt.BinanceData.load()

1. Change the arguments to be passed to the class here

If there are issues with saving an instance of a specific class, set the reconstruction id `_rec_id` with any string and then reconstruct the object using this id (first argument of `RecInfo`).

Set a custom identifier to a class and reconstruct its instance using another class

    class MyClass1(vbt.Configured):  # (1)!
        _rec_id = "MyClass"
        ...

    my_obj = MyClass1()
    vbt.save(my_obj, "my_obj")

    # (2)!

    class MyClass2(vbt.Configured):
        ...

    rec_info = vbt.RecInfo("MyClass", MyClass2)
    rec_info.register()
    my_obj = vbt.load("my_obj")  # (3)!

1. `vbt.Configured` can be replaced by any other VBT class that subclasses it
2. ...restart the kernel...
3. The resulting object is of the class `MyClass2` even though we saved it as `MyClass1`!

#### Cookie consent

We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.

- Google Analytics
- GitHub

Manage settings