Metadata-Version: 2.4
Name: cvxsimulator
Version: 1.3.14
Summary: Simple simulator for investors
Project-URL: repository, https://github.com/cvxgrp/simulator
Author-email: Thomas Schmelzer <thomas.schmelzer@gmail.com>
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: jquantstats>=0.0.17
Requires-Dist: numpy>=2.0
Requires-Dist: pandas>=2.2.3
Provides-Extra: dev
Requires-Dist: clarabel>=0.9.0; extra == 'dev'
Requires-Dist: cvxpy-base>=1.5.1; extra == 'dev'
Requires-Dist: loguru; extra == 'dev'
Requires-Dist: pre-commit>=4.0.1; extra == 'dev'
Requires-Dist: pytest-cov>=6.0.0; extra == 'dev'
Requires-Dist: pytest>=8.3.3; extra == 'dev'
Requires-Dist: tinycta; extra == 'dev'
Description-Content-Type: text/markdown

# 🔄 [cvxsimulator](https://www.cvxgrp.org/simulator/book)

[![PyPI version](https://badge.fury.io/py/cvxsimulator.svg)](https://badge.fury.io/py/cvxsimulator)
[![Apache 2.0 License](https://img.shields.io/badge/License-APACHEv2-brightgreen.svg)](https://github.com/cvxgrp/simulator/blob/master/LICENSE)
[![Downloads](https://static.pepy.tech/personalized-badge/cvxsimulator?period=month&units=international_system&left_color=black&right_color=orange&left_text=PyPI%20downloads%20per%20month)](https://pepy.tech/project/cvxsimulator)
[![Coverage Status](https://coveralls.io/repos/github/cvxgrp/simulator/badge.png?branch=main)](https://coveralls.io/github/cvxgrp/simulator?branch=main)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://github.com/renovatebot/renovate)

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/cvxgrp/simulator)

A simple yet powerful simulator for investment strategies and portfolio backtesting.

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 helps to
simplify the accounting. It keeps track of the available cash,
the profits achieved, etc.

![Analytics](https://raw.githubusercontent.com/cvxgrp/simulator/main/newplot.png)

## 📥 Installation

Install cvxsimulator via pip:

```bash
pip install cvxsimulator
```

## 📊 Creating Portfolios

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

- [Create the builder object](#create-the-builder-object)
- [Loop through time](#loop-through-time)
- [Build the portfolio](#build-the-portfolio)

We demonstrate these steps with simple example 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 initializing the amount of cash used in an experiment:

```python
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 the 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.

```python
import numpy as np

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
implement the popular $1/n$ strategy.

```python
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.

```python
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:

```python
portfolio = b.build()
```

## 📈 Analytics

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

```python
portfolio.nav  # Net Asset Value
portfolio.cash  # Cash position
portfolio.equity  # Equity value
```

It is possible to generate a snapshot of the portfolio:

```python
portfolio.snapshot()
```

## 🛠️ Development

### UV Package Manager

Start with:

```bash
make install
```

This will install [uv](https://github.com/astral-sh/uv) and create
the virtual environment defined in
pyproject.toml and locked in uv.lock.

### Marimo Notebooks

We install [marimo](https://marimo.io) on the fly within the aforementioned
virtual environment. Execute:

```bash
make marimo
```

This will install and start marimo for interactive notebook development.

## 📚 Documentation

- Full documentation is available at [cvxgrp.org/simulator/book](https://www.cvxgrp.org/simulator/book)
- API reference can be found in the documentation
- Example notebooks are included in the repository under the `book` directory

## 🤝 Contributing

Contributions are welcome! Here's how you can contribute:

1. Fork the repository
2. Create a feature branch: `git checkout -b feature-name`
3. Commit your changes: `git commit -m 'Add some feature'`
4. Push to the branch: `git push origin feature-name`
5. Open a pull request

Please make sure to update tests as appropriate and follow the
code style of the project.

## 📄 License

This project is licensed under the Apache License 2.0 - see
the [LICENSE](LICENSE) file for details.

Copyright 2023 Stanford University Convex Optimization Group
