ALGOEXPERT
Trading Intelligence
How to do Algotrading?
An algorithm is a step by step definition of a strategy that has a target. Here the target is to win, to increase the invested money. Let us give an example:
We would like to increase our money in fx market by investing to EURUSD parity. Here is our trading algorithm:
​
-
Consider 1 minute chart of TSLA
-
At the end of every hour do this:
-
Calculate 20 period exponential moving average value and name it as EMA20.
-
Make a record of closing value of TSLA as CLS
-
If there exists TSLA stocks in the account and if CLS is smaller than EMA20 then
-
Sell all the stocks
-
-
If there is no TSLA stock in the account and if CLS is bigger than EMA20 then
-
Buy TSLA with all the available money
-
-
1. What is an Algorithm?
2. What is Algotrading?
Algotrading is implementing a trading algorithm via a programming language and executing it in a market. Algotrading is a trading method which is combination of software engineering, technical and fundamental analysis strategies. Simply, you need to tell a computer how to trade and computer trades on behalf of you. It decides when to sell, when to buy.
​
Telling computer how to trade is made via a programming language like C#, Pyton, Java. What to tell to computer is how to do technical and fundamental analysis and your experiences about markets.
​
Software programming knowledge would be enough to produce simple algorithms. However, software engineering is necessary to write sophisticated algorithms. A sophisticated algorithm runs very fast not to miss fast fading trading opportunities, it usually is an implementation of an Artificial Intelligence learning from mistakes and avoiding same mistakes for the future trades.
​
Here I will not mention about sophisticated algorithms. Instead, I will try to tell basic requirements to get into the algorithmic trading world.
3. How to do Algotrading?
3.1. Obtaining an Algotrading Environment
First you need a software development environment to write your code. Here you have three options:
​
1. You can use the environments provided by brokerages or by the companies who provide algotrading environments that can be plugged to several brokerages. This is the easiest way. You can only concentrate on your algorithm, you don’t need to take care of how to connect market and communicate with the computers of the market. Quantconnect.com is a good example of such an environment.
​
2. You can obtain a FIX connection from a brokerage or market. They will give you an IP address and port number to which you can connect your custom built algotrading software. You can download some open source FIX client and extend the source code and make your algorithm send the orders through FIX connection. FIX stands for financial information exchange. It is a protocol which is the industry standard in almost all the markets. In order to understand how FIX protocol work and how you can use it you can visit http://quickfixn.org/ . You may also download an opensource fix client from https://github.com/connamara/quickfixn/tree/master/Examples/TradeClient
3. You can obtain an Order Entry API (webservice or some dll) from a brokerage and you may build your algotrading coding environment upon this API. You need to contact your brokerage to learn if they provide such a service.
The easiest method is the first one and here further explanations will be made with the examples of quantconnect.com.
If you are done with setup of an algotrading environment, now you can start writing your algorithm.
Now you need to write a step by step definition of a trading strategy like in the ‘What is an algorithm?’ section above. Let’s say you wrote the algorithm above:
​
-
Consider 1 minute chart of TSLA
-
At the end of every hour do this:
-
Calculate 20 period exponential moving average value and name it as EMA20.
-
Make a record of closing value of TSLA as CLS
-
If there exists TSLA stocks in the account and if CLS is smaller than EMA20 then
-
Sell all the stocks
-
-
If there is no TSLA stock in the account and if CLS is bigger than EMA20 then
-
Buy TSLA with all the available money
-
-
​
Now you are ready with your algorithm and all you need to do writing this algorithm with a programming language again. Here is the C# translation of the algorithm. OnData function is a predefined function on Quantconnect lab and triggers when the constitution of a candle bar on a chart completes:
​
3.2. How to write an algorithm?
​In order the above code works MovingAverage class must be coded as well:
You can download complete source code here. In order run this code you need to open an account at Quantconnect and create a project there.
​
Now your algorithm is ready and you need to test your algorithm with past data of the market.
4. Backtesting
​Backtesting is running the algorithm against the past data and measure the performance. Here is the backtesting result of the sample algorithm above:
I have run the algorithm from 01.01.2017 to the present and set the starting balance as 100 000 dolars. Algotrading environments provide you some cofigureable parameters to enable you measure fertility of your algorithm. You may set start and end dates to test your algorithms against particular dates. You may set different amounts of virtual money to start trading.
The backtest result of the algorithm shows that, if we would run the algorithm starting from 01.01.2017 with 100 000 dolars, we would profit 60 020 dolars. We would pay 295 dolars commition to our brokerage and would still hold 12 479 dolars of unrealized profit which waits for the next price cross moving average to be realized.