Skip to main content

Simple simulator for investors

Project description

cvxsimulator

PyPI version Apache 2.0 License Downloads Coverage Status

Open in GitHub Codespaces

Given a universe of $m$ assets we are given prices for each of them at time $t_1, t_2, \ldots t_n$, e.g. we operate using an $n \times m$ matrix where each column corresponds to a particular asset.

In a backtest we iterate in time (e.g. row by row) through the matrix and allocate positions to all or some of the assets. This tool shall help to simplify the accounting. It keeps track of the available cash, the profits achieved, etc.

Analytics

Installation

It is possible to install cvxsimulator with the quantstats extra

pip install cvxsimulator[quantstats]

or without it

pip install cvxsimulator

Creating portfolios

The simulator shall be completely agnostic as to the trading policy/strategy. Our approach follows a rather common pattern:

We demonstrate those steps with somewhat silly policies. They are never good strategies, but are always valid ones.

Create the builder object

The user defines a builder object by loading prices and initialize the amount of cash used in an experiment:

import pandas as pd
from cvx.simulator import Builder

prices = pd.read_csv("prices.csv", index_col=0, parse_dates=True, header=0)
b = Builder(prices=prices, initial_aum=1e6)

Prices have to be valid, there may be NaNs only at the beginning and the end of each column in frame. There can be no NaNs hiding in the middle of any time series.

It is also possible to specify a model for trading costs. The builder helps to fill up the frame of positions. Only once done we construct the actual portfolio.

Loop through time

We have overloaded the __iter__ and __setitem__ methods to create a custom loop. Let's start with a first strategy. Each day we choose two names from the universe at random. Buy one (say 0.1 of your portfolio wealth) and short one the same amount.

for t, state in b:
    # pick two assets at random
    pair = np.random.choice(state.assets, 2, replace=False)
    # compute the pair
    units = pd.Series(index=state.assets, data=0.0)
    units[pair] = [state.nav, -state.nav] / state.prices[pair].values
    # update the position
    b.position = 0.1 * units
    # Do not apply trading costs
    b.aum = state.aum

Here t is the growing list of timestamps, e.g. in the first iteration t is $t1$, in the second iteration it will be $t1, t2$ etc.

A lot of magic is hidden in the state variable. The state gives access to the currently available cash, the current prices and the current valuation of all holdings.

Here's a slightly more realistic loop. Given a set of $4$ assets we want to implemenent the popular $1/n$ strategy.

for t, state in b:
    # each day we invest a quarter of the capital in the assets
    b.position = 0.25 * state.nav / state.prices
    b.aum = state.aum

Note that we update the position at the last element in the t list using a series of actual units rather than weights or cashpositions. The builder class also exposes setters for such alternative conventions.

for t, state in b:
    # each day we invest a quarter of the capital in the assets
    b.weights = np.ones(4)*0.25
    b.aum = state.aum

Build the portfolio

Once finished it is possible to build the portfolio object

portfolio = b.build()

Analytics

The portfolio object supports further analysis and exposes a number of properties, e.g.

portfolio.nav
portfolio.cash
portfolio.equity

It is possible to perform

portfolio.snapshot()

We have also integrated the quantstats package as an optional extra for further analysis. It is usually enough to inject the portfolio.nav into the many functions exposed by this package.

uv

You need to install task. Starting with

task simulator:install

will install uv and create the virtual environment defined in pyproject.toml and locked in uv.lock.

marimo

We install marimo on the fly within the aforementioned virtual environment. Executing

task simulator:marimo

will install and start marimo.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

cvxsimulator-1.3.4.tar.gz (9.2 MB view details)

Uploaded Source

Built Distribution

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

cvxsimulator-1.3.4-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file cvxsimulator-1.3.4.tar.gz.

File metadata

  • Download URL: cvxsimulator-1.3.4.tar.gz
  • Upload date:
  • Size: 9.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cvxsimulator-1.3.4.tar.gz
Algorithm Hash digest
SHA256 2f1d9a985fc40e4f7aa1009df2b9911d4bebe908482c1e0aa6f02dc815bd9d0d
MD5 43932c2bac9235f7159f2d42cadc2e0c
BLAKE2b-256 78606ad54029e36e450d52fa697d8aafb244b6113c2abb98f1a46c556ec1f228

See more details on using hashes here.

Provenance

The following attestation bundles were made for cvxsimulator-1.3.4.tar.gz:

Publisher: release.yml on cvxgrp/simulator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cvxsimulator-1.3.4-py3-none-any.whl.

File metadata

  • Download URL: cvxsimulator-1.3.4-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for cvxsimulator-1.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b14cd590a62da6555d50c741e10958bde9cdd74219b014cefa87771620c3a854
MD5 8478403951bd0d61b390dcb2da77c993
BLAKE2b-256 f5bdda272c7dfefdf87a674880e4bd83f208b95112c9001b11658fa2104d9b92

See more details on using hashes here.

Provenance

The following attestation bundles were made for cvxsimulator-1.3.4-py3-none-any.whl:

Publisher: release.yml on cvxgrp/simulator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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