Skip to content

Commit

Permalink
Working to create a Docker image to work with Visual Studio Code (#181)
Browse files Browse the repository at this point in the history
Working to create a Dev Container image to work with Visual Studio Code
  • Loading branch information
miohtama authored Jan 27, 2023
1 parent 6df6aca commit c6789c0
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 5 deletions.
37 changes: 37 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
See [Notebook Docker Image documentation](../docs).

38 changes: 38 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ strategy-state.json

# Generated by crashed trade-executor
# for atomic strategy-state.json write
tmp*
tmp*

# Filled by set-up-examples.sh script
examples
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

## 0.2

- Breaking API changes

- Breaking API changes


asdasd
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
Binary file added docs/backtesting-period.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/command-line-python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/open-in-dev-container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/run-all-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/run-all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 131 additions & 0 deletions docs/visual-studio-code-docker-dev-container-for-jupyter-notebooks.md
Original file line number Diff line number Diff line change
@@ -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
Binary file added docs/vscode-splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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.
41 changes: 41 additions & 0 deletions notebook.dockerfile
Original file line number Diff line number Diff line change
@@ -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



2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 12 additions & 0 deletions scripts/set-up-examples.sh
Original file line number Diff line number Diff line change
@@ -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 \;

0 comments on commit c6789c0

Please sign in to comment.