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.
Installing Python
Linux:
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.
Mac OS:
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”)
- Type:
python3
into 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:
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.
And then….
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
https://pythonprogramming.net/introduction-to-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):
1 2 |
import backtrader print(backtrader.__version__) |
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)
[…] Getting Setup: Python and Backtrader […]
[…] 3 – Getting Setup: Python and Backtrader […]
[…] This code in this post follows on from the code developed in the Backtrader: First Script post and will form part of the getting started series. If you are completely new to Backtrader and/or Python, I suggest starting here: Getting Setup: Python and Backtrader […]
Thanks a lot for your tutorials!
Very useful for an absolute beginner!
You are very welcome Cedric. I am glad they can help!
Hi ,
The version of Python running on my computer is Python 2.7.14.
Should I upgrade to python 3 to run backtrader?
backtrader is installed.
Thanks
Hi Matt,
Backtrader does support Python 2.7 according to the github page. However, most samples I see online (including this site) use Python 3 and the syntax (code) is a little different in places.
Also if you are using a Mac, it comes with Python 2.x installed but that is considered to be “System Python” and it is not recommended to use it for development. See here:
https://github.com/MacPython/wiki/wiki/Which-Python
Cheers!
Please can you help me¿
when i type “pip3 install backtrader”
this is what i get
C:\Users\Luichi>pip3 install backtrader
Requirement already satisfied: backtrader in c:\users\luichi\appdata\local\progr
ams\python\python36-32\lib\site-packages (1.9.64.122)
yahoo 0.1 has requirement requests==1.1.0, but you’ll have requests 2.11.1 which
is incompatible.
what is that about yahoo?
then
When i use anaconda to import backtrader
i get
File “”, line 1, in
import backtrader
ModuleNotFoundError: No module named ‘backtrader’
Hi Luis,
Have you installed a yahoo package via pip separately?
Hi Boss,
Thank you for your help;
About Backtrader; how to connect with Binance via CCXT; any good link?
Thanks again
Just installed python and backtrader as per your very clear instructions. thank you!
ModuleNotFoundError: No module named ‘backtrader’
i use to python 3.6 . i installed backtrader. but doesnt work. how i fix . all the time same problem. pypi pandas all installed.