What is Algo trading and can I do it?

Samplecode

What is Algo trading?

Algo trading (Algorithmic trading) is the process of using a computer that has been programmed to follow a specific set of instructions for placing a trade. These instructions are programmed to be fast and generate the most profits with the least amount of risks. In doing so it should be able to make more money than a traditional human trader.

What programing language, platform or data you use is up to the developer. The Markets are affected by so many different factors some easier to code than others. You can easily code a program that will instantly calculate the Earnings Per Share – EPS or Price-Earnings Ratio – P/E Ratio for a stock. World events such as war and civil unrest, natural disasters and terrorism are harder to code into your algorithm, but can still impact the prices in the Market.

The top Algo trading companies like Optiver, KCG and Tradebot Systems have the best computers and software developers to develop algorithms that crunch the numbers on anything and everything and perform High Frequencies Trades in nanoseconds. But it’s an arms race and an algorithm that works great today may not work great tomorrow. Constant tweaks are required to the algorithm.

Who can Algo Trade?

Technically anyone can Algo Trade, but to do so successfully you need trading and operation capital. If you don’t have the capital there are other alternative. One option is to trade normally on the stock market and you create a standalone program that tells you whether to buy or sell. This can be done with something as simple as an excel VBA macro. The advantage here is that you are ultimately in control of buying and selling and you keep the cost to a minimum. The main cost being histrorical data.

The second option is to try QuantConnect (C#), Quantopian (Python) or a similar provider. They allow you to simulate your code using their data on their systems. They even have prizes now and then for the best algorithm. You can sign up to both for free and trial testing some code. The big advantage here is you can try coding an algorithm without spending any money at all. Great for someone who is interested, but doesn’t want to risk money if they find out they are no good at it.

Quantopian

So you have signed up for Quantopian and you have been given their default Sample Mean Reversion Algorithm. Now what?

The first thing you want to do is understand the code. So let’s take a few steps back. You will notice there are two main parts def initialize(context): and def handle_data(context, data):.   Underneath initialize is where you setup all the variables that your algorithm will use. Handle_data is where you write your algorithm.

Context is a persistent variable the will be used throughout the algorithm. It is created on start-up and holds all the variables we will create in initialize. When I say it is persistent I mean they will be created but can be changed later on in handle_data.

Some of the variables you might create in initialize are currentdate, currenttime or a list of the all the securities that you want to use in your algorithm. You will also need to place methods in initialize for example Set_universe().

Once you have setup all your variables and methods in initialize you need to create your algorithm. If you have ever coded before think of handle_data as your main method. You can almost think of it as a loop. Basically you will set up a frequency every minute, every day, every week or a combination. In each iteration of this frequency handle_data is called and you algorithm is run once.

So you could set it up so on Monday every week your handle_data algorithm is run once or you could set it up so that your handle_data is run every minute of every day.


Consider the simple code above. Sid is the Security Identifier number. So context.stocks is grabbing all the stock from stock with SID 24 to sock with SID of 46632. The algorithm in hangle_data is then saying for each stock in context.stock check to see if the stock has data, if it has data order 1 share.

Those are the very basics of how handle_data and initialize work. Quantopia have a heap of resources and the best way to learn is to get your hands dirty with the code. There are other sites to choose from like quantconnect. I just choose Quantopia because it uses python and people who don’t code find it easy to code in python.

The last thing to note is that when writing strategies, the number of lines of code that implement the strategy will be a lot more than the number of lines to write the strategy. So when starting off write very simple strategies and code them first.

Leave a Reply

Your email address will not be published. Required fields are marked *