Trading Algorithms VI – The Stages of a trading algorithm
In this video, we will discover the different parts needed for a complete trading system.
One of the most common systems involves the crossover of two moving averages, a short- and a long-term SMA. Let’s do a system based on this idea.
Creating a trading algorithm involves at least two stages: the entry logic and the trade management logic, and the position sizing logic.
The Entry Logic
The entry logic sets the rules for entries. The logic can be subdivided into two sections: the entry signal and the Filter or Trade setup.
The entry signal
An entry signal is a moment in time when something happens in the asset. Entries can be MA crossovers, level breakouts, bullish or bearish candlestick formations, and so forth.
A filter is a condition imposed for the entry signal to be valid. For instance, you can allow a MA crossover to the long side only if the main trend is up. Then, you can programmatically define the primary trend, and this is the filter. For instance, we could describe an upward direction when the price is higher than the +1 SD line of a Bollinger Band ( in which we set the bands to 1 SD instead of the standard 2SD). We could also say that the upward trend is the price above its 200-period MA, or when there are higher highs and higher lows. A down-trend can be defined using the opposite logic.
On mean-reverting assets, an interesting filter might be overbought/oversold indicators such as RSI, Percent R, or Stochastics curving against the current move, allowing then use of candlestick reversal signals.
We can also add a filter that excludes trades whose projected reward/risk ratio is below one.
As a caveat, the higher the number of conditions, the higher the probability of over-optimizing it. The best designs are those with a few parameters. Also, the higher the number of filters, the less the system will trade. Thus, not always a filter improves a raw signal.
When building your trading system, a sound methodology starts with raw entry signals and a time stop at a determined number of bars. If the entry has an edge, it will be proved by higher than 50% profitable signals after 5-10 bars.
The trade management Logic
Trade management logic takes care of open trades. It is constituted of at least a stop-loss logic and a take-profit logic. It may include other decision steps, such as break-even logic and trail stops.
We have already published several articles on stop-losses. There are several ways to set a stop-loss level. We can do it using a multiple of the Average True Range of the asset, using the last swing low (or high in the case of a short-trade), or by statistically optimizing the distance using John Sweeney’s Maximum Adverse Excursion (MAE) concept.
Trail stops are also a recurrent idea in trading, although the developer should test them and assess if they really improve the results. The same is valid for the break-even logic. Both concepts seem logical and mind relieving, but I have yet to find their utility to improve a trading system.
In some trading systems, a time stop can be handy. A time stop closes if the trade is not profitable after a certain period or a specific number of bars.
Take profit logic can also be varied, from dollar-based stops to stops based on key levels, supports, and resistances or pivots. A take-profit condition may be set, too, when a signal opposite to the current trade happens, such as an MA crossover against the trade, the price below the -1SD Bollinger line on longs or over +1SD line on short positions.
Take profit code can be added for scaling out the trade, letting a percent of the original position open to ride the wave and improve profits when your trade is right.
Position Sizing logic
The position size logic is a final step that involves setting up the right trade size for the trader’s objectives. This step should be used only in real-time trading, not during the definition, optimization, and evaluation of a new trading system.
During the definition step, a trading system must be used with one trade unit, and its results normalized to its risk, so instead of dollar profits, it should produce a stream of multiples of R, a standard one-dollar risk.
Position sizing logic is key to maximizing the returns of a system and limiting the max drawdown to the levels desired by the trader.
We will further develop these concepts in the coming videos, with specific algorithms demonstrating how to create them properly.