All Projects → polakowo → Vectorbt

polakowo / Vectorbt

Licence: apache-2.0
Ultimate Python library for time series analysis and backtesting at scale

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Vectorbt

Aat
Asynchronous, event-driven algorithmic trading in Python and C++
Stars: ✭ 109 (-87.25%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading, trading-strategies
51bitquant
51bitquant Python数字货币量化交易视频 CCXT框架 爬取交易所数据 比特币量化交易 交易机器人51bitquant tradingbot cryptocurrency quantitative trading btc trading
Stars: ✭ 284 (-66.78%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading, trading-strategies
Roq Api
API for algorithmic and high-frequency trading
Stars: ✭ 132 (-84.56%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading, trading-strategies
Jesse
An advanced crypto trading bot written in Python
Stars: ✭ 1,038 (+21.4%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading, trading-strategies
Socktrader
🚀 Websocket based trading bot for 💰cryptocurrencies 📈
Stars: ✭ 152 (-82.22%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading, trading-strategies
Cryptocurrency Analysis Python
Open-Source Tutorial For Analyzing and Visualizing Cryptocurrency Data
Stars: ✭ 278 (-67.49%)
Mutual labels:  cryptocurrency, data-analysis, data-visualization
Pydataroad
open source for wechat-official-account (ID: PyDataLab)
Stars: ✭ 302 (-64.68%)
Mutual labels:  data-analysis, data-mining, data-visualization
Quantitative Notebooks
Educational notebooks on quantitative finance, algorithmic trading, financial modelling and investment strategy
Stars: ✭ 356 (-58.36%)
Mutual labels:  data-analysis, algorithmic-trading, trading-strategies
Introneuralnetworks
Introducing neural networks to predict stock prices
Stars: ✭ 486 (-43.16%)
Mutual labels:  trading, algorithmic-trading, trading-strategies
Crypto Signal
Github.com/CryptoSignal - #1 Quant Trading & Technical Analysis Bot - 3,100+ stars, 900+ forks
Stars: ✭ 3,690 (+331.58%)
Mutual labels:  trading, trading-strategies, algorithmic-trading
Stocksharp
Algorithmic trading and quantitative trading open source platform to develop trading robots (stock markets, forex, crypto, bitcoins, and options).
Stars: ✭ 4,601 (+438.13%)
Mutual labels:  cryptocurrency, trading, trading-strategies
Sgx Full Orderbook Tick Data Trading Strategy
Providing the solutions for high-frequency trading (HFT) strategies using data science approaches (Machine Learning) on Full Orderbook Tick Data.
Stars: ✭ 733 (-14.27%)
Mutual labels:  trading, algorithmic-trading, trading-strategies
Knowage Server
Knowage is the professional open source suite for modern business analytics over traditional sources and big data systems.
Stars: ✭ 276 (-67.72%)
Mutual labels:  data-analysis, data-mining, data-visualization
Wolfbot
Crypto currency trading bot written in TypeScript for NodeJS
Stars: ✭ 335 (-60.82%)
Mutual labels:  cryptocurrency, trading, trading-strategies
Tai
A composable, real time, market data and trade execution toolkit. Built with Elixir, runs on the Erlang virtual machine
Stars: ✭ 264 (-69.12%)
Mutual labels:  cryptocurrency, trading, algorithmic-trading
Quantdom
Python-based framework for backtesting trading strategies & analyzing financial markets [GUI ]
Stars: ✭ 449 (-47.49%)
Mutual labels:  trading, algorithmic-trading, trading-strategies
Alphapy
Automated Machine Learning [AutoML] with Python, scikit-learn, Keras, XGBoost, LightGBM, and CatBoost
Stars: ✭ 564 (-34.04%)
Mutual labels:  cryptocurrency, trading, trading-strategies
Data Science With Ruby
Practical Data Science with Ruby based tools.
Stars: ✭ 549 (-35.79%)
Mutual labels:  data-analysis, data-mining, data-visualization
Kelp
Kelp is a free and open-source trading bot for the Stellar DEX and 100+ centralized exchanges
Stars: ✭ 580 (-32.16%)
Mutual labels:  cryptocurrency, trading, trading-strategies
Freqtrade Strategies
Free trading strategies for Freqtrade bot
Stars: ✭ 697 (-18.48%)
Mutual labels:  cryptocurrency, trading, trading-strategies

Build Status Code Coverage Website PyPi Downloads License Gitter Binder Patreon

vectorbt

Logo

vectorbt is a backtesting library on steroids - it operates entirely on pandas and NumPy objects, and is accelerated by Numba to analyze time series at speed and scale 🔥

In contrast to conventional libraries, vectorbt represents any data as nd-arrays. This enables superfast computation using vectorized operations with NumPy and non-vectorized but dynamically compiled operations with Numba. It also integrates plotly.py and ipywidgets to display complex charts and dashboards akin to Tableau right in the Jupyter notebook. Due to high performance, vectorbt is able to process large amounts of data even without GPU and parallelization, and enable the user to interact with data-hungry widgets without significant delays.

With vectorbt you can

  • Analyze time series and engineer new features
  • Supercharge pandas and your favorite tools to run much faster
  • Test many trading strategies, configurations, assets, and periods in one go
  • Test machine learning models
  • Build interactive charts/dashboards without leaving Jupyter

Installation

pip install vectorbt

To also install optional dependencies:

pip install vectorbt[full]

Troubleshooting:

Examples

You can start backtesting with just a couple of lines.

Here is how much profit we would have made if we invested $100 into Bitcoin in 2014 and held (Note: first time compiling with Numba may take a while):

import vectorbt as vbt

price = vbt.YFData.download('BTC-USD').get('Close')

portfolio = vbt.Portfolio.from_holding(price, init_cash=100)
portfolio.total_profit()
8412.436065824717

The crossover of 10-day SMA and 50-day SMA under the same conditions:

fast_ma = vbt.MA.run(price, 10)
slow_ma = vbt.MA.run(price, 50)
entries = fast_ma.ma_above(slow_ma, crossover=True)
exits = fast_ma.ma_below(slow_ma, crossover=True)

portfolio = vbt.Portfolio.from_signals(price, entries, exits, init_cash=100)
portfolio.total_profit()
12642.617149066731

Quickly assessing the performance of 1000 random signal strategies on BTC and ETH:

import numpy as np

symbols = ["BTC-USD", "ETH-USD"]
price = vbt.YFData.download(symbols, missing_index='drop').get('Close')

n = np.random.randint(10, 101, size=1000).tolist()
portfolio = vbt.Portfolio.from_random_signals(price, n=n, init_cash=100, seed=42)

mean_expectancy = portfolio.trades.expectancy().groupby(['rand_n', 'symbol']).mean()
fig = mean_expectancy.unstack().vbt.scatterplot(xaxis_title='rand_n', yaxis_title='mean_expectancy')
fig.show()

rand_scatter.png

For fans of hyperparameter optimization, here is a snippet for testing 10000 window combinations of a dual SMA crossover strategy on BTC, USD and LTC:

symbols = ["BTC-USD", "ETH-USD", "LTC-USD"]
price = vbt.YFData.download(symbols, missing_index='drop').get('Close')

windows = np.arange(2, 101)
fast_ma, slow_ma = vbt.MA.run_combs(price, window=windows, r=2, short_names=['fast', 'slow'])
entries = fast_ma.ma_above(slow_ma, crossover=True)
exits = fast_ma.ma_below(slow_ma, crossover=True)

portfolio_kwargs = dict(size=np.inf, fees=0.001, freq='1D')
portfolio = vbt.Portfolio.from_signals(price, entries, exits, **portfolio_kwargs)

fig = portfolio.total_return().vbt.heatmap(
    x_level='fast_window', y_level='slow_window', slider_level='symbol', symmetric=True,
    trace_kwargs=dict(colorbar=dict(title='Total return', tickformat='%')))
fig.show()

dmac_heatmap.gif

Digging into each strategy configuration is as simple as indexing with pandas:

portfolio[(10, 20, 'ETH-USD')].stats()
Start                            2015-08-07 00:00:00
End                              2021-03-12 00:00:00
Duration                          2041 days 00:00:00
Init. Cash                                       100
Total Profit                                  679765
Total Return [%]                              679765
Benchmark Return [%]                         63250.5
Position Coverage [%]                         55.463
Max. Drawdown [%]                             70.735
Avg. Drawdown [%]                            12.0311
Max. Drawdown Duration             760 days 00:00:00
Avg. Drawdown Duration    28 days 20:03:34.925373134
Num. Trades                                       49
Win Rate [%]                                  55.102
Best Trade [%]                                1075.8
Worst Trade [%]                             -29.5934
Avg. Trade [%]                               49.0573
Max. Trade Duration                 80 days 00:00:00
Avg. Trade Duration       23 days 01:28:09.795918367
Expectancy                                   14440.6
SQN                                          1.70339
Gross Exposure                               0.55463
Sharpe Ratio                                 2.16534
Sortino Ratio                                3.81088
Calmar Ratio                                 5.43694
Name: (10, 20, ETH-USD), dtype: object
portfolio[(10, 20, 'ETH-USD')].plot().show()

dmac_portfolio.png

It's not all about backtesting - vectorbt can be used to facilitate financial data analysis and visualization. Let's generate a GIF that animates the %B and bandwidth of Bollinger Bands for different symbols:

symbols = ["BTC-USD", "ETH-USD", "ADA-USD"]
price = vbt.YFData.download(symbols, period='6mo', missing_index='drop').get('Close')
bbands = vbt.BBANDS.run(price)

def plot(index, bbands):
    bbands = bbands.loc[index]
    fig = vbt.make_subplots(
            rows=5, cols=1, shared_xaxes=True, 
            row_heights=[*[0.5 / 3] * len(symbols), 0.25, 0.25], vertical_spacing=0.05,
            subplot_titles=(*symbols, '%B', 'Bandwidth'))
    fig.update_layout(template=vbt.settings.dark_template, showlegend=False, width=750, height=650)
    for i, symbol in enumerate(symbols):
        bbands.close[symbol].vbt.lineplot(add_trace_kwargs=dict(row=i + 1, col=1), fig=fig)
    bbands.percent_b.vbt.ts_heatmap(
        trace_kwargs=dict(zmin=0, zmid=0.5, zmax=1, colorscale='Spectral', colorbar=dict(
            y=(fig.layout.yaxis4.domain[0] + fig.layout.yaxis4.domain[1]) / 2, len=0.2
        )), add_trace_kwargs=dict(row=4, col=1), fig=fig)
    bbands.bandwidth.vbt.ts_heatmap(
        trace_kwargs=dict(colorbar=dict(
            y=(fig.layout.yaxis5.domain[0] + fig.layout.yaxis5.domain[1]) / 2, len=0.2
        )), add_trace_kwargs=dict(row=5, col=1), fig=fig)
    return fig

vbt.save_animation('bbands.gif', bbands.wrapper.index, plot, bbands, delta=90, step=3, fps=3)
100%|██████████| 31/31 [00:21<00:00,  1.21it/s]

bbands.gif

Motivation

While there are many great backtesting packages for Python, vectorbt combines an extremely fast backtester and a data science tool: it excels at processing performance and offers interactive tools to explore complex phenomena in trading. With it, you can traverse a huge number of strategy configurations, time periods, and instruments in little time, to explore where your strategy performs best and to uncover hidden patterns in data.

How it works?

vectorbt combines pandas, NumPy and Numba sauce to obtain orders-of-magnitude speedup over other libraries. It natively works on pandas objects, while performing all computations using NumPy and Numba under the hood. This way, it is often much faster than pandas alone:

>>> import numpy as np
>>> import pandas as pd
>>> import vectorbt as vbt

>>> big_ts = pd.DataFrame(np.random.uniform(size=(1000, 1000)))

# pandas
>>> %timeit big_ts.expanding().max()
48.4 ms ± 557 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

# vectorbt
>>> %timeit big_ts.vbt.expanding_max()
8.82 ms ± 121 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In contrast to most other similar backtesting libraries where backtesting is limited to simple arrays (price, signals, etc.), vectorbt is optimized for working with multi-dimensional data: it treats index of a DataFrame as time axis and columns as distinct features that should be backtest, and performs computations on the entire matrix at once, without slow Python loops.

To make the library easier to use, vectorbt introduces a namespace (accessor) to pandas objects (see extending pandas). This way, user can easily switch between pandas and vectorbt functionality. Moreover, each vectorbt method is flexible towards inputs and can work on both Series and DataFrames.

Features

  • Extends pandas using a custom vbt accessor -> Compatible with any library
  • For high performance, most operations are done strictly using NumPy and Numba -> Much faster than comparable operations in pandas
# pandas
>>> %timeit big_ts + 1
242 ms ± 3.58 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# vectorbt
>>> %timeit big_ts.vbt + 1
3.32 ms ± 19.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  • Functions for combining, transforming, and indexing NumPy and pandas objects
    • NumPy-like broadcasting for pandas, among other features
# pandas
>>> pd.Series([1, 2, 3]) + pd.DataFrame([[1, 2, 3]])
   0  1  2
0  2  4  6

# vectorbt
>>> pd.Series([1, 2, 3]).vbt + pd.DataFrame([[1, 2, 3]])
   0  1  2
0  2  3  4
1  3  4  5
2  4  5  6
  • Compiled versions of common pandas functions, such as rolling, groupby, and resample
# pandas
>>> %timeit big_ts.rolling(2).apply(np.mean, raw=True)
7.32 s ± 431 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# vectorbt
>>> mean_nb = njit(lambda col, i, x: np.mean(x))
>>> %timeit big_ts.vbt.rolling_apply(2, mean_nb)
86.2 ms ± 7.97 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
  • Splitting functions for time series cross-validation
>>> pd.Series([1, 2, 3, 4, 5]).vbt.expanding_split()[0]
split_idx    0    1    2    3  4
0          1.0  1.0  1.0  1.0  1
1          NaN  2.0  2.0  2.0  2
2          NaN  NaN  3.0  3.0  3
3          NaN  NaN  NaN  4.0  4
4          NaN  NaN  NaN  NaN  5
  • Drawdown analysis
>>> pd.Series([2, 1, 3, 2]).vbt.drawdowns.plot().show()

drawdowns.png

  • Functions for working with signals
    • Entry, exit, and random signal generators
    • Ranking and distance functions
>>> pd.Series([False, True, True, True]).vbt.signals.first()
0    False
1     True
2    False
3    False
dtype: bool
  • Signal factory for building iterative signal generators
    • Includes basic generators such for random signal generation
>>> rand = vbt.RAND.run(n=[0, 1, 2], input_shape=(6,), seed=42)
>>> rand.entries
rand_n      0      1      2
0       False   True   True
1       False  False  False
2       False  False  False
3       False  False   True
4       False  False  False
5       False  False  False
>>> rand.exits
rand_n      0      1      2
0       False  False  False
1       False  False   True
2       False  False  False
3       False   True  False
4       False  False   True
5       False  False  False
  • Functions for working with returns
    • Numba-compiled versions of metrics found in empyrical
    • Rolling versions of most metrics
>>> pd.Series([0.01, -0.01, 0.01]).vbt.returns(freq='1D').sharpe_ratio()
5.515130702591433
  • Class for modeling portfolios
    • Accepts signals, orders, and custom order function
    • Supports long and short positions
    • Supports individual and multi-asset mixed portfolios
    • Offers metrics and tools for analyzing returns, orders, trades and positions
    • Allows saving and loading from disk using dill
>>> price = [1., 2., 3., 2., 1.]
>>> entries = [True, False, True, False, False]
>>> exits = [False, True, False, True, False]
>>> portfolio = vbt.Portfolio.from_signals(price, entries, exits, freq='1D')
>>> portfolio.trades.plot().show()

trades.png

  • Indicator factory for building complex technical indicators with ease
    • Includes technical indicators with full Numba support
      • Moving average, Bollinger Bands, RSI, Stochastic, MACD, and more
    • Each indicator has methods for generating signals and plotting
    • Each indicator takes arbitrary parameter combinations, from arrays to Cartesian products
    • Supports TA-Lib indicators out of the box
    • Can integrate third-party indicators, such as pandas-ta
    • Supports parallelization with Ray
>>> SMA = vbt.IndicatorFactory.from_talib('SMA')
>>> SMA.run([1., 2., 3., 4., 5.], timeperiod=[2, 3]).real
sma_timeperiod    2    3
0               NaN  NaN
1               1.5  NaN
2               2.5  2.0
3               3.5  3.0
4               4.5  4.0
  • Label generation for machine learning
    • Labeling based on local extrema, breakouts, and more
>>> price = np.cumprod(np.random.uniform(-0.1, 0.1, size=100) + 1)
>>> vbt.LEXLB.run(price, 0.2, 0.2).plot().show()

local_extrema.png

  • Supports yfinance, python-binance, and synthetic data generation
  • Interactive Plotly-based widgets for visual data analysis

Resources

Head over to the documentation to get started.

Notebooks

Note: you must run the notebook to play with the widgets.

Dashboards

Articles

How to contribute

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

First, you need to install vectorbt from the repository:

pip uninstall vectorbt
git clone https://github.com/polakowo/vectorbt.git
cd vectorbt
pip install -e .

After making changes, make sure you did not break any functionality:

pytest

Please make sure to update tests as appropriate.

Disclaimer

This software is for educational purposes only. Do not risk money which you are afraid to lose. USE THE SOFTWARE AT YOUR OWN RISK. THE AUTHORS AND ALL AFFILIATES ASSUME NO RESPONSIBILITY FOR YOUR TRADING RESULTS.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].