Code Monkey home page Code Monkey logo

vectorbt's Introduction

vectorbt

Made by Vectors Market

This package shares a similar idea as behind most other Python backtesting packages, but designed especially for fast strategy backtesting, tuning and comparison at scale.

It builds upon numpy and Numba to obtain orders-of-magnitude speedup over pandas. Furthermore, it integrates plotly.py and ipywidgets to build interactive charts and complex dashboards. Due to its high processing performance, vectorbt is able to re-calculate data on the fly, thus enabling the user to interact with data-hungry widgets without significant delays.

Here a snippet for testing 4851 window combinations of a dual SMA crossover strategy on the whole Microsoft stock history in about 5 seconds:

import vectorbt as vbt
import numpy as np
import itertools
import yfinance as yf

# Prepare data
msft = yf.Ticker("MSFT")
history = msft.history(period="max")
ohlcv = vbt.OHLCV.from_df(history)
investment = 100 # $

# Create window combinations
windows = np.arange(2, 101)
comb = itertools.combinations(np.arange(len(windows)), 2) # twice less params
fast_idxs, slow_idxs = np.asarray(list(comb)).transpose()
fast_windows, slow_windows = windows[fast_idxs], windows[slow_idxs]

# Calculate the performance of the strategy
dmac = vbt.DMAC(ohlcv.open, fast_windows, slow_windows)
entries, exits = dmac.crossover_signals()
positions = vbt.Positions.from_signals(entries, exits)
portfolio = vbt.Portfolio(ohlcv.open, positions, investment=investment)
tnp = portfolio.total_net_profit

# Plot heatmap
tnp_matrix = np.empty((len(windows), len(windows)))
tnp_matrix[fast_idxs, slow_idxs] = tnp
tnp_matrix[slow_idxs, fast_idxs] = tnp # symmetry

vbt.Heatmap(data=tnp_matrix, x_labels=windows, y_labels=windows, width=600, height=450).show_png()

msft_heatmap.png

Motivation

As data scientist and recent trader, I've been curious of how effective is technical analysis. I wanted answers to general questions like "How this strategy compares to the other ones? What technical indicators are best and what are worst for this market? Is technical analysis of any use at all, or is it just a buzzword and everything in the market is fully governed by random choice?"

To answer these and more, you need to set up experiments where you traverse thousands or even millions of parameter combinations, time ranges and markets, to see what performs best where. While there are many great backtesting libraries for Python, I found none that could handle these amounts of tests in a timely manner.

Take for example pandas: while certain array operations such as window functions are implemented using either Cython or Numba, they cannot be accessed within a user-defined Numba code. Moreover, some operations may be extremely slow compared to their NumPy counterparts:

a = np.arange(100)
s = pd.Series(a)

%timeit a[i]
1000000 loops, best of 3: 998 ns per loop

%timeit s[i]
10000 loops, best of 3: 168 µs per loop

The idea behind vectorbt is to create a backtesting library that operates entirely on NumPy arrays and is powered by Numba to deliver backtesting at scale. It also integrates Plotly to display charts and dashbaords akin to Tableau right in the Jupyter notebook.

How it works?

Each vectorbt class is a subclass of np.ndarray with a custom set of methods optimized for working with time series data. For example, the Signals class is a binary NumPy array supporting advanced binary operations. Each method is either vectorized or Numba compiled for best peformance; most of the times even a badly "looped" Numba is faster than vectorized NumPy though. Moreover, each class stricly accepts a 2-dimensional array, where first axis is index (time) and second axis are columns (features), and provides standardized methods for processing 2-dimensional data along first axis. Thus, similar to a pd.DataFrame, one can do a single operation to transform tons of columns simultaneously. This, for example, is the magic behind backtesting thousands of window combinations at once.

For more details, check tests.

Installation

pip install git+https://github.com/polakowo/vectorbt.git

Note: importing vectorbt for the first time may take a while due to compilation.

Examples

Note: you will need to run the notebook to play with widgets.

vectorbt's People

Contributors

polakowo avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.