diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 000000000..648f6cd2d --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,37 @@ +# +# Build trade-executor as a Docker container for backtesting notebooks +# + +# Use Microsoft specific base image +ARG VARIANT="3.10-bullseye" +FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} + +# Passed from Github Actions +ARG GIT_VERSION_TAG=unspecified +ARG GIT_COMMIT_MESSAGE=unspecified +ARG GIT_VERSION_HASH=unspecified + +ENV PYTHONDONTWRITEBYTECODE 1 \ + PYTHONUNBUFFERED 1 + +RUN apt-get update \ + && apt-get install curl gcc python3-docutils -y \ + && curl -sSL https://install.python-poetry.org | python - --version 1.3.1 + +ENV PATH="/root/.local/bin:$PATH" + +WORKDIR /trading-strategy + +# Set the version control information within Github Actions +# if available +RUN echo $GIT_VERSION_TAG > GIT_VERSION_TAG.txt +RUN echo $GIT_COMMIT_MESSAGE > GIT_COMMIT_MESSAGE.txt +RUN echo $GIT_VERSION_HASH > GIT_VERSION_HASH.txt + +# Copy package source code to the Docker image +COPY . . + +# Install all Python dependencies using Poetry +RUN poetry config virtualenvs.create false +RUN poetry install --no-dev --no-interaction --no-ansi -E web-server -E execution -E qstrader + diff --git a/.devcontainer/README.md b/.devcontainer/README.md new file mode 100644 index 000000000..5f5e73df0 --- /dev/null +++ b/.devcontainer/README.md @@ -0,0 +1,2 @@ +See [Notebook Docker Image documentation](../docs). + diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..2764ff5c2 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,38 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/python +// The original blog post this is based at: https://marioscalas.medium.com/using-python-and-poetry-inside-a-dev-container-33c80bc5a22c +{ + "name": "Trading Strategy notebooks", + + "build": { + "dockerfile": "Dockerfile", + "context": "..", + "args": { + // Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6 + // Append -bullseye or -buster to pin to an OS version. + // Use -bullseye variants on local on arm64/Apple Silicon. + "VARIANT": "3.10-bullseye" + } + }, + + // Features to add to the dev container. More info: https://containers.dev/features. + // "features": {}, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "pip3 install --user -r requirements.txt", + + // Configure tool-specific properties. + "extensions": [ + "ms-python.python", + "ms-python.vscode-pylance", + "ms-toolsai.jupyter", + "ms-toolsai.jupyter-keymap", + "ms-toolsai.jupyter-renderers", + "vscode-icons-team.vscode-icons", + "lextudio.restructuredtext-pack", + "trond-snekvik.simple-rst" +] +} diff --git a/.gitignore b/.gitignore index ea373bb89..29ecca64e 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,7 @@ strategy-state.json # Generated by crashed trade-executor # for atomic strategy-state.json write -tmp* \ No newline at end of file +tmp* + +# Filled by set-up-examples.sh script +examples \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ee3fd9055..25e881567 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,4 +2,8 @@ ## 0.2 -- Breaking API changes \ No newline at end of file + +- Breaking API changes + + +asdasd \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5bd3c9acb..1640f6645 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # -# Build trade-executor as a Docker container +# Build trade-executor as a Docker container for live treading # # See https://stackoverflow.com/a/71786211/315168 for the recipe # diff --git a/deps/trading-strategy b/deps/trading-strategy index bb68f408f..f4af57323 160000 --- a/deps/trading-strategy +++ b/deps/trading-strategy @@ -1 +1 @@ -Subproject commit bb68f408fe2e61dce1f02c2c060e59512aef3413 +Subproject commit f4af57323eceb8c1ec00a2be0b0e518db6201aa6 diff --git a/docs/backtesting-period.png b/docs/backtesting-period.png new file mode 100644 index 000000000..63916b2fa Binary files /dev/null and b/docs/backtesting-period.png differ diff --git a/docs/command-line-python.png b/docs/command-line-python.png new file mode 100644 index 000000000..d5c3c4391 Binary files /dev/null and b/docs/command-line-python.png differ diff --git a/docs/open-in-dev-container.png b/docs/open-in-dev-container.png new file mode 100644 index 000000000..c86fcbafe Binary files /dev/null and b/docs/open-in-dev-container.png differ diff --git a/docs/run-all-results.png b/docs/run-all-results.png new file mode 100644 index 000000000..fea96b7e9 Binary files /dev/null and b/docs/run-all-results.png differ diff --git a/docs/run-all.png b/docs/run-all.png new file mode 100644 index 000000000..8d525672c Binary files /dev/null and b/docs/run-all.png differ diff --git a/docs/visual-studio-code-docker-dev-container-for-jupyter-notebooks.md b/docs/visual-studio-code-docker-dev-container-for-jupyter-notebooks.md new file mode 100644 index 000000000..dda642ba8 --- /dev/null +++ b/docs/visual-studio-code-docker-dev-container-for-jupyter-notebooks.md @@ -0,0 +1,131 @@ +# Visual Studio Code Dev Container for Jupyter Notebooks + +These instructions are for using [Trading Strategy](https://tradingstrategy.ai) +backtesting and decentralised finance research environment in [Microsoft Visual Studio Code](https://code.visualstudio.com/). + +![img_1.png](vscode-splash.png) + +[Microsoft Visual Studio Code](https://code.visualstudio.com/), a popular editor for Jupyter notebooks. +[Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) is a Visual Studio Code +feature to easily distribute ready-made development environments to users. +Dev Containers work on any operating system (Windows, macOS, Linux). +Dev Container users a special `.devcontainer` configuration format in supported Git repositories. + +The Trading Strategy Dev Container is a pre-made development environment +for quant finance research in decentralised finance. + +It combines + +- Trading Strategy framework and libraries +- Ready set up Python environment with correct Python interpreter +- Visual Studio Code plugins and settings needed to run and + edit these notebooks +- Example notebooks and documentation notebooks ready in the file explorer +- The Docker image can be re-used for other Python editors and purposes; + in this documentation we focus on Visual Studio Code as it is the easiest +- Apple Silicon (Macbook M1) friendliness + +## Prerequisites + +- Existing basic knowledge of Python programming, Jupyter notebooks and data science and trading +- The set up will download 2 GB+ data, so we do not recommend to try this + over a mobile connection + +## Setting up Visual Studio Code + +- [Install Visual Studio code](https://code.visualstudio.com/) +- [Install Docker desktop](https://www.docker.com/products/docker-desktop/) +- Install the [Dev Containers extension](https://code.visualstudio.com/docs/devcontainers/containers) + within Visual Studio Code + +## Checkout the repository from Github + +After you are done with the local software installation steps above, +you can check out the repository using Visual Studio Code. + +Press `F1` to bring up the command palette (`fn` + `F1` on Macs) + +Choose `Clone from Github`. + +Paste in the repository name: ``` + +## Start the Dev Container + +When `trade-executor` project opens you get a pop up *Reopen in container*. + +![img.png](open-in-dev-container.png) + +Click it and Visual Studio Code will build the development environment for you. +This will take 2 - 15 minutes depening on your Internet connection speed. + +You can also manually execute this action by pressing `F1` to bring up the command palette (`fn` + `F1` on Macs) +and finding *Reopen in container* action. + +## Using the container + +After the container is started, open Terminal in Visual Studio Code (*View > Terminal*). + +Paste in the following command: + +```shell +scripts/set-up-examples.sh +``` + +This will create `examples` folder and copies all notebooks [from the documentation](https://tradingstrategy.ai/docs/) +there. + +### Running an example + +Here are short instructions how to edit and run notebooks. + +Open `examples/synthetic-ema.ipynb` + +Edit the backtesting period in the first code cell: + +![img_1.png](backtesting-period.png) + + +Set to + +```python +start_at = datetime.datetime(2022, 1, 1) +end_at = datetime.datetime(2023, 1, 1) +``` + +Then press Run all: + +![img_1.png](run-all.png) + +Now scroll to the bottom of the notebook and see you have updated results for 2022 - 2023: + +![img_1.png](run-all-results.png) + +### Using command line Python + +If you open Visual Studio Code terminal and run `python` command +it comes with Trading Strategy packages installed. + +![img_1.png](command-line-python.png) + +## Troubleshooting + +### No space left on device error + +Make sure you clean up old Docker images, containers and volumes in your Docker for Desktop +to reclaim disk apce. + +### Manual build + +Building the Docker image by hand: + +```shell +docker build --file .devcontainer/Dockerfile . +``` + + +## Further reading + +- https://code.visualstudio.com/docs/devcontainers/containers +- https://stackoverflow.com/questions/63998873/vscode-how-to-run-a-jupyter-notebook-in-a-docker-container-over-a-remote-serve +- https://keestalkstech.com/2022/08/jupyter-notebooks-vscode-dev-container-with-puppeteer-support/ +- https://marioscalas.medium.com/using-python-and-poetry-inside-a-dev-container-33c80bc5a22c diff --git a/docs/vscode-splash.png b/docs/vscode-splash.png new file mode 100644 index 000000000..6bf08d4d6 Binary files /dev/null and b/docs/vscode-splash.png differ diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 000000000..0b00830fd --- /dev/null +++ b/examples/README.md @@ -0,0 +1,6 @@ +# Example notebooks + +This folder will contain example notebooks to be used with +[Visual Studio Code Dev Container](../docs/visual-studio-code-docker-dev-container-for-jupyter-notebooks.md). + +Follow the Dev Container instructions to get the examples. diff --git a/notebook.dockerfile b/notebook.dockerfile new file mode 100644 index 000000000..c54206833 --- /dev/null +++ b/notebook.dockerfile @@ -0,0 +1,41 @@ +# +# Build trade-executor as a Docker container for backtesting notebooks +# +# See https://stackoverflow.com/a/71786211/315168 for the recipe +# + +# See official Python Docker images +# https://hub.docker.com/_/python/ +FROM python:3.10.9-slim-buster + +# FROM python:3.11.1-slim-buster + +# Passed from Github Actions +ARG GIT_VERSION_TAG=unspecified +ARG GIT_COMMIT_MESSAGE=unspecified +ARG GIT_VERSION_HASH=unspecified + +ENV PYTHONDONTWRITEBYTECODE 1 \ + PYTHONUNBUFFERED 1 + +RUN apt-get update \ + && apt-get install curl gcc -y \ + && curl -sSL https://install.python-poetry.org | python - --version 1.3.1 + +ENV PATH="/root/.local/bin:$PATH" + +WORKDIR /trading-strategy + +# Set in Github Actions +RUN echo $GIT_VERSION_TAG > GIT_VERSION_TAG.txt +RUN echo $GIT_COMMIT_MESSAGE > GIT_COMMIT_MESSAGE.txt +RUN echo $GIT_VERSION_HASH > GIT_VERSION_HASH.txt + +# package source code +COPY . . + +RUN poetry config virtualenvs.create false +RUN poetry install --no-dev --no-interaction --no-ansi -E web-server -E execution -E qstrader + + + diff --git a/pyproject.toml b/pyproject.toml index 204c15fff..352f72ab8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ packages = [ ] [tool.poetry.dependencies] -python = ">=3.10,<3.11" +python = ">=3.10,<3.12" # Use these during development # web3-ethereum-defi = {path = "deps/web3-ethereum-defi", develop = true} diff --git a/scripts/set-up-examples.sh b/scripts/set-up-examples.sh new file mode 100755 index 000000000..b5a373be7 --- /dev/null +++ b/scripts/set-up-examples.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# +# This script will set up an examples folder for notebooks. +# +# It combines notebooks from tradd-executor and docs repos +# + +set -e + +find ./notebooks -iname "*.ipynb" -exec cp {} examples \; +git clone https://github.com/tradingstrategy-ai/docs.git /tmp/docs +find /tmp/docs -iname "*.ipynb" -exec cp {} examples \; \ No newline at end of file