vectorbt

repository·master·Indexed 27 days ago

https://github.com/polakowo/vectorbt

A high-performance Python library for backtesting and analyzing trading strategies at scale. It utilizes vectorized operations via NumPy, Numba, and a Rust engine to simultaneously test thousands of strategy parameter combinations. Features include large-scale strategy research, portfolio analysis, financial indicator animations, and a Dash-based application for visualizing and backtesting candlestick patterns.

Tokens
16.8K
Snippets
42
Records
101
Agent score
93%

What's inside vectorbt

  1. Overview of Candlestick Patterns app features

    master

    The Candlestick Patterns app is a Dash-based application designed to visualize and backtest candlestick patterns. Key capabilities include:

    • Data & Detection: Fetches market data via yfinance and detects patterns using TA-Lib.
    • Strategy Customization: Choose entry/exit patterns, override candle settings, or specify signals manually.
    • Backtesting: Uses VectorBT to backtest signals.
    • Visualization: Uses Plotly to display OHLCV, signals, orders, trades, and portfolio value.
    • Analysis: Displays performance metrics (e.g., Sharpe ratio) and compares strategies against 'buy & hold' and random trading.
    • UI: Responsive interface built with Dash Bootstrap Components.
  2. Understand the vectorbt license terms

    master

    vectorbt is distributed under a fair-code model using the Apache 2.0 with Commons Clause license.

    • Permitted: Individuals and organizations can use the source code and software for free.
    • Restricted: You are not allowed to sell products or services that consist primarily of this software.

    If you require a license exception, you must contact the author at olegpolakow@vectorbt.pro.

    Note: Installing optional dependencies may be subject to more restrictive licenses than the core vectorbt package.

  3. Understand the vectorbt License and Commons Clause

    master

    The vectorbt software is licensed under the Apache 2.0 license, but it is subject to the Commons Clause License Condition v1.0.

    Under this condition, you are granted the rights provided by the Apache 2.0 license except for the right to Sell the software.

    Definition of "Sell": Practicing any or all of the rights granted under the license to provide to third parties, for a fee or other consideration (including hosting or consulting/support services), a product or service whose value derives, entirely or substantially, from the functionality of the software.

    If you use the software, any license notice or attribution required by the Apache 2.0 license must also include this Commons Clause License Condition notice.

  4. License Information: GNU Affero General Public License v3

    master

    This project is licensed under the GNU Affero General Public License (AGPL) version 3.

    Key implications for users and developers:

    • Network Interaction Requirement: If you modify the program and run it on a network server, you must provide the modified source code to the users of that server.
    • Copyleft: Any modified versions or works based on this program must also be licensed under the AGPL v3.
    • No Warranty: The software is provided "as is" without any warranty of any kind.
    • Limitation of Liability: The copyright holders and contributors are not liable for any damages arising from the use or inability to use the program.
  5. Perform Trade and Drawdown Analysis

    master

    You can perform retrospective analysis on trades (entry/exit) and positions. Additionally, you can perform drawdown analysis on any numeric time series using .vbt.drawdowns.

    # Get projected returns of buy orders
    >>> entry_trades = vbt.Portfolio.from_random_signals(price, n=5).entry_trades
    >>> returns_pd = entry_trades.returns.to_pd()
    
    # Plot deepest price dips
    >>> price.vbt.drawdowns.plot(top_n=3).show()
  6. Learn VectorBT via external articles and guides

    master

    VectorBT has a wide range of community and official resources for learning different aspects of the library, from beginner introductions to advanced optimization and pattern recognition.

    Beginner Guides

    • AlgoTrading101: Introduces VectorBT concepts, examples, and common workflows.
    • Trading Dude (Medium): A beginner-oriented walkthrough of setting up and running backtests.
    • Plain English: Guide on building your first algorithmic trading model in Python.

    Advanced Workflows & Optimization

    • Greyhound Analytics: Covers optimizing entry/exit points, creating custom indicators via IndicatorFactory, using multiple indicators, and plotting custom Plotly graphs.
    • PyQuantLab: Focuses on minimal grid searches using MultiIndex signals, parameter sweeping (e.g., EMA-ATR breakout), and specific strategies like AO Twin Peaks.
    • Tobi Lux (Medium): A multi-part 'Backtesting Cookbook' series covering practical backtesting patterns.

    Performance & Scale

    • PyQuant News: Demonstrates large-scale simulations (e.g., 1,000,000 backtests in 20 seconds).
    • Coding Nexus / AIMonks: Explanations of VectorBT's ultra-fast vectorized approach and its role in quantitative research.
  7. Accelerate Pandas operations with VectorBT

    master

    VectorBT provides a custom accessor .vbt on top of Pandas objects to access compiled, high-performance versions of common operations like mapping, reducing, rolling, grouping, and resampling. These operations use NumPy, Numba, and optional Rust kernels for significant speedups over standard Pandas.

    import vectorbt as vbt
    import pandas as pd
    import numpy as np
    from numba import njit
    
    big_ts = pd.DataFrame(np.random.uniform(size=(1000, 1000)))
    
    @njit
    def vbt_zscore_nb(i, col, x):
        return (x[-1] - np.mean(x)) / np.std(x)
    
    # Use the .vbt accessor for accelerated rolling apply
    %timeit big_ts.vbt.rolling_apply(2, vbt_zscore_nb)
  8. Generate Markdown benchmark reports

    master

    To generate comprehensive Markdown reports, run bench_matrix.py. This produces three files by default: BENCHMARKS.md, BENCHMARKS_NUMBA.md, and BENCHMARKS_RUST.md. You can specify a custom output path for the speedup report using --output.

    python benchmarks/bench_matrix.py --output benchmarks/BENCHMARKS_LOCAL.md
  9. Add new benchmark cases

    master

    To add new benchmarks, add cases to make_cases in bench_engine.py after ensuring Rust implementations and dispatch tests are stable.

    Requirements for new cases:

    • Must be deterministic.
    • Must be representative of public dispatch behavior.
    • Must be cheap enough to run across the full matrix.
    • Must be explicit about cases where parity cannot be exact.
    • Must be tagged if they should be excluded from the core suite.

    After adding cases, run a targeted check:

    python benchmarks/bench_engine.py --pattern <subpackage-or-function> --check

    Then regenerate reports:

    python benchmarks/bench_matrix.py