教程

多时间周期分析

对齐、聚合和分析不同时间周期的数据

会员内容预览(英文)

Focusing on a single timeframe 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 timeframes (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 timeframe, 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 timeframes is called resampling. There are two types of resampling: upsampling and downsampling.

Upsampling converts a time series to a shorter timeframe 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 timeframe 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 timeframes 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 timeframe we want to analyze?" Once we answer this question, we need to pull data with that exact granularity. For example, to work with the timeframes 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 timeframes.

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"
)
Period 18/18

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 vectorbtpro.data.custom.hdf.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 vectorbtpro.base.resampling.base.Resampler and then use vectorbtpro.base.resampling.base.Resampler.index_difference with reverse=True:

✅ 了解重采样,以及如何正确更改已获取数据的时间周期。

✅ 学习对齐具有不同时间周期的多条时间序列。我们将重点讨论如何在此过程中避免前视偏差,以及如何构建多时间周期(MTF)指标与图表。

✅ 聚合是发现更大尺度趋势的有力方法。学习高效聚合数据,以及如何先使用最细粒度的数据建立投资组合模型,再降采样进行分析 ☄️

会员主题(英文内容)

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

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