# Algorithmic Trading Models - Oscillators

*In the third article of this series, we will continue to summarise a collection of commonly used technical analysis trading models that will steadily increase in mathematical and computational complexity. Typically, these models are likely to be most effective around fluctuating or periodic instruments, such as forex pairs or commodities, which is what I have backtested them on. The aim behind each of these models is that they should be objective and systematic i.e. we should be able to translate them into a trading bot that will check some conditions at the start of each time period and make a decision if a buy or sell order should be posted or whether an already open trade should be closed.*

*Please note that not all of these trading models are successful. In fact, a large number of them were unsuccessful. This summaries series has the sole objective of describing the theory behind different types of trading models and is not financial advice as to how you should trade. If you do take some inspiration from these articles however, and do decide to build a trading bot of your own, make sure that you properly backtest your strategies, on both in and out of sample data and also in dummy accounts with live data. I will cover these definitons and my testing strategies in a later article.*

As we increase our level of complexity of algorithmic trading models, we will add the use of oscillators to our arsenal of strategies. Oscillators require a little more maths to calculate than our previous models, but most automated trading software’s should come with built in examples to calculate these for you.

We will begin this article by defining what an oscillator is and what makes them effective and the formulas used to calculate a few commonly used oscillators. We will then look at potential setups for a trading strategy, discuss the options we have for exiting a trade and include some diagrams along the way to better illustrate how these strategies might shape up for an actual currency pair.

In traditional physics, an oscillator is a circuit that produces a continuous waveform[1]. That definition is slightly adapted for the financial markets, in which we define an oscillator to be an indicator that fluctuates within a fixed range or around a specific value and is normally used to represent moments of strength and weakness in an asset. Essentially, what many oscillators aim to do is represent the current price relative to a set of x previous prices, to determine whether buyers or sellers are in control. Oscillators are usually used in conjunction with overbought and oversold ranges and supply and demand theory to try and predict future movements. Let’s have a closer look at that now.

For example, let’s take the Relative Stength Index, or the RSI indicator. I will explain the calculation of this indicator later, but for now what we need to know is that this indicator fluctuates between 0 and 100, with values above 70 traditionally meaning the asset is in an overbought state and less than 30 meaning the asset is in an oversold state, both of which tend to precede trend reversals. Why do they do this?

Well one reason is price action. After a sustained period of buying or selling, fluctuating markets in particular, such as forex or commodities, experience pullbacks, sometimes significant enough to then start trend reversals. Oscillators are designed to represent these sustained periods of buying or selling as overbought and oversold regions respectively, which is why when our indicator reaches these areas, there is normally a lot of trades placed in the opposite direction of the trend.

A second reason is trader sentiment. It’s always important to remember that every single trader has access to oscillator values for any asset. When an oscillator crosses into one of our significant regions, traders are immediately alerted. Many institutions may have have triggers placed at these regions to place trades a soon as the oscillator returns the desired value.

With that being said, let’s have a look at some commonly used oscillators.

## Oscillators

### The Relative Strength Oscillator (RSI)

The Relative Strength Indicator or RSI is an oscillator introduced by J. Welles Wilder Jr in 1978[2], that fluctuates between 0 and 100 to present bullish or bearish momentum and indicate potential reversal zones. It is calculated using the following formula:

Typically, we use a 14 period look back for the RSI. So as an example, if over the past 14 periods, we have 10 gainers with an average gain of 1.1% and 4 losers with an average loss of 0.5%, our RSI indicator calculation will return 68.75. The chart below shows how the RSI indicator matches up with market higher and lows.

### The Stochastic Oscillator

The Stochastic Oscillator aims to look at the closing price of the most recent period and return a value that represents that price relative to the previous x prices. Just like the RSI, it is used to generate overbought and oversold signals, but it differs in that these signals are generated at the 80 and 20 levels respectively. The Stochastic oscillator is calculated as follows[4]:

