Laying the foundations
Before we can build any house, we must first dig the foundations. In our world, the foundations are made out of programming languages and backtesting frameworks / libraries. A framework is essentially pre-written code which already takes care of all the hard/boring stuff like streaming data, managing orders, tracking profit/losses, analyzing trades etc. This allows you to focus on writing your strategy.
So you might quite rightly wonder:
- Which programming language to use?
- Which backtesting framework to use?
There are many different programming languages that can be used for algorithmic trading and backtesting. Which one is right for you? Well to give a boring, generic answer, it depends on your aims and ultimate goals.
Below is my take on a few of the main programming languages that I have come across. It is not exhaustive or authoritative but I hope it can at least provide a starting point.
C++ is an object orientated programming language (basically means that the code focuses on doing things with objects rather than performing actions one by one). The language is both fast and pervasive across all industries. However, one drawback for the beginner is that can be a little harder to learn than some of the other languages we will talk about below.
Much of the older financial infrastructure is based on C++ code and since it can be very fast when coded right. This is a language that suits applications where speed is king.
If you are looking to get into HFT (high frequency trading) it is a good language to learn. (Although you won’t find much content to help you here! For now at least…)
R is something I don’t have experience with but I do regularly hear great things about it. Originally designed for statistical computing, this language is widely used by data miners and statisticians for data analysis.
If you are interested more in statistical analysis, this might be the language for you.
Python is another object orientated language and my language of choice. The high level nature makes it great one for beginners as the language is highly readable. It has become quite popular with the scientific community and people interested in finance whom may not have an deep computer science background but need to power of computers to perform research.
Python is a great all rounder and has some excellent 3rd party libraries which will make life easier as you ease into this field. We will look at some of those later in the series.
One drawback of Python and all higher level languages is that they are not as fast as languages like C++. So back to the point above, if you need raw speed, it might be better to spend your time learning C++.
For more information on Python see here:
Other honorable mentions include C# & Java. It appears that a lot of data providers have API’s supporting these languages and I heard on the grapevine a some financial institutions use these languages for parts of their infrastructure. (Though don’t quote me on that!). If you already have skills in these areas, there is no reason not to utilize them.
Back Test Frameworks
Like programming languages, there are an abundance of frameworks available for back testing and algorithmic trading. Below I will give a very brief overview of the big 3 backtesting frameworks available for Python (There are many more available). I am choosing to focus on Python as it is my language of choice. I wouldn’t offer much value discussing the others.
Please keep in mind, these are basic introductions and not full reviews. Detailed reviews may follow at a later date for some of the frameworks.
From the site:
“A feature rich python framework for backtesting and trading. Backtrader enables to focus on writing reusable trading strategies, indicators and analyzers instead of spending time building infrastructure.”
English might not be the developers mother tongue but python certainly is! Backtrader aims to be a pure python backtrading framework that is extensible as the user sees fit. This is a project with a great community, excellent support from the developers and very active development.
I am an avid user of backtrader and a good proportion of the blog will involve working with this framework. The getting started series even more so.
One killer feature of Backtrader is that it supports live trading through Oanda, Interative Brokers and Visual charts out of the box. With a few minimal tweaks, your backtest scripts can power live algorithmic trading bots.
“PyAlgoTrade is an event driven algorithmic trading Python library.”
PyAlgoTrade is feature rich backtesting framework with support for live trading of Bitcoins using bitstamp live data feeds. It is actively developed and has good documentation.
PyAlgoTrade is developed using python 2.7 so it personally it ranks lower on my todo list for the blog as I prefer to code with python 3.
“Zipline is a Pythonic algorithmic trading library. It is an event-driven system that supports both backtesting and live-trading.
Zipline is currently used in production as the backtesting and live-trading engine powering Quantopian — a free, community-centered, hosted platform for building and executing trading strategies.”
Quantopian provide a huge seal of approval for the frame work. You can bet that if a large enterprise focused solely on algorithmic trading is using the software to power their platform, it is going to have most, if not all of the features you need.
For those of you that have not already heard of it, Trading view started life out as a charting platform for technical analysis. It has since evolved into much more than that. Trading view have implemented their own programming language called “pine script” which allows you to perform backtesting on the platform and create your own custom indicators. On top of all this they support direct trading with a number of brokers such as Oanda, Forex.com, FXCM.
The platform itself is really excellent. I regularly use it for technical analysis and generating ideas for backtesting. The only downside is that your strategies and scripts are all written in a proprietary language (pine script) and are only usable on this platform. It is for this reason I chose not to include pine script in the languages section. Some other people may have concerns about storing their scripts / intellectual property on a 3rd party server.
A bit about pine script: Pine is designed to be a lightweight language, focused on developing indicators. It is not intended to be a full blown programming language. So if you are looking to delve really deep into algo trading, this might not be the best language for you. Also since it is a cloud based service (all the calculations are done on Tradingview’s web servers), it means they need to place limits on how many resources you can use. This is totally understandable as they need to ensure a good quality of service for all. Note that if you just want to test out some simple strategies than I think Tradingview is the perfect solution. It allows you to mix discretionary, automated, technical analysis and research all in one place and across all platforms. Pretty nice!
Too much information…..
Ok, Ok… Enough of the boring stuff. Lets get our hands wet.
In the next post we shall install python, setup backtrader and create a simple script! See Backtrader: First Script.