Trading Algorithms -From Ideas to code
As we have already said, computers are dumb. We need to explain to them everything. Moreover, digital computers are binary. They only understand ones and zeroes. Nothing else.
Compilers and interpreters
To make our lives easier, we have created interpreters and compilers, able to translate our ideas into binary. Basically, both do the same job. Compilers produce a binary file that a computer can later execute, whereas interpreters translate each instruction as it comes in real-time.
From idea to the algorithm
Usually, traders think about when to enter and exit trades. An example brought by George Pruitt in his book The Ultimate Algorithmic Trading System Toolbox is the following. A trader wanted a code to enter the market and told him:
” Buy when the market closes above the 200-day moving average and then starts to trend downward and the RSI bottoms out below 20 and starts moving up. The sell-short side is just the opposite.”
No computer would understand that. In this case, the idea was partially defined, though: To buy when the price was above the 200-day SMA, and the RSI crosses down below 20. But what did he mean by “downward trend”? or “starts moving up”?
The first step to make a trading algorithm is to create an approximation to the code using plain English, but with more concise wording.
In the example above, Pruitt says that he could translate the above sentence into the following pseudo-code after some calls to his client:
The number inside brackets represents the close x days before the current session; thus,  is yesterday’s close.
In the pseudo-code, close below close and close  below close  and close below close is the definition of a downtrend. But we could define it differently. What’s important is that a computer doesn’t know what a downtrend is, and every concept needed for our purposes should be defined, such as a moving average, RSI, and so forth.
The next thing we need to do is move the pseudo-code to the actual code. There are several languages devised for trading. MT4/5 offers MQL4/5, which are variants of C++, with a complete library for trading. Another popular language is Easylanguage, created by Tradestation, which is also compatible with other platforms, such as Multicharts. Another popular language among quants is Python, a terrific high-level language with extensive libraries to design and test trading systems.
The code snippet above creates a Python function that translates the above code idea. In this case, the myBuy function must be told the actual asset to buy ( which should point to the asset’s historical data), and it checks for a buy condition. If true, it will return a label for the buy and the level to perform the buy, the next open of the asset in this case.
Systematic or discretionary?
The steps from idea to pseudo-code to code is critical. If you do not have a working algorithm, there is no way you could create a systematic trading system. But this is only the beginning. Creating a successful automated trading system is very hard and involves many developing, testing, and optimizing cycles. The market shifts its condition, and not always your system will perform. Then, you have to ask yourself if you’ll endure the drawdown stage until the market comes in sync with the system again.
Some systematic traders think that the best way to attack the market is to have a basket of uncorrelated trading systems, which are in tune with the market’s different stages: low-volatility trend, high-volatility trend, low-volatility sideways, high-volatility sideways, so your risk and reward is an average of all of them.
In the coming videos, we will dissect the steps to create an automated trading system. Stay tuned!