How to Design Your Algorithmic Trading Strategy

In previous articles, we have illustrated how API trading benefits traders and explained how simple it is to get started. In this article, we will discuss a key element to API trading: the trading strategy. A trading strategy is a set of rules used to execute trades, which will vary depending on the trader’s approach to the markets. A strategy can be as simple as opening trades at market open and closing them at market close, or more complex, such as using a machine-learning algorithm to spot market anomalies as potential opportunities for profit. There are many schools of thought behind strategy development. Some traders use principles of fundamental analysis, like economic data and news events, to formulate their trading strategy. Others prefer technical analysis, which involves applying mathematical formulas to the price of an asset to evaluate current and future price changes. Whatever approach you choose, having a testable idea and a clear understanding of the logic behind the idea are key. In this article, we will provide a brief overview of strategy development using principles of technical analysis.

Principles of Technical Analysis: Momentum

Many technical trading strategies are based on principles of momentum or trend-following, which assert that historically well-performing assets will continue to perform well, and historically poor-performing assets will continue to underperform. Both economists and traders have heavily researched concepts of momentum strategies and produced evidence of positive returns. Early research dates back to the late 18th century when economist David Ricardo proclaimed that traders cut their losses short and let their profits run, which is the basis of modern-day trend-following strategies1. Jegadeesh and Titman explain the reason momentum strategies are profitable historically is due to “delayed reaction to firm-specific information2”. Looking to ride an existing trend to higher highs or lower lows, momentum traders often use technical indicators as tools to evaluate the momentum of an existing trend. These indicators show how quickly the price of an asset is moving in a particular direction and the volume behind the price movements. This information can then be evaluated by traders and used to make trading decisions.

Technical Indicators

Traders use technical indicators to predict the future behavior of an instrument’s price. Indicators are calculated by applying a mathematical formula to the price data of an instrument and are often displayed as data points on a chart for the purpose of visually analyzing price behavior. Below are a few examples of commonly-used technical indicators which are based on principles of momentum.

Moving Averages

A moving average (MA) is calculated by adding the price of an asset over N number of periods and dividing the result by N, producing a smooth trend line to help identify the overall price trend. There are many kinds of moving averages: a simple moving average plots the average price of an asset over the past N periods, while an exponential moving average (EMA) uses a calculation that gives higher weight to prices of recent periods. Moving averages can be calculated for any number of periods. A shorter moving average will be more responsive to price changes while a longer moving average will provide a smoother line, therefore it is common to combine a longer MA and a shorter MA to formulate a trading strategy or signal. When using the MA as a signal, one should use caution against having too few periods as a hypersensitive MA can lead to false signals, while a smoother MA may not detect some signals at all.


Featured by J. Welles Wilder in his popular 1978 book, the relative strength index (RSI) is an oscillator that measures the momentum of price movements3. The RSI oscillates between 0 and 100, with values below 30 generally considered oversold and values above 70 considered overbought. Oversold in this context indicates an instrument has been trending down and may be due for a correction or trend reversal. Conversely, overbought indicates an asset is trending up and may be nearing a correction. The overbought and oversold levels can vary, but the higher (lower) the overbought (oversold) levels used, the more sensitive the RSI will be. More information about the RSI calculation can be found here.


The Moving Average Convergence/Divergence (MACD) is an oscillator that uses a moving average and momentum oscillator to provide both trend-following and momentum analysis. It subtracts a longer-term moving average from the shorter-term moving average and fluctuates over zero as the moving averages converge and diverge. It is made up of three parts: the MACD line, the signal line and the MACD histogram. The MACD line is the short moving average less the long moving average, generally 12-day and 26-day moving averages are used. The signal line is the 9-day EMA of the MACD line, and the histogram is the MACD line less the Signal line. When the MACD line crosses above zero this indicates the short-term EMA is above the long-term EMA and upside momentum may be increasing. When the MACD line crosses below zero, this indicates the short-term EMA is below the long-term EMA and downside momentum may be increasing. The signal line trails the MACD line and signals bullish or bearish crossovers. A bullish crossover occurs when then MACD crosses above the signal line, while a bearish crossover occurs when the MACD crosses below the signal line4.

