Documentation

Data

Retrieve, organize, update, and manage market data

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 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 Data class defines an abstract class method 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 Data.pull that solves most of these challenges around iterating, fetching, and merging symbols. It accepts one or multiple symbols, fetches each using Data.fetch_symbol, collects the data into a dictionary, and passes this dictionary to Data.from_data for further processing and class instantiation.

Building on the example, let's subclass Data and override the 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)

Financial data rarely arrives in the shape a strategy needs. This section explains how VBT represents features and symbols, aligns time series, and manages the full lifecycle of a dataset through the Data class and its specialized subclasses.

Topics

Copyright © 2021–2026 Oleg Polakow. All rights reserved.

Site content and documentation are provided for using and evaluating VectorBT PRO and for educational purposes. Any other use, including building or supporting competing products or services, requires prior written consent.