Tutorials
MTF analysis
Align, aggregate, and analyze data across multiple time frames
Focusing on a single time frame can cause us to miss the broader trend, overlook important support and resistance levels, and ignore high-probability entry and stop points. By observing the same pair across different time frames (or time compressions), we can identify the overall movement of an asset (the trend is your friend) and spot key chart patterns. In fact, all technical indicators can yield different results depending on the time frame, and combining these results can provide a more complete view of the market we are trading in.
Since VBT works with time series, the main operation that allows us to switch between different time frames is called resampling. There are two types of resampling: upsampling and downsampling.
Upsampling converts a time series to a shorter time frame that has a higher frequency, such as changing daily prices to hourly prices. The "up" prefix means there is an increase in the number of data points. This operation does not result in any information loss because no data is removed, just re-indexed: the value for each day appears at the very first hour in the upsampled array, while all other hours have NaN. By forward-filling those NaN values, you can compare any daily time series with an hourly time series!
(Reload the page if the diagram does not appear.)
Downsampling, on the other hand, converts a time series to a longer time frame with a lower frequency, such as turning hourly prices into daily prices. The "down" prefix means there is a decrease in the number of data points. Unlike upsampling, downsampling causes information loss because multiple pieces of data are aggregated into one. That's why time frames are also called time compressions. Even though we lose some information, we can now see a broader trend!
Downsampling is similar to a moving average, as both aggregate data at each time step to show a broader trend.
Data
Before pulling any data, we should ask ourselves: "What is the shortest time frame we want to analyze?"
Once we answer this question, we need to pull data with that exact granularity. For example, to work
with the time frames H1 (1 hour), H4 (4 hours), and D1 (1 day), we need data at least as
granular as H1, which can later be downsampled to get the H4 and D1 time frames.
This does not work the other way around: we cannot upsample H4 or D1 data to generate H1
because most data points would just become NaN.
from vectorbtpro import *
h1_data = vbt.BinanceData.pull(
"BTCUSDT",
start="2020-01-01 UTC",
end="2021-01-01 UTC",
timeframe="1h"
)Let's persist the data locally to avoid re-fetching it every time we start a new runtime:
h1_data.to_hdf()We can then easily access the saved data using HDFData:
h1_data = vbt.HDFData.pull("BinanceData.h5")Let's look at the index of the data:
h1_data.wrapper.index DatetimeIndex(['2020-01-01 00:00:00+00:00', '2020-01-01 01:00:00+00:00',
'2020-01-01 02:00:00+00:00', '2020-01-01 03:00:00+00:00',
'2020-01-01 04:00:00+00:00', '2020-01-01 05:00:00+00:00',
...
'2020-12-31 18:00:00+00:00', '2020-12-31 19:00:00+00:00',
'2020-12-31 20:00:00+00:00', '2020-12-31 21:00:00+00:00',
'2020-12-31 22:00:00+00:00', '2020-12-31 23:00:00+00:00'],
dtype='datetime64[ns, UTC]', name='Open time', length=8766, freq=None)As expected, the index starts at midnight on January 1 and ends at 11 PM on December 31.
But why is freq=None? Pandas was not able to infer the frequency because some data
points are missing. This often happens when the exchange experiences downtime. To get all
the missing indices, we can create a resampler using Resampler
and then use Resampler.index_difference
with reverse=True:
✅ Learn about resampling and how to properly change the time frame of retrieved data.
✅ Learn how to align multiple time series with different time frames. In particular, we will discuss how to avoid look-ahead bias when doing this, and how to build multiple time frame (MTF) indicators and graphs.
✅ Aggregation is a powerful technique for discovering larger trends. Learn how to aggregate data effectively, and how to model a portfolio using the most granular data, then downsample it afterward for analysis ☄️
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.