Using Indicators to Build an Automated Trading Strategy

Now that we understand the logic behind technical analysis and the indicators at work, we can explore the process of developing a strategy using technical indicators. Put simply, instead of watching a chart to identify when a set of moving averages cross, a trader can create a program to calculate the moving averages automatically and execute trades when one crosses over the other. Since technical indicators are mathematical, programming one into a trading strategy is simple. Simply choose the indicator (or indicators) you wish to utilize and add in the mechanics for opening and closing trades. A good rule of thumb is to write out your strategy logic and then translate that into your programming language of choice. For example:

Entry Logic: When the Fast EMA crosses above the Slow EMA, execute a buy order.
Exit Logic: When the Fast EMA crosses below the Slow EMA the existing order will be closed.

The next step is to translate the logic into code in the programming language of your choice. Then, the strategy can be further customized, for example, you may want to add parameters for risk management such as a take profit and stop loss. Once you have finished customizing the strategy it is ready for testing. Backtesting a strategy can help determine how the strategy would have performed in past market conditions and ensure the strategy is executing trades properly. Click here to see an example of a backtest of an exponential moving average (EMA) trading strategy using FXCM’s historical data, which can be accessed for free via REST API.

Once you have backtested your strategy, you may notice some adjustments that need to be made. Some adjustments are structurally necessary and must be fixed for the strategy to Function properly; say your strategy buys when you intend for it to sell. However, traders oftentimes make the mistake of adjusting the logic behind their strategy to fit the historical data, resulting in an overfit strategy that may perform well in backtests but will not necessarily perform well in live market conditions. Determining the appropriate amount of adjusting is a skill learned through experience, but newer traders should be cognizant of this common mistake and ensure they are testing on both in-sample and out-of-sample data. If your strategy performs noticeably better on your in-sample data than your out-of-sample data, you may have an overfit strategy.

Overfit strategies, or strategies that simply perform poorly, are something all traders face at some point, and should not be a source of discouragement. Finding a winning strategy can be a daunting but rewarding task. Start your API trading journey today by creating a free API-enabled demo account and connecting to the REST API. Then, backtest your ideas and turn them into a trading strategy using this free strategy template.

Questions? Email us at for more information.

1 Hurst, Brian and Ooi, Yao Hua and Pedersen, Lasse Heje, A Century of Evidence on Trend-Following Investing (June 27, 2017). Available at SSRN: or
2 Jegadeesh, Narasimhan and Titman, Sheridan, Momentum (August 29, 2011). Available at SSRN: or
3 StockCharts, Relative Strength Index (RSI)
4 StockCharts, MACD(Moving Average Convergence/Divergence Oscillator)

Trading Forex/CFD's on margin carries a high level of risk and may not be suitable for all investors as you could sustain losses in excess of deposits. Leverage can work against you. The products are intended for retail, professional and eligible counterparty clients. For clients who maintain account(s) with Forex Capital Markets Limited ("FXCM LTD"), retail clients could sustain a total loss of deposited funds but are not subject to subsequent payment obligations beyond the deposited funds and professional clients could sustain losses in excess of deposits. Prior to trading any products offered by Forex Capital Markets Limited, inclusive of all EU branches, FXCM Australia Pty. Limited, FXCM South Africa (PTY) Ltd, any affiliates of aforementioned firms, or other firms within the FXCM group of companies [collectively the "FXCM Group"], carefully consider your financial situation and experience level. If you decide to trade products offered by FXCM Australia Pty. Limited ("FXCM AU") (AFSL 309763), you must read and understand the Financial Services Guide, Product Disclosure Statement, and Terms of Business. The FXCM Group may provide general commentary which is not intended as investment advice and must not be construed as such. Seek advice from a separate financial advisor. The FXCM Group assumes no liability for errors, inaccuracies or omissions; does not warrant the accuracy, completeness of information, text, graphics, links or other items contained within these materials. Read and understand the Terms and Conditions on the FXCM Group's websites prior to taking further action. Read full disclaimer.

Be sure to read the next Financial article: Spotware Releases cTrader Open API v2.0