Skip to content

Commit 4d5e00e

Browse files
committed
update of the book
1 parent d5dd462 commit 4d5e00e

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

Diff for: book/_config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Book settings
22
# Learn more at https://jupyterbook.org/customize/config.html
33

4-
title: cvx.simulator
4+
title: cvxsimulator
55
author: Thomas Schmelzer
66
only_build_toc_files: true
77

Diff for: book/docs/index.md

+99-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,102 @@
11
# Simulator
22

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

6+
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.
7+
This tool shall help to simplify the accounting. It keeps track of the available cash, the profits achieved, etc.
8+
9+
## Modus operandi
10+
11+
The simulator shall be completely agnostic as to the trading policy/strategy.
12+
Our approach follows a rather common pattern:
13+
14+
* [Create the portfolio object](#create-the-portfolio-object)
15+
* [Loop through time](#loop-through-time)
16+
* [Analyse results](#analyse-results)
17+
18+
We demonstrate those steps with somewhat silly policies. They are never good strategies, but are always valid ones.
19+
20+
### Create the portfolio object
21+
22+
The user defines a portfolio object by loading a frame of prices and initialize the initial amount of cash used in our experiment:
23+
24+
```python
25+
import pandas as pd
26+
from cvx.simulator.portfolio import build_portfolio
27+
28+
prices = pd.read_csv(Path("resources") / "price.csv", index_col=0, parse_dates=True, header=0).ffill(
29+
portfolio = build_portfolio(prices=prices, initial_cash=1e6)
30+
```
31+
32+
It is also possible to specify a model for trading costs.
33+
34+
### Loop through time
35+
36+
We have overloaded the `__iter__` and `__setitem__` methods to create a custom loop.
37+
Let's start with a first strategy. Each day we choose two names from the universe at random.
38+
Buy one (say 0.1 of your portfolio wealth) and short one the same amount.
39+
40+
```python
41+
for before, now, state in portfolio:
42+
# pick two assets at random
43+
pair = np.random.choice(portfolio.assets, 2, replace=False)
44+
# compute the pair
45+
stocks = pd.Series(index=portfolio.assets, data=0.0)
46+
stocks[pair] = [state.nav, -state.nav] / state.prices[pair].values
47+
# update the position
48+
portfolio[now] = 0.1 * stocks
49+
```
50+
51+
A lot of magic is hidden in the state variable.
52+
The state gives access to the currently available cash, the current prices and the current valuation of all holdings.
53+
54+
Here's a slightly more realistic loop. Given a set of $4$ assets we want to implmenent the popular $1/n$ strategy.
55+
56+
```python
57+
for _, now, state in portfolio:
58+
# each day we invest a quarter of the capital in the assets
59+
portfolio[now] = 0.25 * state.nav / state.prices
60+
```
61+
62+
Note that we update the position at time `now` using a series of actual stocks rather than weights or cashpositions.
63+
Future versions of this package may support such conventions, too.
64+
65+
### Analyse results
66+
67+
The loop above is filling up the desired positions. The portfolio object is now ready for further analysis.
68+
It is possible dive into the data, e.g.
69+
70+
```python
71+
portfolio.nav
72+
portfolio.cash
73+
portfolio.equity
74+
...
75+
```
76+
77+
## The dirty path
78+
79+
Some may know the positions they want to enter for eternity.
80+
Running through a loop is rather non-pythonic waste of time in such a case.
81+
It is possible to completely bypass this step by submitting
82+
a frame of positions together with a frame of prices when creating the portfolio object.
83+
84+
## Poetry
85+
86+
We assume you share already the love for [Poetry](https://python-poetry.org). Once you have installed poetry you can perform
87+
88+
```bash
89+
poetry install
90+
```
91+
92+
to replicate the virtual environment we have defined in pyproject.toml.
93+
94+
## Kernel
95+
96+
We install [JupyterLab](https://jupyter.org) within your new virtual environment. Executing
97+
98+
```bash
99+
./create_kernel.sh
100+
```
101+
102+
constructs a dedicated [Kernel](https://docs.jupyter.org/en/latest/projects/kernels.html) for the project.

0 commit comments

Comments
 (0)