Python is one of the most popular programming languages in the world and almost a must-have skill if you are going into finance. However, there are still lots of people that don’t know how to use it. So, how do you get started with Python to make a trading strategy and backtesting?
This article is the first of a series of articles about how to get started making a trading strategy (and begin backtesting) using Python. We will start from the basics and build our way up. By the end of the series, you should know how to use Python like a professional and use it to backtest trading strategies and build financial models.
Also, if you are looking for a profitable investment strategy, you might want to check out that clickable link. We have hundreds of strategies with specific trading rules and backtests!
Today we are going to begin learning what Python is, where to run your code, what variables are, and the different data types that exist in Python. All this is needed in order to make a Python trading strategy.
What is Python? How to use it for a trading strategy?
Python is a general-purpose programming language that can be used for virtually anything, from building websites, making applications for analyzing data, automating tasks, making financial models, making trading strategies, doing backtesting, and running trading strategies live (automatically).
Python was developed by Guido Van Rossum in the late 80s and, contrary to popular belief, the name Python doesn’t come from the snake but from the British comedian Monty Python!
Python has become very popular in recent years for a variety of reasons.
First, it has a simple syntax that mimics natural language, making it easy to learn and understand.
Second, it’s beginner friendly, making it popular for entry-level coders.
And last, it has a vast and active community that contributes to the Python pool of modules and libraries and acts as a helpful resource for other programmers. The latter is extremely important for a coding language. There is plenty of free code available.
These reasons, among others, are why Python is so big in the financial world, especially among investors and traders that want to backtest their trading strategies.
Python-related resources
We have written many articles about Python, and you might find these interesting:
- How To Download Data For Your Trading Strategy From Yahoo!Finance With Python
- Best Python Libraries For Algorithmic Trading (Examples)
- Python Trading Strategy (Backtesting, Code, List, And Examples)
- How To Measure Skewness Of A Trading Strategy Using Python
- Python Bollinger Band Trading Strategy: Backtest, Rules, Code, Setup, Performance
- Python and Trend Following Trading Strategy
- Python and RSI Trading Strategy
- Python and Momentum Trading Strategy
- How To Build A Trading Strategy From FRED Data In Python
- Python and MACD Trading Strategy: Backtest, Rules, Code, Setup, Performance
- How To Measure Skewness Of A Trading Strategy Using Python
- How To Do A Monte Carlo Simulation Using Python
Where do I run my Python code?
You must download Python and an IDE (Integrated development environment) to run a Python program on your computer.
However, it is not only beyond this blog post to explain how to download Python and an IDE but also tough to understand if you are new to programming and not very technical.
Google Colaboratory as an option for beginners
This is why Google Colaboratory is a much better option for beginners. Google Colab is a free tool that allows users to write and run Python code through the browser. You don’t need to worry about installing anything, just coding. The code is stored in your google drive, and you can share it with anyone much more quickly.
To get started, just go to your google drive, click on New, and search for Google Colaboratory.
Now that we are set up, it is time for some coding to start making trading strategies.
What are variables?
The essential thing in Python is variables.
A variable is a container for storing data values. In other words, if you imagine a box filled with books in real life, in Python, the box would be the variable (something where you can store things), and the books would be the data values (but keep in mind it can store many other things as well).
A variable is defined simply by naming it, followed by the equal sign and the value you want it to contain. For example, the variable named stock contains a value that, in this case, is the text “AAPL”:
When creating and using variables in Python, you should keep the rules below in mind. Otherwise, the code could cause errors or be difficult to understand.
- Variable names can only contain letters, numbers, and underscores. They can begin with a letter or an underscore but not with a number.
- Spaces are not allowed in variable names.
- Avoid using Python keywords and function names as variable names. For example, print, for, if, return, etc.
- Variable names should be short but descriptive of what they contain. For example, name is better than n.
Data types in Python
Data types are the classification of the different kinds of values.
Remember when we said that a variable stored a value? Well, there are many different kinds of values in Python. For example, a number is not the same data type as a list.
How do we know what data type a variable holds? With the type() function.
If you take the type() function and write the variable’s name between the parentheses, it will tell you what type of value the variable holds.
In Python, data types are divided into 5 classes:
Numeric data types hold numeric values, aka numbers. There are 3 kinds of numeric data types:
- Integers(int): It contains positive or negative whole numbers (without decimals).
- Floats(float): Unlike integers, Python calls any number with a decimal point a float.
- Complex(complex): They are composed of real and imaginary parts. The real numbers can be either integers or float; the imaginary number is represented with the letter j.
With numeric data types, you can add(+), subtract(-), multiply(*), and divided(/) numbers in Python, among others. Moreover, it supports the order of the operation.
Sequence data types are the ordered collection of similar or different data types. Sequences allow storing of multiple variables in an organized and efficient way. There are three kinds of sequence data types:
- Strings(str): a string is simply a series of characters. Anything inside quotes in Python is considered a string. You can use single or double quotes.
- Lists: a list is a collection of items in a particular order. In Python, square brackets indicate a list, and commas separate individual elements in a list. You can put whatever you want in a list.
You can access any list element by telling Python which position(index) the item is desired. To access a list element, you need to write the list’s name and the item’s position in square brackets.
Keep in mind that the index positions start at 0, not 1. For example, if I only want the first name of the list, I would write the following code:
- Tuples: The only difference between a tuple and a list is that a tuple cannot be modified once created. In other words, tuples are immutable. Instead of square brackets, parenthesis indicates a tuple, and commas separate values. You can access an item in a tuple just like a list.
Booleans are used to represent the true value of an expression. For example, 5 > 4 is True, whereas 2 > 8 is False. There are only two boolean values: True or False.
A set is like a list with two differences: unordered and no duplicate values. Sets are created with the function set(), and the values inside it are placed between square brackets and separated by commas.
As you can see below, Mexico is repeated, but it only appears once when we print the set.
A dictionary in Python is a collection of key-value pairs. This means that each key is connected to a value.
A dictionary is created with brackets, the key is separated from the value with a colon, and the comma separates the key-value pairs.
It may be easier to think of the key as a variable. Inside a dictionary, you can create keys(read variables) and associate a value to it. And you can access the value associated with a key by calling the key.
For example, the variable stocks is a dictionary that contains two keys: AAPL and MSFT. Each key has a value associated with it, in this case, the price per share. To access the price per share of Apple, we need to write the name of the dictionary and the key between square brackets.
Related reading:
Python and Trend Following Trading Strategy
Python and Momentum Trading Strategy
Python and RSI Trading Strategy
How to get started with Python to make a trading strategy – conclusion
To sum up, today we learned what Python is, how to run our code, what variables are, and the different data types available in Python. It’s a solid start, but there’s still much to learn to write good Python code for backtesting trading strategies.