Stochastic Oscillator Trading Strategy Backtest in Python

I thought for this post I would just continue on with the theme of testing trading strategies based on signals from some of the classic “technical indicators” that many traders incorporate into their decision making; the last post dealt with Bollinger Bands and for this one I thought I’d go for a Stochastic Oscillator Trading Strategy Backtest in Python.

Let’s start with what the Stochastic Oscillator actually is; Investopedia describes it as follows:

“What is the ‘Stochastic Oscillator’
The stochastic oscillator is a momentum indicator comparing the closing price of a security to the range of its prices over a certain period of time. The sensitivity of the oscillator to market movements is reducible by adjusting that time period or by taking a moving average of the result.

BREAKING DOWN ‘Stochastic Oscillator’
The stochastic oscillator is calculated using the following formula:

%K = 100(C – L14)/(H14 – L14)

Where:

C = the most recent closing price

L14 = the low of the 14 previous trading sessions

H14 = the highest price traded during the same 14-day period

%K= the current market rate for the currency pair

%D = 3-period moving average of %K

The general theory serving as the foundation for this indicator is that in a market trending upward, prices will close near the high, and in a market trending downward, prices close near the low. Transaction signals are created when the %K crosses through a three-period moving average, which is called the %D.”

I want to test two different implementations of the Stochastic Oscillator:

1) A sell entry signal is given when the %K line crosses down through the %D line, and the %K line is above 80. The exit signal for this short position is given as soon as the %K line crosses back up through the %D line, irrespective of the actual value of the %K line when this happens. A buy entry signal is given when the %K line passes up through the %D line, and the %K line is under 20 at that time. The exit signal for this long position is given as soon as the %K line crosses back down through the %D line, irrespective of the actual value of the %K line when this happens. In this implementation there are 3 possible states – long, short, flat (i.e. no position).

2) In this implementation there are only 2 possible states – long or short. Once a position is entered into, the position is held until an opposite signal is given, at which point the position is reversed (i.e from long to short or from short to long). For example, if a buy entry position is signalled by the %K line crossing up through the %D line whilst the %K line is below 20, the position is held until the %K line crosses down through the %D line whilst the %K line is above 80.

So lets get to some code and try out the strategy on Apple Inc. stock.

Now let’s create a plot (with 2 subplots) showing the Apple price over time, along with a visual representation of the Stochastic Oscillator.

Let’s plot the position through time to get an idea of when we are long and when we are short:

So we see that our returns are indeed positive at least, but we could have done much better by just buying and holding Apple stock, which is slightly disappointing.

So, on to our second implementation of the strategy – the one where we are either long or short. I will just paste the whole code in one go and present the equity curve at the end:

So unfortunately this implementation give us a worse outcome, with the overall return being pretty strongly negative.

So, once again we have shown that using a simple technical indicator such as the Stochastic Oscillator isn’t enough to generate superior returns (shock horror!), at least for Apple over the back-tested period. I would imagine that stocks for which this strategy worked in a robust enough fashion to actually rely on would be few and far between. Doesn’t hurt to look though…

Until next time!

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Reddit
Written by s666