A collection of scripts for trading strategy back-testing and OHLC (+ Volume) time series scraping.
To have a light-weight, event-driven backtesting script that produces a short report with the most common portfolio/strategy performance ratios & visualizes a comparison of said performance against common indices. That way one can focus on rapid strategy development and have some data to decide whether or not the strateg(y/ies) should be allocated more time on a more robust testing platform.
A Jupyter notebook connected to:
- a small Python module I wrote to run an event-driven back-test
- a Node/Express/Playwright service for scraping time series data wherever the officially used API isn't an option
The Python module consists of the following chain of files (which may be used independently, but have been constructed to work best together):
- A
DataFetcher
class which handles fetching.csv
historical OHLC(+ Volume) time series, saving it to a file & returning apandas
dataframe. - A
Strategy
class which takes in several arguments, most important of which 2 functions to define entry and exit criteria for the strategy. (Seemean_reversion_strategy.py
&momentum_strategy.py
for examples of such functions). - A
Portfolio
class which keeps track of capital, registers strategies for use & at the end of a period returns equity data for the next class, which returns the actual report. PerformanceReport
- takes equity data, calculates several risk/reward ratios + max drawdown during the active trading period and returns a small object with them.
For the OHLC time series 2 integrations exist:
- AlphaVantage (data provider with an API, only has data on US/Canadian & Crypto securities AFAIK). Chosen because of their Free API & adjusted time series functionality + support for cryptocurrencies.
- Avanza Bank (Swedish neo-bank whose page I've built a script to extract OHLC + Volume data on Stocks from). Chosen because option nr.1 does not have data on European & Asian stocks (e.g DAX, OMX, etc).
Contains the actual Jupyter notebook workbench from which:
- the main loop for the back-test is run,
- the test/portfolio/strategy settings & data provider keys are propagated further into the module
Contains a short script that brings up an ExpressJS server that in turn uses Playwright to parse Avanza's page to provide OHLC (+ Volume) time series data in the same .csv
format as AlphaVantage's API.
- (assumming you have jupyter notebook / VSCode with the proper extensions enabled / your choice of notebook engine)
jupyter lab
(in/backtesting
) - Then make sure you have your AlphaVantage API token set as environment variables.
- Bring the
web_scraping/
service up by runningyarn start
(after making sure you have the necessarynode_modules
by runningyarn
ofc) - Click run on the main cell (wait for the time series to be downloaded, which usually takes roughly a minute if one of the strategies uses the web scraper, otherwise some 20 odd seconds)
- Inspect the performance report