教程
基础 RSI 策略
从信号到绩效,回测一个完整的 RSI 策略
会员内容预览(英文)
One of the main strengths of VBT (PRO) is its ability to quickly create and backtest multiple strategy configurations. In this introductory example, we will explore the profitability of the following RSI strategy, which is commonly used by beginners:
If the RSI is below 30, it suggests that a stock is reaching oversold conditions and may soon experience a trend reversal or a bounce back to a higher share price. Once the reversal is confirmed, a buy trade is executed. Conversely, if the RSI is above 70, it indicates that a stock is reaching overbought conditions and may experience a trend reversal or a pullback in price. After the reversal is confirmed, a sell trade is executed.
As a bonus, we will gradually expand the analysis to include multiple parameter combinations. Sound interesting? Let's get started.
Single backtest
First, let's handle the data. With a simple one-liner, we can download all available daily data for the BTC/USDT pair from Binance:
from vectorbtpro import *
data = vbt.BinanceData.pull('BTCUSDT', end='2022-08-04 UTC')
data<vectorbtpro.data.custom.binance.BinanceData at 0x7f9c40c59550>The returned object is of type vectorbtpro.data.custom.binance.BinanceData, which extends
vectorbtpro.data.base.Data to interact with the Binance API. The vectorbtpro.data.base.Data
class is VBT's built-in container for retrieving, storing, and managing data. When a DataFrame is
received, it is post-processed and stored inside the dictionary vectorbtpro.data.base.Data.data,
keyed by pair (also known as a "symbol" in VBT). You can access your DataFrame either from this
dictionary or by using the convenient vectorbtpro.data.base.Data.get method, which lets you
specify one or more columns instead of returning the entire DataFrame.
Let's plot the data using vectorbtpro.data.base.Data.plot:
data.plot().show() Another way to describe the data is by using Pandas' info method. The tabular format is very helpful for counting null values (it looks like our data does not have any—great!)
data.data['BTCUSDT'].info()<class 'pandas.DataFrame'>
DatetimeIndex: 1813 entries, 2017-08-17 00:00:00+00:00 to 2022-08-03 00:00:00+00:00
Freq: D
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Open 1813 non-null float64
1 High 1813 non-null float64
2 Low 1813 non-null float64
3 Close 1813 non-null float64
4 Volume 1813 non-null float64
5 Quote volume 1813 non-null float64
6 Trade count 1813 non-null int64
7 Taker base volume 1813 non-null float64
8 Taker quote volume 1813 non-null float64
dtypes: float64(8), int64(1)
memory usage: 141.6 KBIn our example, we will generate signals based on the opening price and execute them using the closing price. We could also place orders as soon as the signal is generated, or at a later time, but here we will show how to separate the generation of signals from their execution.
open_price = data.get('Open')
close_price = data.get('Close') Now it is time to run the indicator!
VBT supports five different RSI implementations: one written using Numba, and four more ported from
three different technical analysis libraries. Each indicator is wrapped with the versatile
vectorbtpro.indicators.factory.IndicatorFactory. 🦾
To list all available indicators or search for a specific one, use
vectorbtpro.indicators.factory.IndicatorFactory.list_indicators:
vbt.IF.list_indicators("RSI*")['vbt:RSI', 'talib:RSI', 'pandas_ta:RSI', 'ta:RSIIndicator', 'technical:RSI']You can retrieve the actual indicator class like this:
vbt.indicator("talib:RSI")vectorbtpro.indicators.factory.talib.RSIOr manually:
vbt.RSI vectorbtpro.indicators.custom.rsi.RSIvbt.talib('RSI') vectorbtpro.indicators.factory.talib.RSIvbt.ta('RSIIndicator') vectorbtpro.indicators.factory.ta.RSIIndicatorvbt.pandas_ta('RSI') vectorbtpro.indicators.factory.pandas_ta.RSIvbt.technical('RSI') vectorbtpro.indicators.factory.technical.RSIHere is a general rule for choosing an implementation:
- Use TA-Lib indicators for the fastest execution (they are written in C).
- Use VBT indicators for fast execution and plotting (compiled with Numba).
- Use indicators from other libraries if they offer more features.
To run any indicator, use the run method. To see which arguments this method accepts, pass it to
vectorbtpro.utils.formatting.phelp:
✅ 通过回测一个基础 RSI 策略,学习 VBT 的核心知识。
版权所有 © 2021–2026 Oleg Polakow。保留所有权利。
本站内容与文档供使用和评估 VectorBT PRO 以及教育用途。任何其他用途,包括开发或支持竞争产品或服务,均须事先取得书面同意。