Author: Leonel Silima
Date of Publication: 08/03/2023
This post may contain affiliate links, which means I may receive a small commission, at no cost to you, if you make a purchase through a link
You might have read a lot about some data processing techniques in other articles of ours. However, in this article we are getting to work to create our own tool for testing trading strategies.
What are the tools needed for this project?
Firstly, you need basic notions of python and programming logic. Next I will present step by step how to create the project. You just need to follow all the points mentioned.
1. Python Installation
You can install python easily and free and to both Windows and Linux systems. Click here to access the installer according to your operating system and follow the instructions.
Don't worry about the pip package because version 3 of python already has it built in. In fact, you just need to go to its command line and view the version of python installed as shown in the following example.
Ready to install python with its incorporated pip? Let's proceed with the installation and import of the necessary libraries for the project.
2. Installation of necessary tools for the project
The first step is to install the text editor (the place where you will write your code). My advice is to install anaconda because it brings all the necessary tools for python including editors like PyCharm, Jupyter and others. In this project we will work with Jupyter. Find here anaconda installer and enjoy its amazing tools.
3. Installation of libraries
As we mentioned above, python has a package called pip that is used to install, update, and uninstall libraries. So, we are going to use this tool in our text editor to install the libraries that we are going to use in this project. See the following example.
Pandas installation
Pandas is a library used for data analysis and manipulation, it is open source and easy to use, it was built on the Python language. For its installation you just need to type in your text editor the following command:
pip install pandas
TA-Lib installation
TA-Lib is a library used by trading software developers required to perform technical analysis of financial market data. Type following command to install:
pip install TA-Lib
Yfinance installation
Yfinance is a open source library developed by Ran Aroussi as a means to access the financial data available on Yahoo Finance. Therefore, install it with the following command:
pip install yfinance
Backtesting installation
It’s a python framework that uses historical data to infer the viability of trading strategies. Install it with following command:
pip install Backtesting
4. Library import
With all the libraries installed, we proceed with their import for this purpose, we will apply the following procedures.
Then we define two input variables where we will ask the end user to define his trading range with start and end date. As we can see the example below:
Now let's instantiate an object with our yfinance library to call our input variables and download the data, based on our input stock also defined by the user.
At this point we create a class that asks us to strategize to test.
Next we run backtesting to visualize the results of the strategy.
Moreover, to improve the visualization, let's call the plot function. In particular, thi is incorporated in the backtesting library, to print a graph based on the parameters we defined.
Finally, we will run stats to view the details of the results.
FULL CODE
RESULTS
As noted in the result above, we had input data from May 2022 to today's date and AMZN (amazon) is our stock. In your case, you can try any stock available on yfinance.
Last thoughts
This tutorial presented an overview of the capabilities of the backtesting library. Our next step in the future is to develop an installable tool for these purposes. Stay tuned for our updates!