Typical look back values for the Stochastic oscillator include 14 and 5. Using a lookback of 5 can generate some very choppy signals, so should definitely be analysed with care. Sometimes, both of these oscillators together can be used together to confirm reversals or momentum, as the chart below shows.

In many diagrams, you may see two lines plotted on the Stochastic oscillator chart. The first line is called the %K line, calculated using the formula above. The second line, the %D line, is simply a 3 period moving average of the %K line. We will look at a strategy that involved both of these lines later on.

### The Moving Average Convergence Divergence Indicator (MACD)

The MACD oscillator differs slightly from the RSI and Stochastic in that, whilst it does oscillate, it does this around a value, specifically 0 and does not have an upper or lower bound. Similarly to the Stochastic oscillator, the MACD also has two lines, the second of which is a lagging moving average line of the first. The MACD line, as it is called, is calculated by subtracting the 26 period exponential moving average from the 12 period exponential moving average. During upward momentum, we would expect the 12 — EMA to be greater than the 26 — EMA and we will have a positive MACD value and vice versa during downward momentum. In a ranging market (a market with no definitive direction), we will expect to see the MACD close to the 0 line. As mentioned before, another line is added, called the signal line to the MACD chart, a 9 period moving average of the MACD line.

## Strategies

I will describe two potential strategies we can use with oscillators:

Overbought/Oversold Trend Reversal Strategy

Moving Average Crossover Strategy

The first strategy utilises our overbought and oversold regions. The first thing that we will note here is that our MACD doesn’t have these regions. We can create temporary regions using some form of calculation of recent volatility. From a analytics point of view, this might be an interesting strategy, to see if a dynamic overbought and oversold level generates better returns.

From that, I see this strategy going two possible ways.

The first is to place a trade as soon as our oscillator passes out of one of our regions after previously moving into it. This concept is important. For example, if an asset is bullish, we know that in a typical scenario, buying pressure might be beginning to wear off as the RSI moves above 70. However, this is purely a setup for our trade and not the actual trigger. We have not yet been provided with any indication of the price actually beginning to fall, in other words, we have not yet been informed that sellers are gaining momentum. It is very possible for our oscillator to be in one of the significant regions for a long period of time and therefore we should only make trends when we know a reversal is beginning. With this kind of trade, we can exit with usual average true range calculated stops, or when the oscillator goes into the opposite region (in our example, the oversold region below 30).

The second way that we can use the overbought and oversold regions is to look for a candlestick reversal indicator when we are in these zones. With this method, we avoid having to wait for the oscillator to cross back out of the zones and ensure that we enter the trade at a more optimal point. I will draw up an article to describe useful candlestick patterns in the future, but to give you an idea, when an RSI is returning a value greater than 70, we will look for bearish candles (bearish harami, shooting star or bearish engulfing to name a few), after which we will place a sell trade. Take a look at the chart above and see if you can find any candlestick patterns in the overbought and oversold regions. Just from a cursory glance, I can see a bullish engulfing in the first buy trade and two bearish engulfing candles in the next two sell trades.

The second method is to make use of the signal lines with the oscillator lines in a similar way to our moving average crossover from the previous article. The MACD and Stochastic are already presented with these lines and by taking the 3 period moving average of the RSI, we will have a line for all three oscillators. The standard rules will then follow. When the oscillator line crosses above the signal line, place a buy trade and when the oscillator line crosses below the signal line, place a sell trade. Our standard stops will then apply, namely a take profit and stop loss level as a multiple of the average true range or if the oscillator indicates a position reversal.

These are the basic principles of oscillator models. In a later article, we will look into how to code one of these models in MQL4 (the modified C++ language MetaTrader uses for algorithmic trading in the financial markets).

**References**

[1] https://www.electrical4u.com/what-is-an-oscillator/

[2] https://www.investopedia.com/terms/r/rsi.asp

[3] https://www.investopedia.com/terms/s/stochasticoscillator.asp

[4] https://corporatefinanceinstitute.com/resources/knowledge/trading-investing/stochastic-oscillator/

## Create your profile

## Only paid subscribers can comment on this post

Sign in## Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.