DEV Community

dennislwm
dennislwm

Posted on • Originally published at tldr.pro on

How to Dockerize Backtrader in 4 GIF Steps

How to Run Backtrader on a Docker Container in 4 GIF Steps

Backtrader is "a feature-rich Python framework for backtesting and trading."

Backtrader isn't just for backtesting strategies. It's also has live trading and is integrated with InteractiveBrokers ["IB"], Oanda, VisualChart, Alpaca, ccxt, etc. Using your own dockerized Backtrader platform, you don't have to be tied down to any operation system or third-party platforms that provide backtesting functions. And if you're a serious trader, you can create your own stock, forex, crypto or even a deep machine learning trading robot to trade automatically.

Introduction

TL;DR

In this article, you will dockerize Backtrader on your Windows machine. In the Docker container, you will execute the example Hello Algotrading python script, which outputs a visual result of the backtesting strategy. You will have access to thousands of Data Science libraries using the Anaconda package manager.

Prerequisites

Step 1 - Pull a Pre-Defined Docker Image

There are many pre-defined Docker images on the cloud, from both official and unofficial partners.

The are many reasons for choosing an image, such as reliablity, speed, size, backward compatibility, etc.

Why did I choose the Docker Anaconda3 image?

The benefits of choosing the above pre-defined image are as follows:

  • Anaconda is a package manager for Python Data Science libraries

  • Python (with matplotlib) comes prepackaged

  • It runs in root administrator mode.

The only downside that I can see is that the image takes a whopping 2.6 Gigabyte of hard disk space!

Type the following command in a Powershell or Command Prompt ["CMD"] to pull the Docker Anaconda image:

PS> docker pull continuumio/anaconda3
Enter fullscreen mode Exit fullscreen mode

Note: The Docker Anaconda3 image has Python 3.5 installed by default. If you want to use Python 2.7 instead, then you should pull the continuumio/anaconda image.

Step 2 - Configure Docker to Enable XLaunch as Display

In this article, we will show you how to enable the Graphical User Interface ["GUI"] of a Docker Linux container from a Windows 10 host system.

Why would we want to do that?

Although Backtrader can be executed solely from the terminal, however, some of us may prefer to have a visual output for our backtesting strategies.

First, to find your Docker's IP address, type the following command in Powershell:

PS> ipconfig
Enter fullscreen mode Exit fullscreen mode

Search for the Ethernet adapter called DockerNAT, and note down the IPv4 Address. For example:

IPv4 Address. . . . . . . . : 10.0.75.1
Enter fullscreen mode Exit fullscreen mode

In the Powershell terminal, type the following command:

PS> set-variable -name DISPLAY -value <ip-address>:0.0
Enter fullscreen mode Exit fullscreen mode

where is your Docker's IP address.

Note: Remember to append :0.0 to the end of your IP address.

Step 3 - Create a Docker Container from Our Image

Type the following command in PowerShell to create a folder, e.g. anaconda3, which will be mounted as a shared folder in the new Docker container:

PS> mkdir anaconda3
Enter fullscreen mode Exit fullscreen mode

Then, type the following command to create a Docker container:

PS> docker run -v d:\docker\anaconda3\:/home --name objAnaconda3 -e DISPLAY=$DISPLAY -it continuumio/anaconda3
Enter fullscreen mode Exit fullscreen mode

These inputs require some explanation.

Bind Mount a Volume

By design, Docker containers do not store persistent data. Once the container stops, any data written to it will no longer be available. To solve this issue of persisting data from a container, Docker has two options: (a) bind mounts; (b) volumes.

In the above example, we use the first option, where a Windows folder (host) is bound to a Docker container folder using the -v option as follows:

-v <host-folder>:<container-folder>
Enter fullscreen mode Exit fullscreen mode

Container Name

While the input --name is optional, we use it to give the container a reference name, which we can use later to restart the container as follows:

--name <reference-name>
Enter fullscreen mode Exit fullscreen mode

Set Environment Variable

We can set the environment variables for the Docker container as follows:

-e <variable-name>=<variable-value>
Enter fullscreen mode Exit fullscreen mode

Interactive Terminal

We can tell Docker to run the container in an interactive mode by using the input -it as follows:

PS> docker run -it <image-name>
Enter fullscreen mode Exit fullscreen mode

Note that all the other inputs are optional, and the only input required for the Docker run command is the image-name.

Next, type the following command in the Docker bash terminal:

# pip install backtrader[plotting]
Enter fullscreen mode Exit fullscreen mode

Followed by exit command to stop the Docker container and return to Powershell:

# exit
Enter fullscreen mode Exit fullscreen mode

Step 4 - Execute Hello Algotrading Python Script

The final step is to restart our previous container, and run our Python scripts.

Ensure that you're in the mounted folder of the container.

Create a file called basic.py and insert the following code:

from __future__ import (absolute_import, division, print_function,
                         unicode_literals)
 import backtrader as bt
 if __name__ == ' __main__':
     cerebro = bt.Cerebro()
     print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
     cerebro.run()
     print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Enter fullscreen mode Exit fullscreen mode

Next, create another file called sma.py and insert the following code:

from datetime import datetime
 import backtrader as bt
 # Create a subclass of Strategy to define the indicators and logic
 class SmaCross(bt.Strategy):
     # list of parameters which are configurable for the strategy
     params = dict(
         pfast=10, # period for the fast moving average
         pslow=30 # period for the slow moving average
     )
     def __init__ (self):
         sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
         sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
         self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
     def next(self):
         if not self.position: # not in the market
             if self.crossover > 0: # if fast crosses slow to the upside
                 self.buy() # enter long
         elif self.crossover < 0: # in the market & cross to the downside
             self.close() # close long position
 cerebro = bt.Cerebro() # create a "Cerebro" engine instance
 # Create a data feed
 data = bt.feeds.YahooFinanceData(dataname='MSFT',
                                  fromdate=datetime(2011, 1, 1),
                                  todate=datetime(2012, 12, 31))
 cerebro.adddata(data) # Add the data feed
 cerebro.addstrategy(SmaCross) # Add the trading strategy
 cerebro.run() # run it all
 cerebro.plot() # and plot it with a single command
Enter fullscreen mode Exit fullscreen mode

Type the following command in PowerShell to restart our previous container:

PS> docker start -ia objAnaconda3
Enter fullscreen mode Exit fullscreen mode

Then, in the Docker interactive terminal, type the following commands to run both scripts:

# cd /home
 # python basic.py
 # python sma.py
Enter fullscreen mode Exit fullscreen mode

Your project folder should look like this:

docker/
   +- anaconda3/
       |- basic.py
       |- sma.py
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article, you used Docker pull command to download an image of Anaconda package manager from DockerHub.com. Then, you created an instance of this image using the Docker run command, which mounted a local folder and started an interactive terminal within this container. In this terminal, you installed Backtrader with plotting feature and you executed the Hello Algotrading python script.

What To Do Next

You can further explore Backtrader in several meaningful ways:

Top comments (0)