文档

数据

获取、组织、更新和管理市场数据

会员内容预览(英文)

VBT works with Pandas and NumPy arrays, but where do these arrays come from? Obtaining financial data manually can be challenging, especially when an exchange provides only a single batch of data at a time. In such situations, users need to handle tasks like iterating over time ranges, concatenating results, and aligning indexes and columns themselves. This process becomes even more complex when working with multiple symbols.

To simplify data retrieval and management, VBT implements the vectorbtpro.data.base.Data class, which streamlines the handling of features (such as OHLC) and symbols (such as "BTC-USD"). This is a semi-abstract class, so you need to create a subclass and define custom logic in a few places to fully access its advanced features. Fortunately, a set of custom data classes is already available, but it is helpful to understand how to create your own data class as well.

The following graph illustrates the steps discussed below:

Fetching

The vectorbtpro.data.base.Data class defines an abstract class method vectorbtpro.data.base.Data.fetch_symbol for generating, loading, or fetching data for a single symbol from any data source. You must override and implement this method yourself so that it returns a single (Pandas or NumPy) array based on certain parameters, such as the start date, end date, and frequency.

Here is a function that retrieves data for any symbol from Yahoo Finance using yfinance:

from vectorbtpro import *

def get_yf_symbol(symbol, period="max", start=None, end=None, **kwargs):
    import yfinance as yf
    if start is not None:
        start = vbt.local_datetime(start)  
    if end is not None:
        end = vbt.local_datetime(end)
    return yf.Ticker(symbol).history(
        period=period,
        start=start,
        end=end,
        **kwargs
    )

get_yf_symbol("BTC-USD", start="2020-01-01", end="2020-01-05")
                                  Open         High          Low        Close  \
Date
2019-12-31 00:00:00+00:00  7294.438965  7335.290039  7169.777832  7193.599121
2020-01-01 00:00:00+00:00  7194.892090  7254.330566  7174.944336  7200.174316
2020-01-02 00:00:00+00:00  7202.551270  7212.155273  6935.270020  6985.470215
2020-01-03 00:00:00+00:00  6984.428711  7413.715332  6914.996094  7344.884277
2020-01-04 00:00:00+00:00  7345.375488  7427.385742  7309.514160  7410.656738

                                Volume  Dividends  Stock Splits
Date
2019-12-31 00:00:00+00:00  21167946112        0.0           0.0
2020-01-01 00:00:00+00:00  18565664997        0.0           0.0
2020-01-02 00:00:00+00:00  20802083465        0.0           0.0
2020-01-03 00:00:00+00:00  28111481032        0.0           0.0
2020-01-04 00:00:00+00:00  18444271275        0.0           0.0

Why does the returned data start from 2019-12-31 instead of 2020-01-01? The start and end dates you provide are in your local timezone and are then converted to UTC. For example, in the Europe/Berlin timezone, depending on the time of year, 2020-01-01 becomes 2019-12-31 22:00:00 UTC, which is the date Yahoo Finance actually receives. To specify a date directly in UTC, append "UTC": 2020-01-01 UTC, or use a proper Timestamp instance.

Using the Pandas format is convenient for a single symbol, but what if you want data for multiple symbols? Remember, VBT expects you to provide each feature, such as open price or high price, as a separate variable. Each variable should have symbols as columns, which means you would need to fetch every symbol manually and reorganize their data. This can become especially troublesome if the symbols have mismatched indexes or columns.

Fortunately, there is a class method called vectorbtpro.data.base.Data.pull that solves most of these challenges around iterating, fetching, and merging symbols. It accepts one or multiple symbols, fetches each using vectorbtpro.data.base.Data.fetch_symbol, collects the data into a dictionary, and passes this dictionary to vectorbtpro.data.base.Data.from_data for further processing and class instantiation.

Building on the example, let's subclass vectorbtpro.data.base.Data and override the vectorbtpro.data.base.Data.fetch_symbol method to call our get_yf_symbol function:

class YFData(vbt.Data):
    @classmethod
    def fetch_symbol(cls, symbol, **kwargs):
        return get_yf_symbol(symbol, **kwargs)

You can replace get_yf_symbol with any function that returns array-like data!

That's it. YFData is now a fully functional data class capable of pulling data from Yahoo Finance and storing it:

金融数据最初的形状很少恰好符合策略需求。本节介绍 VBT 如何表示特征与交易标的、对齐时间序列,以及通过 Data 类和其专用子类管理数据集的完整生命周期。

会员主题(英文内容)

版权所有 © 2021–2026 Oleg Polakow。保留所有权利。

本站内容与文档供使用和评估 VectorBT PRO 以及教育用途。任何其他用途,包括开发或支持竞争产品或服务,均须事先取得书面同意。