Python is a versatile programming language that is comparatively easier to learn and program. One of the advantages of Python is that there are a host of libraries that the user can install on their system so that they don't have to code from scratch.
These libraries are updated time and again to make sure everything is working smoothly. But there can be occasions where some libraries might become incompatible with each other due to an update in one library.
You can use this article to set up your local Python environment with all the recommended packages for quant analysis and algo trading. This blog covers the following sections. If you are already familiar with Anaconda installation or have already installed anaconda then you can straight away go to the ‘Set the Python Version and Install Packages’ section.
- Install Anaconda on Windows
- Install Anaconda on Mac
- Troubleshooting Installation Issues
- Set the Python Version and Install Packages
- Check Python Version
- Troubleshooting Tips
For Quantra users
While the Python console is self-contained, wouldn’t it be better if you had a helping hand every now and then? What if you wanted to create a program to analyse the stock price of Tesla and share it with a colleague?
For these tasks and more, Anaconda navigator was created. In a nutshell, Anaconda is a Python distribution platform that helps you in organising your code, write simple explanations to the code in markdown cells, as well as give control over how the program should be run.
The Anaconda platform supports various applications such as Jupyter Notebook, JupyterLab, Spyder and more, to help you program easily. You can also use the Anaconda prompt to install Python packages easily.
Let us first see how to download Anaconda on Microsoft Windows. If you have already installed Anaconda, you can skip the sections and head straight to the section on setting the python version.
Install Anaconda on Windows
Step 0: Check whether your computer system is 32-bit or 64 bit.
Select the Start button, then select Settings > System > About . Open About settings.
Note: We will see the process to install 64-bit version of Anaconda now.
1. You can easily search for “Anaconda navigator” on Google or click on the link here.
2. Click on the “Download” button. You will be taken to the bottom of the webpage where you have to select the correct version. If your computer system is new, you can click on the 64-bit version.
Note: There are certain libraries like TensorFlow which require a 64-bit version of Python. Please check your system configuration and if possible, install the 64-bit version.
For Windows: Select the Start button, then select Settings > System > About . Open About settings.
For Mac: Click on the Apple menu and click on “About This Mac”. Further, select “More Info”. You will see the processor name. Unless it is an Intel Core Solo or Intel Core Duo, it will be a 64-bit system.
3. Once you click on the correct version, your download should start immediately.
4. Once the download has been completed, click on the setup file. The following dialog box will open.
5. Click “Next”.
6. The standard terms and conditions are provided. You can click, “I Agree” to proceed.
7. The next dialog box will ask if you want to install Anaconda on your user profile or all users. If you share the system with others, you can simply select the “Just me” checkbox and click “Next” further.
8. Now you can select the destination folder and click, “Next”.
9. Finally, select the “Register Anaconda3 as my default Python 3.8” checkbox and click “Install”.
Note: If you are an advanced user, you can select the first option. However this might lead to issues and we recommend selecting the second checkbox, i.e. “Register Anaconda3 as my default Python 3.8”.
10. Click on the “Next” button and you are done. Anaconda is installed.
We will now see the installation process for a Mac. You can skip this section if you have already installed Anaconda.
Install Anaconda on Mac
1. You can visit the download page of Anaconda installers here.
2. You can select the “64-Bit Graphical Installer” and download the set-up file. Once the file is downloaded, open the file and the installation process will start.
3. You can simply click on “I agree” to the terms and on the “Installation Type” screen.
4. If you do not want to install Anaconda in the default location, you can click on “Change Install Location…” and select the appropriate location. Otherwise, click “Install”.
5. Finally, once the installation is complete, you will find the summary page. Click on “Close”.
Running Anaconda on Your system
1. Anaconda is built just like any other software. You can easily search for Anaconda on your windows search bar, or open terminal on Mac, and click on the icon for Anaconda Navigator.
2. Anaconda Navigator gives you a lot of apps for making your programming journey easy. You can use Jupyter Notebook for opening Python notebooks.
3. All Quantra courses provide strategy codes in the form of Jupyter notebooks.
4. Just like the windows command line or the terminal on Mac, Anaconda also has a command line that can be used to install Python libraries. You can access the Anaconda command prompt by searching for “Anaconda Prompt” on your search bar.
Check Python Version
You can check the Python version installed by simply typing “python” and pressing the “Enter” key.
There might be some occasions when the setup process is not as smooth. Read on to explore some common issues and how to resolve them.
Troubleshooting Installation issues
One of the common issues we have encountered while installing Anaconda is the issue of file location. It is generally recommended to install Anaconda where you are installing Python.
Sometimes, your system might not be able to install Anaconda due to the system requirements. In that case, you can pick one of the earlier versions from this link.
Now that Python and Anaconda have been installed, we will modify the Python environment to the same environment used by Quantra. Let us see the process in the next section.
Set the Python Version and Install Packages
Usually, there are times when you want to work on a project which requires different versions of libraries and packages. In this type of scenario, it is better to use a virtual environment with all the packages you need. This ensures that if you have various notebooks with different package requirements, they can all run in different environments without any errors.
You can also replicate another person’s environment without modifying your own environment. This way, you don’t have to install and uninstall various libraries.
Let us not now create an environment in Python using the Anaconda prompt.
Creating an Environment with a Specific Version of Python
1. You will first open the Anaconda Command Prompt or terminal.
2. Type “conda create -n py39 python=3.9.5”.
3. You will get a list of files that are required to be downloaded to set up an environment with the name “py39”. Press the “y” key and then press the “Enter” key.
4. Once all the packages and files are downloaded, you will get a message telling you how to activate and deactivate this environment.
5. According to the instructions, activate the “py39” environment by typing the following text, “conda activate py39” and press the “Enter” key.
Note: You should always activate the correct environment before you start working on your strategies. Initially, the default environment is named “base” and the ones you create will be named however you seem fit to.
6. Before we download the other packages, let us download Ta-Lib first. Ta-Lib is a technical analysis library that hosts a large database of indicators that can be used on your dataset with minimal lines of code. You have to enter the indicator name and pass the required parameters, and voila! Ta-lib analyses your dataset in a matter of minutes.
To install Ta-lib in the Anaconda prompt, enter the text, “conda install -c conda-forge ta-lib”, and press the “Enter” key.
7. The Ta-lib library might require additional files, which will be downloaded as well. You will have to confirm the download by pressing the “y” key and then press the “Enter” key.
Note: Before we start with the installation process, we have to check which libraries will be needed to run our Python notebooks. If you are keen on machine learning, and want to explore in-depth about the world of AI and machine learning, you will have to install ML libraries such as Keras or TensorFlow.
We have created a text file that lists down most of the Python packages and libraries required to run a machine-learning-based trading strategy with ease. This text file can be downloaded from the link here.
If you are not going to use ML in your strategy, you can download and use another text file that does not list ML libraries. This text file can be downloaded from the link here.
Do remember that you should place the “requirements.txt” file in the same directory where you are running the command prompt from.
For example, if I have downloaded and installed Python and Anaconda in the file directory “C:\Users\rekhi”, then I should place the text file here.
8. Finally, we will install the necessary files in the “requirements.txt” file by using the following input instruction, “pip install -r requirements.txt” and pressing the “Enter” key. Since there are some large packages, this step will require 10-15 minutes.
Congratulations! You have successfully recreated the Quantra environment on your local machine.
9. You can use the command “jupyter notebook” to open the Jupyter notebook in the Quantra environment.
This will open a web browser window and you can see all the notebook files in the tab.
Now you can open any notebook and start working on it right away.
While installing the packages, you might get an error message saying “Windows C++ or higher is needed to install packages. You can simply copy-paste the link and download the build tools.
Click on the setup file and make sure you have selected the “C++ build tools” checkbox and further selected the 5 “optional” tools as well, as seen in the screenshot below.
Follow the instructions during setup and the required packages will be installed. You can then try installing the Python packages again, and this time the set-up will be complete.
Once the environment is set up, you can run all the notebooks in the downloaded zip files from your course.
For an optimum user experience, you should extract all the files to the root directory where Python is installed. For example, if the Python installation is in the folder “C:\Users\rekhi”, then extract the files to this folder itself.
Let us now see the different files present in the zip folder of a Quantra course.
Exclusive Section only for Quantra Learners
How to run files included in the download section of Quantra Courses
In every Quantra course, you will find the downloadables folder in the last section. This folder contains the Jupyter notebooks and the code files used in the course. There are folders such as data_modules which store the required data files by the strategy notebooks. The folder structure is such that the main folder contains the name of the section and the strategy notebooks, with the same name as mentioned in the course, are present in that folder.
To run any file, follow the below steps:
1. Go to the Anaconda prompt.
2. Change the environment to the one we set up in the previous step.
3. Type Jupyter notebook as shown below:
4. Go the browser where Jupyter notebook server is working.
5. Locate the folder from the Jupyter NB navigator, where the .ipynb file is present. For example, “\Downloads\Python-for-Trading-Basic-Resources\Importing Data and Data Visualization”.
6. Open the Jupyter notebook.
7. Run the Jupyter notebook by pressing Shift + Enter.
You can see the steps in the gif below:
What are different file types in data_module?
Data files: These are text files that contain alphanumeric data. Quantra provides certain financial data in the form of CSV, bz2 or pick files so that the user does not have to download the data every time from a web resource.
For example, the “infy_dv.csv” file could contain the OHLC data for the year 2018.
These files can be of the format csv, bz2 or pick. But the way to use them is the same, using the pandas method.
Modules or file with an extension of .py: Quantra collates all the code which is used repeatedly, such as calculating the strategy returns, in a function, which is stored in the modules. In this way, instead of writing the same lines of code in every notebook, you can simply call the function defined in the module file.
In this way, you have received all the data files as well as the strategy notebooks for your analysis. Thus, you don’t have to spend time making sure everything runs. Rather, you can devote your time optimising and tweaking your strategy, in order to start trading systematically.
1. Before you set up the environment, make sure the Python version which you have installed is the correct one. You can check the version by typing “python” in the command prompt.
2. If you face a “ModuleNotFound” error, then you can fix this by installing the required package.
3. If you face a “FileNotFound” Error, you may not have written the correct file path.
What to do if you still face an error in running the code provided in the Quantra course?
You can always post your query to the community page where like-minded people like you are learning Python and answer each other’s queries.
How will you access the CSV files stored in the data_modules?
You can go to any folder and open the Jupyter notebook. In that you need to write following code to access the file
1. Import Pandas library
import pandas as pd
2. State the path of the data file correctly.
For example, “infy_dv.csv” is stored in file location “C:\Downloads\Python-for-Trading-Basic-Resources\data_modules”, and the notebook “Data Visualization.ipynb” is in the folder “C:\Downloads\Python-for-Trading-Basic-Resources\Importing Data and Data Visualization”.
You would need to go one folder above the notebook location and enter the “data_modules” folder. Here ”..\” indicates we are going one folder above the notebook location.
Then, you will write “..\data_modules\name of the file” you want to access.
The code will be as below:
infy = pd.read_csv('../data_modules/infy_dv.csv')
Here, infy is the dataframe where we store the contents so that we can analyse it later.
How to access the functions stored in the data_modules folder?
The functions can be accessed in the same manner as importing libraries. But there are a few extra steps. Let us go through them now:
1. Let’s say the function “analyse performance” is stored in the “ST_functions_quantra.py” file. As specified earlier, the module is in the data_modules folder. The location is given as .“C:\Downloads\Python-for-Trading-Basic-Resources\data_modules”, and the notebook “Data Visualization.ipynb” is in the folder “C:\Downloads\Python-for-Trading-Basic-Resources\Importing Data and Data Visualization”.
2. You would first write the line
“import sys” which will help you in importing functions in modules.
3. You would then write “sys.path.append(“..”)” so that we can add a specific path for the interpreter to search.
4. Finally, we specify the name of the file and function we want to import. Here the name of the file is "quantra_functions" and the function name is "analyse performance".
Thus, the code will be as follows
from data_modules.quantra_functions import analyse performance
In this manner the function will get imported.
You have started the journey in the world of algorithmic trading. First, you installed Python and Anaconda Navigator. You have then installed all the necessary libraries which will aid you when you are performing backtesting and analysing your own trading strategy. You have also seen the different data files which are used in Quantra courses so that you can minimise the time required to retrieve data while backtesting. Finally, the environment you have set up is the same as Quantra, and thus, you will have minimal errors while using any of the course strategy notebooks.
Disclaimer: All data and information provided in this article are for informational purposes only. QuantInsti® makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information in this article and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.