Skip to main content

InvestLib is a Python package for backtesting ETF investments

Project description

InvestLib

InvestLib is a Python package for backtesting ETF investments. It allows you to create strategies, filter ETFs, and allocate capital based on your personal preferences within a rebalancing period.

Flow

  1. Choose etfs
  2. Choose rebalancing timer (every month, every 3 month at a custom interval)
  3. Filter certain ETFs for the next period.
  4. Allocate the current budget based on your chosen allocation

Rebalance

MonthlyTimer class allow you to set number of month to execute the rebalancing and on which day. Common possibilities includes FirstFriday.

Filters

Filters allow you to select a group of etfs in your list based on specific conditions, to create for example a momentum selection every month: - Percentage variation in month/days - Sharpe ratio (assuming risk-free rate as zero)

Allocations

You can allocate percentages between the chosen ETFs using: - Fixed allocation - Statically - Dinamically based on the number of filtered ETFs - PctChangeRank Allow you to make a ranking on filtered ETF and allocate money based on your preference

Data

The current unique data source is Tiingo. InvestLib automatically manages cash to avoid making repeated data calls. To use this feature, you need to create a free account on Tiingo and set the following environment variables:

  • tiingo_api_key: the API token provided by Tiingo upon subscription
  • tiingo_backup_path: the folder where you want to save the end-of-day CSV data for all the ETFs you need (default '/tmp/Tiingo').

Installation

I raccomand to use virtualenv:

pip install investlib

Example

Here's some code to test out some famous portfolios:

60/40 (60 stocks/40 bond)

import os

from investlib.strategy import Strategy
from investlib.rebalance import FixedAllocation
from investlib.rebalance import MonthlyTimer
from investlib.rebalance import FirstFriday
from investlib.report.draw import generate_report

os.environ['tiingo_api_key'] = '<tiingo_api_key>'

start = '2007-01-01'
end = '2021-06-04'
tickers = ['VTI', 'IEF']

# We create a strategy that rebalances the current money (invested + cash) to 60% VTI and 40% IEF
# every month on the first Friday.
s = Strategy(
    tickers, 
    start=start, 
    end=end, 
    timer=MonthlyTimer(months=1, day=FirstFriday()),
    allocation_class=FixedAllocation({'VTI':0.6,'IEF':0.4})
)
s.run()

generate_report(s, path='/tmp/6040.pdf', name='60/40')

Futures

- Add more stats
- Add more source of data
- Currency
- Tax
- Portfolio over Strategy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

investlib-0.0.6.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

investlib-0.0.6-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file investlib-0.0.6.tar.gz.

File metadata

  • Download URL: investlib-0.0.6.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for investlib-0.0.6.tar.gz
Algorithm Hash digest
SHA256 4ea2ed6866c0d3b06879e59c7c3b98ef0df0a68e650058017511214a3005cb0c
MD5 12981d054cfd0d78cae8a36f9a6f3e7d
BLAKE2b-256 feb2e662fffd40a4b3d8f377352c85ac221cae0a10f38e85c2116636a14d0243

See more details on using hashes here.

File details

Details for the file investlib-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: investlib-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for investlib-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a7d30b720f53997e95d6c222f295844e28590142af52e8c8c060b24a51b87896
MD5 cb0c03d5d2fd5957dbae0043da9854b2
BLAKE2b-256 c032927813e8f3eda012255d0b102ab089042b3bb6ac22b27534661951e80078

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page