文档
构建模块
理解支撑高级工作流程的核心抽象
会员内容预览(英文)
In the following sections, we will explore some sub-packages, modules, and especially classes that
serve as building blocks for advanced functionalities in VBT, such as
vectorbtpro.portfolio.base.Portfolio. To demonstrate this, we will gradually build a custom class,
CorrStats, which enables us to analyze the correlation between two arrays in the most efficient
and flexible way 🧠
Utilities
VBT uses a modular project structure composed of several subpackages. Each subpackage is designed for a specific area of analysis.
The vectorbtpro.utils subpackage provides a set of utilities that power every part of VBT ⚡ These
utilities are loosely connected and offer small but powerful reusable code snippets that can be used
independently of other functionality.
The main reason we avoid importing third-party packages and instead implement many utilities from scratch is to maintain full control over execution and code quality.
Formatting
VBT includes its own formatting engine that can pretty-print any Python object. It is much more
advanced than formatting with JSON because it recognizes
native Python data types and adds intelligent formatting for more structured data types, such as
np.dtype and namedtuple. In many cases, you can even convert the formatted string back into a
Python object using eval.
Let's beautify a nested dictionary using vectorbtpro.utils.formatting.prettify and then convert
the string back into an object:
from vectorbtpro import *
dct = {'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}
print(vbt.prettify(dct)){
'planet': {
'has': {
'plants': 'yes',
'animals': 'yes',
'cryptonite': 'no'
},
'name': 'Earth'
}
}eval(vbt.prettify(dct)) == dctTrueCurious why we used vbt.prettify instead of vbt.utils.formatting.prettify? Any utility that
may be useful to the end user can be accessed directly from vbt.
To see which utilities are accessible from the root of the package, visit
vectorbtpro/utils/__init__.py
or any other subpackage, and look for the objects that are listed in __all__.
The vectorbtpro.utils.formatting.Prettified class implements the abstract method
vectorbtpro.utils.formatting.Prettified.prettify, which can be overridden by a subclass to
pretty-print an instance using vectorbtpro.utils.formatting.prettify. Read below to learn how this
method can be used to introspect instances of various classes.
Pickling
Pickling is the process of converting a Python object into a byte stream to store it in a file or
database. The vectorbtpro.utils.pickling.Pickleable class enables pickling of objects of any
complexity using Dill (or
pickle if Dill is not installed). Each subclass
inherits ready-to-use methods for serializing, deserializing, saving to a file, and loading from a
file. This is especially powerful because it allows us to persist objects containing any type of
data, including instances of vectorbtpro.data.base.Data and
vectorbtpro.portfolio.base.Portfolio.
Configuring
VBT relies heavily on automation driven by specifications. The specification for most repetitive tasks is usually stored in "configs," which serve as settings for specific tasks, data structures, or even classes. This approach makes most parts of VBT transparent, easily traversable, and programmatically changeable.
The vectorbtpro.utils.config.Config class is like a dictionary on steroids: it extends Python's
dict with various configuration features, such as frozen keys, read-only values, dot notation
access to keys, and nested updates. The most notable feature is the ability to reset a config to its
initial state and even create checkpoints, which is especially useful for settings. In addition,
since vectorbtpro.utils.config.Config inherits from vectorbtpro.utils.pickling.Pickleable, we
can save any configuration to disk, and by subclassing vectorbtpro.utils.formatting.Prettified we
can beautify it (this approach is used to generate the API reference):
print(vbt.Records.field_config)Config(
dtype=None,
settings={
'id': {
'name': 'id',
'title': 'Id'
},
'col': {
'name': 'col',
'title': 'Column',
'mapping': 'columns'
},
'idx': {
'name': 'idx',
'title': 'Timestamp',
'mapping': 'index'
}
}
)Configs are very common structures in VBT. There are three main types of configs (that either
subclass or partially use vectorbtpro.utils.config.Config) used throughout VBT:
本节探索构成 VBT 各种高级功能基础的子包、模块和核心类。
通过逐步构建一个自定义分析类,您将了解这些部分如何协同工作,掌握构建高效、灵活扩展所需的基础知识。
版权所有 © 2021–2026 Oleg Polakow。保留所有权利。
本站内容与文档供使用和评估 VectorBT PRO 以及教育用途。任何其他用途,包括开发或支持竞争产品或服务,均须事先取得书面同意。