The article “VectorBT – An Introductory Guide” first appeared on AlgoTrading101 blog.
Excerpt
What is VectorBT?
VectorBT is an open-source Python library for quantitative analysis and backtesting.
What is VectorBT used for?
VectorBT is used by algorithmic traders and investors to perform quantitative analysis, strategy testing, and research. It is built and optimized for performance and uses NumPy and Numba under the hood.
Why should I use VectorBT?
- VectorBT is open-source
- VectorBT is easy to use
- Is fast
- Integrates with Telegram
- Offers interactive charting via Jupyter notebooks
Why shouldn’t I use VectorBT?
- VectorBT could use more features
- It doesn’t have the best documentation
- If you want to get serious with VectorBT, you will need to acquire a proprietary VectorBT Pro membership that gives you access to the upgraded library
Is VectorBT free?
VectorBT is an open-source library and it is free to use. Do keep in mind that there is VectorBT Pro which is a successor of VectorBT and comes with advanced features and performance which is a premium product on an invite-only GitHub basis.
What are some VectorBT alternatives?
VectorBT can be replaced with other software that can be more suitable for your needs. Here are some of them:
- QuantConnect
- TradingView (Pine Script)
- Backtrader
- Interactive Brokers
- Thikscript
- QuantRocket
- HaasOnline
- cTrader
- Trality, and more
How to get started with VectorBT?
To get started with VectorBT, you will need to download the Python library via pip with the following command (you might want to have a new environment):
pip install -U vectorbt
If you want all the dependencies and features that VectorBT has, you will want to run this command:
pip install -U "vectorbt[full]"
If you are a fan of using Docker for your development, you have the option of spinning up a docker container that hosts a Jupyter Lab with VectorBT inside of it:
docker run --rm -p 8888:8888 -v "$PWD":/home/jovyan/work polakowo/vectorbt
The code block above pulls the latest polakowo/vectorbt
image from Docker Hub. It then starts a container running a Jupyter Notebook server and exposes the server on host port 8888.
Visiting http://127.0.0.1:8888/?token=<token>
in a browser loads JupyterLab, where the token is the secret token printed in the console.
When you’re done with using the Docker container, Docker destroys the container after the notebook server exit, but any files written to the working directory in the container remain intact in the working directory of the host.
I’ll personally use it inside of a Google Colab notebook and install it via pip.
Now that we have VectorBT ready, let us explore in the following headers what it has to offer in terms of features and performance.
How to get data with VectorBT?
To get data with VectorBT, you will need to utilize the function that connects to Yahoo Finance to download the data and provide it with the asset you wish to obtain the data. For example, let’s obtain the ETH-USD ticker closing price data:
eth_price = vbt.YFData.download('ETH-USD').get('Close') eth_price[:5]
Date 2017-11-09 00:00:00+00:00 320.884003 2017-11-10 00:00:00+00:00 299.252991 2017-11-11 00:00:00+00:00 314.681000 2017-11-12 00:00:00+00:00 307.907990 2017-11-13 00:00:00+00:00 316.716003 Freq: D, Name: Close, dtype: float64
How to use technical indicators with VectorBT?
To use technical indicators with VectorBT, you will need to use in-built functions that host indicators such as the MA, MSTD, BBANDS, RSI, and more. For example, let’s calculate a fast and slow MA for ETH and the RSI.
fast_ma = vbt.MA.run(eth_price, 10) slow_ma = vbt.MA.run(eth_price, 50) rsi = vbt.RSI.run(eth_price)
How to define entries and exists with VectorBT?
To define entries and exists with VectorBT, all you need to do is to define the logic of those conditions that need to be satisfied in order to be marked as an entry or exit.
For example, let’s define the entry to be when the fast MA crosses the slow MA while the RSI is over 50 and the exit when the slow MA crosses above the fast MA and the RSI is under 50:
entries = fast_ma.ma_crossed_above(slow_ma) & rsi.rsi_above(50) exits = slow_ma.ma_crossed_above(fast_ma) & rsi.rsi_below(50)
Note that this strategy is just an example and strategies like this almost never work in real-life.
How to perform backtesting with VectorBT?
To perform backtesting with VectorBT, you can use the Portfolio function and its modules to define the trading requirements such as the entry and exit conditions, initial cash, and more. For example, let’s implement our exits and entries from the header above and backtest them:
pf = vbt.Portfolio.from_signals(eth_price, entries, exits, init_cash=10000) pf.total_profit()
55760.53
Let us observe the overall statistics of our simple trading strategy that is only for showcase purposes by running the pf.stats()
command:
Start 2017-11-09 00:00:00+00:00 End 2022-11-07 00:00:00+00:00 Period 1825 days 00:00:00 Start Value 10000.0 End Value 65760.531431 Total Return [%] 557.605314 Benchmark Return [%] 393.42466 Max Gross Exposure [%] 100.0 Total Fees Paid 0.0 Max Drawdown [%] 61.262033 Max Drawdown Duration 513 days 00:00:00 Total Trades 18 Total Closed Trades 17 Total Open Trades 1 Open Trade PnL 1156.40722 Win Rate [%] 41.176471 Best Trade [%] 318.026171 Worst Trade [%] -21.805347 Avg Winning Trade [%] 71.673003 Avg Losing Trade [%] -10.7351 Avg Winning Trade Duration 83 days 06:51:25.714285715 Avg Losing Trade Duration 18 days 14:24:00 Profit Factor 3.388864 Expectancy 3212.007307 Sharpe Ratio 0.919382 Calmar Ratio 0.746707 Omega Ratio 1.23331 Sortino Ratio 1.378526
Visit AlgoTrading101 blog to read the full article.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from AlgoTrading101 and is being posted with its permission. The views expressed in this material are solely those of the author and/or AlgoTrading101 and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.