This is a Guest Post by Troy Bombardia of pythonforfinance.org

Most traders begin trading with discretionary trading strategies since these strategies are usually easier to understand. Just pull up a chart, overlay some indicators onto the chart, and voila! You can start to understand, analyze, and learn about the market from Day 1! But as your trading experience and knowledge accumulates over the years, you may want to level up your trading by looking at quantitative trading strategies. These trading strategies are more difficult to understand and can be quite difficult to create if you don’t have a background in computer programming. So if you’re interested in quantitative trading, I’m going to share with you how quants like myself use Python for trading.

### What is Python, and why not stick with Excel?

Python is a high-level programming language that’s more user and beginner-friendly than many other popular programming languages. Many quants write Python code to backtest strategies and execute their trades. Now you may be wondering, “what if I don’t know Python? Learning it sounds difficult, and I can just stick to Excel!”

You certainly can stick with Excel. Excel is great for backtesting simple trading strategies such as “go long when the S&P 500 is above its 200 day moving average, otherwise sell and shift into cash”.

Like drag-and-drop website templates, Excel is extremely user friendly for beginners. Every equation that you calculate can be done simply through pointing-and-clicking on other cells. Meanwhile, creating the same trading strategy using Python is more complicated and involves a more indepth understanding of Python code. So why learn Python and use it for trading?

While Excel is great for beginners, it isn’t very scalable the way Python is. Some of these problems can be mitigated with the use of Excel VBA, but VBA isn’t as functional as Python:

- Let’s assume I want to backtest a trading model that can simultaneously look at 1000 different stocks, and pick the 50 best stocks to trade. Backtesting such a model in Excel would be a nightmare, since it would take forever to work on 1000 columns of price data. Backtesting such a strategy is much easier in Python.
- Let’s assume that I want to optimize my trading model (while being careful of curve fitting). I recently did this to test 65,000 pairs of MACD settings to find the best one. If you try to do this Excel, it will take days if not weeks to find the best setting. But in Python, all you need to do is write a short piece of code. Let your computer execute the code and within a few minutes, you will have the answer you’re looking for.

### Why Python instead of other programming languages for trading?

If you’re new to programming, the sheer number of programming languages that you can use for quantitative trading may seem daunting. Python, C++, C#, Java, R, etc. Which language should you start with?

I personally prefer Python (and that’s what I started with). Python is one of the most widely used programming languages in quantitative trading since it’s a high-level language (which means that the code is easier to understand and hence, more user friendly). Other programming languages such as C++ are older and as middle-level languages, are harder to learn/use. In addition, Python has some great libraries such as Pandas which uses “dataframes” which look quite similar to Excel spreadsheets.

Aside from Python, Java is probably one of the most popular programming languages for trading, but is more difficult for beginners to learn.

### Python + Pandas

On its own, Python for trading is quite hard to use. That’s where the Pandas library for Python comes into play. From a layman’s perspective, Pandas essentially turns data into a table (or “dataframe”) that looks like an Excel spreadsheet. Once the market price data looks like a spreadsheet with Pandas, you can more easily run Python code for trading purposes (e.g. building trading models). This means that in order to effectively use Python for trading, you need to use Python + Pandas together.

### How can you use Python for trading?

There are many different use cases for Python when trading. The most notable use cases are:

**Backtesting & building trading models**

Many traders begin with discretionary trading strategies. But the problem with discretionary trading is that:

- You don’t know how well your trading strategy works through time and under different types of market environments. Does it work well in a bull market, a bear market, a choppy market, a strongly trending market?
- Even if your discretionary trading strategy worked well so far, how do you know it works because of skill and not luck? E.g. any strategy – even flipping a coin – would have worked very well in 2017 when the market went up nonstop. Relying on one’s “trading experience” can be misleading because unless you’ve been trading for 10-20 years, your experience is short. Your strategy might have succeeded so far not because of skill, but because the market’s environment and price pattern thusfar just so happens to fit the strategy you’ve employed.

That’s where quantitative backtesting comes in. Backtesting allows you to see how well your strategy works under different market environments, including market environments that you haven’t personally experienced yet. In a nutshell, backtesting stress-tests your strategy.

You can easily backtest simple trading models in Excel. But if you want to backtest hundreds or thousands of trading strategies, Python allows you to do so more quickly at scale. Moreover, some complicated strategies (e.g. ones that trade hundreds of markets) are hard to backtest in Excel, but are easy to backtest in Python.

**Optimizing trading models**

Let’s face it – all traders optimize their strategy to a certain extent. While over-optimizing your strategy or trading model is bad, doing some optimizing is still a good idea. Python allows you to optimize your strategy and look for the best indicator parameters with **for loops**. While this optimization might take days in Excel, it’ll just take a few minutes with Python.

**Automatically executing trades**

And finally, you can use Python to automatically scan for trade setups and execute trades. This will help you save time on a day-to-day basis when it comes to market analysis, and also helps you save them when implementing trades. For example, I’m working on a trading model right now that goes through 2000 stocks and trades 50 stocks at a time. Can you imagine scanning through 2000 charts every day? It would be a nightmare! Moreover, executing each of the 50 trades every single day is very time consuming. It’s far more efficient to allow my program to automatically execute the trading strategy.

*Thanks for reading this post! If you don’t know how to code, I highly recommend you learn this skill. Learning Python over the past year has helped my trading dramatically, and there are tons of free resources online or books you can read.*

This is a Guest Post by: Troy Bombardia you can follow him on Twitter at @bullmarketsco and you can also visit his website BullMarkets.co