Time to get our hands wet…
In this post, we will take a look at downloading Python, where you can go to get some excellent introduction to python tutorials, installing the backtrader platform and finally checking that you are able to access the framework within python. If you are still deciding which backtesting framework or programming language to use, check out the Languages and Frameworks post.
Note of warning. This post does not go any further then setting things up. Skip this if you already have experience in using Python and pip.
If you are a Linux, user you are in luck. Python is very likely installed. In fact, I might be talking to myself as I don’t know any Linux user who is not aware of and/or tinkered with Python in the past.
You have it, but don’t use it…. The version of python installed with the OS is intended for use by the system. Some people refer to this as “system python”. Why shouldn’t you use it? For starters this version of python is old. It is running python 2 and most of the tutorials on this site will be coded from python 3. Another issue is that when Mac OS gets upgraded, it has the potential to break some of the updates you installed on the system version of python. Additionally, some packages are difficult to upgrade due to tweaks made by apple in the system python version. Finally, the system python is only available through the terminal. There is no entry in your applications folder and no launcher for clicking on python files.
So as you can probably guess, I recommend downloading and installing a new version of python. When you have downloaded and installed Python, you will then have a Python 3.x folder in your applications folder. This has an IDE (A development environment) for writing and running python code. A python launcher will also be installed which essentially allows you to launch (run) python scripts by double clicking on them.
So let’s get started:
First go to the downloads page on the python.org website by clicking here.
Then choose to install the latest version of Python 3. (Second link from the top)
Once you have downloaded the installer follow the usual steps for installing software on a MAC.
To test Python was installed correctly:
- Open a terminal (If you do not know where the terminal application is, open spotlight and type “terminal”)
python3into the terminal and press enter.
You should see some output like this:
To exit the python shell type
quit() then hit enter. (This is a built-in function call to exit the shell.)
Windows does not come with any Python version installed out of the box. Compared to the Mac, this makes it simpler to wrap your head around as there is not a tweaked “system” Python you need to worry about. As above first go to the Python downloads page by clicking here.
Once the page loads, you once again select the second link from the top, the latest Python 3 release. It looks exactly as the screenshot above for Mac OS except the “Mac OS X” test is replaced with “Windows” (as you would expect!).
Once you have downloaded the installer, follow the usual steps for installing Windows software. On the first installation screen make sure you select:
- Install for all users
- Add Python 3.x to path.
To verify python was installed correctly, open a command prompt and type
python. Notice that on Windows you do not have to specify python3 since there will not be any other Python versions installed. The output text you should see is the same as the in the Mac OS screenshot above.
To exit the python shell type
quit() then hit enter. This is a built-in function call to exit the shell.
Once you have Python 3 installed, I recommend you learn some basics in order not to be completely lost. Whether you prefer to learn with a book, on the web or getting down and dirty in the official docs, you will not be stuck for options. I have curated some of the content I would recommend below.
Most of what we will be doing in trading boils down to “If this happens then do that”. Basic skills should suffice. At least in the beginning. So don’t feel you need to spend months studying. I am a believer in picking things up as you go along. If you spend too long doing tutorials on projects you are not interested in, it is easy to become bored and frustrated when you inevitably get stuck.
Some basics to learn:
- Assigning variables
- Basic calculations
- If / Else statements
- For loops
- Writing functions
- Basic Classes
On the Web – Python Programming
This site offers has a number of excellent series on a range of topics. Better still, Harrison (the author) provides videos and commentary to each lesson on the series. Probably the best site on the net for Python programming.
Here is a copy of Harrison’s introduction to the series video:
Books – Automate the Boring Stuff.
Automate the Boring Stuff with Python: Practical Programming for Total Beginners provides automation projects aimed at the beginner. The nice thing is that the projects have relevance in our day to day lives. You will work with file management, excel sheets, web searches etc. I find that there is nothing more satisfying than getting practical regular use out of something you have coded.
Down and Dirty – The docs
Python provides a lot of great documentation on the official site. Some may find the documentation a little intimidating at first (especially those without computer science backgrounds) but stick with it. Once you become accustomed to it, this resource is invaluable.
Backtrader – The Framework
To install 3rd party packages and frameworks in Python we use a tool called “pip” (pip3 in python3). This is a package management tool that will handle downloading, installing, upgrading and removing the source code needed by 3rd party packages.
Fire up a terminal or console and type the following
pip3 install backtrader
Simple as that! You should no be seeing pip spring into action and start downloading / installing the packages.
Once complete you can verify the install by entering a python shell by typing
python3 in the terminal and hitting enter. Then type the following one line at a time (hitting enter at the end of each line):
You should then see the version number printed:
If you receive any errors, post them below in the comment sections and we can take a look at them (and then update this page as needed)
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.