Python
#
Using the PackagePre-requisite: A PathLit account is required to interact with the API with a unique key to the account. It can be created at the account creation page.
#
Installationpip3 install pathlit
#
Tutorial#
ObjectiveIn this quick tutorial, we are going to install the PathLit Python package, query the paths
endpoint with three tickers "AAPL", "MSFT", "HD"
. paths
computes the dollar returns of a portfolio for the actual time series (e.g. using the realised market data), plot two strategies (l1r.l2d.l3hrp
and l1r.l2d.l3mdcp
(what is this?).
Finally we'll use the sims
endpoint which computes the dollar returns of a portfolio for multiple simulated time series (e.g. the simulated market data) apply three runs for our three tickers ("AAPL", "MSFT", "HD"
), two strategies (l1r.l2d.l3hrp
and l1r.l2d.l3mdcp
) and plot them.
#
Let's do itStep 1: Set-up an account with us and retrieve and API Key. For the purposes of this tutorial, we will use a dummy key: 56gIvzm4dj1vJpNUlv3RJ2CeMd47JETG3bcf5zLS
.
Step 2: Install the required packages to run this tutorial (if you don't already have them).
In order to view the plotted graph, we'll need a Jupyter Notebook, with matplotlib
and numpy
installed.
Install Jupyter here.
pip3 install matplotlib numpy
Step 3: Initialise the package and register the key:
from pathlit.client import Clientimport numpy as npimport matplotlib.pyplot as plt
# initializes clientc = Client("56gIvzm4dj1vJpNUlv3RJ2CeMd47JETG3bcf5zLS")
Note: You can view the documentation using the built-in
help()
function
help(Client.get_paths)
Step 4: Retrieve the list of available tickers, in order to select a few for analysis:
c.get_tickers()
Step 6: Obtain the weights of the tickers selected, via our existing strategies. Please note that even though for this tutorial we chose to focus on two strategies (l1r.l2d.l3hrp
and l1r.l2d.l3mdcp
), the get_weights(tickers)
function will return all the current PathLit
suported strategies.
weights = c.get_weights(["AAPL", "MSFT", "HD"])
Step 7: Obtain the paths of our existing strategies of the tickers selected, using the realised market data, for a staring portfolio of USD 100,000:
paths = c.get_paths(["AAPL", "MSFT", "HD"])strat_1 = paths[2] # l1r.l2d.l3hrpstrat_2 = paths[4] # l1r.l2d.l3mdcp
plt.plot([d["date"] for d in strat_1["data"]], [d["value"] for d in strat_1["data"]], "r-")plt.plot([d["date"] for d in strat_2["data"]], [d["value"] for d in strat_2["data"]], "b-")plt.legend(("l1r.l2d.l3hrp", "l1r.l2d.l3mdcp"))
You should get something like this:
Step 8: Obtain the paths of our existing strategies of the tickers selected, using 3 simulated market data runs, for a staring portfolio of USD 100,000:
sims = c.simulate(["AAPL", "MSFT", "HD"], 3)strat_1 = sims[1][2] # l1r.l2d.l3hrp from the 2nd runstrat_2 = sims[1][4] # l1r.l2d.l3mdcp from the 2nd run
plt.plot([d["date"] for d in strat_1["data"]], [d["value"] for d in strat_1["data"]], "r-")plt.plot([d["date"] for d in strat_2["data"]], [d["value"] for d in strat_2["data"]], "b-")plt.legend(("l1r.l2d.l3hrp", "l1r.l2d.l3mdcp"))
Which should give you something similar to that:
#
ConclusionAt the end of this quick tutorial, you:
- Installed the PathLit Python Package from PyPI
- (optionally) retrieved the currently supported tickers by the PathLit Engine
- Computed the weights for thre tickers (
"AAPL", "MSFT", "HD"
) and two strategies (l1r.l2d.l3hrp
andl1r.l2d.l3mdcp
) - Computed and plotted the dollars returns of a USD 100,000 with realised market data for these three tickers and two strategies (a form of backtesting)
- Computed and plotted the dollars returns of a USD 100,000 with three simulated market data runs for these three tickers and two strategies (a form of risk simulation AKA Monte Carlo simulation).
#
What's next?You could modify your code to plot all the strategies currently supported by the PathLit Engine for these three tickers and observe which one is better than the others given the context of these three tickers.
If you have a personal/proprietary strategy, you could benchmark it against the currently supported strategies by the PathLit Engine and get some meaningful perspectives.
If you are code savvy you could even build an active robo-advisor using the the PathLit Engine to compute the allocation of the stocks you wish to see in one or many portfolio.