Quickstart or "How to get 100% return per year"

First, do some initialization and set debugging level to debug to see progress of computation.

In [1]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
%config InlineBackend.figure_format = 'svg'

import numpy as np
import pandas as pd
import seaborn as sns
import datetime as dt
import matplotlib.pyplot as plt

import universal as up
from universal import tools, algos
from universal.algos import *

plt.rcParams["figure.figsize"] = (16, 8)
In [2]:
# ignore logged warnings
import logging

Let's try to replicate the results of B.Li and S.Hoi from their article On-Line Portfolio Selection with Moving Average Reversion. They claim superior performance on several datasets using their OLMAR algorithm. These datasets are available in data/ directory in .pkl format. Those are all relative prices (start with 1.) and artificial tickers. We can start with NYSE stocks from period 1/1/1985 - 30/6/2010.

In [3]:
# load data using tools module
data = tools.dataset('nyse_o')

# plot first three of them as example
2020-08-31T21:30:06.204946 image/svg+xml Matplotlib v3.3.1, https://matplotlib.org/

Now we need an implementation of the OLMAR algorithm. Fortunately, it is already implemented in module algos, so all we have to do is load it and set its parameters. Authors recommend lookback window $w = 5$ and threshold $\epsilon = 10$ (these are default parameters anyway). Just call run method on our data to get results for analysis.

In [4]:
# set algo parameters
algo = algos.OLMAR(window=5, eps=10)

# run
result = algo.run(data)

Ok, let's see some results. First print some basic summary metrics and plot portfolio equity with UCRP (uniform constant rebalanced portfolio).

In [5]:
result.plot(weights=False, assets=False, ucrp=True, logy=True);
    Profit factor: 1.89
    Sharpe ratio: 3.34 ± 0.54
    Ulcer index: 23.19
    Information ratio (wrt UCRP): 3.27
    Appraisal ratio (wrt UCRP): 3.05 ± 0.21
    UCRP sharpe: 1.16 ± 0.27
    Beta / Alpha: 1.55 / 165.350%
    Annualized return: 466.14%
    Annualized volatility: 56.69%
    Longest drawdown: 185 days
    Max drawdown: 46.29%
    Winning days: 58.3%
    Annual turnover: 349.1
2020-08-31T21:30:16.846929 image/svg+xml Matplotlib v3.3.1, https://matplotlib.org/

That seems really impressive, in fact it looks too good to be true. Let's see how individual stocks contribute to portfolio equity and disable legend to keep the graph clean.

In [6]:
result.plot_decomposition(legend=False, logy=True)
2020-08-31T21:30:17.276020 image/svg+xml Matplotlib v3.3.1, https://matplotlib.org/

As you can see, almost all wealth comes from single stock (don't forget it has logarithm scale!). So if we used just 5 of all these stocks, we would get almost the same equity as if we used all of them. To stress test the strategy, we can remove that stock and rerun the algorithm.

In [7]:
# find name of the most profitable asset
most_profitable = result.equity_decomposed.iloc[-1].idxmax()

# rerun an algorithm on data without it
result_without = algo.run(data.drop([most_profitable], 1))

# and print results
result_without.plot(weights=False, assets=False, ucrp=True, logy=True);
    Profit factor: 1.55
    Sharpe ratio: 2.48 ± 0.43
    Ulcer index: 12.67
    Information ratio (wrt UCRP): 2.36
    Appraisal ratio (wrt UCRP): 2.14 ± 0.21
    UCRP sharpe: 1.12 ± 0.27
    Beta / Alpha: 1.48 / 96.840%
    Annualized return: 192.81%
    Annualized volatility: 47.90%
    Longest drawdown: 202 days
    Max drawdown: 45.91%
    Winning days: 56.5%
    Annual turnover: 346.1
2020-08-31T21:30:27.892143 image/svg+xml Matplotlib v3.3.1, https://matplotlib.org/

We lost about 7 orders of wealth, but the results are more realistic now. Let's move on and try adding fees of 0.1% per transaction (we pay \$1 for every \\$1000 of stocks bought or sold).

In [8]:
result_without.fee = 0.001
result_without.plot(weights=False, assets=False, ucrp=True, logy=True)
    Profit factor: 1.35
    Sharpe ratio: 1.79 ± 0.34
    Ulcer index: 6.76
    Information ratio (wrt UCRP): 1.61
    Appraisal ratio (wrt UCRP): 1.41 ± 0.21
    UCRP sharpe: 1.12 ± 0.27
    Beta / Alpha: 1.47 / 62.608%
    Annualized return: 108.72%
    Annualized volatility: 47.17%
    Longest drawdown: 382 days
    Max drawdown: 50.09%
    Winning days: 50.2%
    Annual turnover: 346.1
[<AxesSubplot:ylabel='Total wealth'>]
2020-08-31T21:30:28.827428 image/svg+xml Matplotlib v3.3.1, https://matplotlib.org/