Backtrader Stochastic Indicator Review

The stochastic indicator was tested and optimized against two entry/exit criteria over 12 years and in 4 markets, resulting in over 1,729 tests. After reviewing the results, one method clearly outperformed the other and had much more predicable behavior. In general the stochastic indicator appeared able to provide profitable outcomes even with fairly low strike rates. Additionally, the default settings ended in profitable outcomes (if only barely) in all but one market when tested over the two strategies. Interestingly, the overall performance and volatility of the results varied greatly when only small changes are made to either the parameters or the entry / exit criteria used.

Read on for the full stochastic review and statistics.

Introduction

The stochastic oscillator has been around for decades (since the 1950’s) and still remains a popular indicator in use today. It compares the closing price of an instrument to the instruments historical prices over a set period of time. As with most oscillators, it attempts to predict price turning points, effectively trying to call the highs and the lows of a price movement.

This Stochastic review post forms part of my series to test and compare Backtrader’s library of built-in indicators. For the series introduction, some notes on the test methodology and results summary table see here: https://backtest-rookies.com/2017/07/31/backtraders-best-forex-indicators/

Note: Results below do not reflect real world performance. As mentioned in the introduction and overview post (linked above), no commissions, leverage or margin are used during testing. The purpose of these tests are to compare the indicators to one and another where all other factors are equal.  For more information on how all indicator reviews will be performed, please click on the link above. 

Understanding the Stochastic Ocillator

The stochastic oscillator is measured with two lines. These lines are called %K and %D and they are calculated as follows.

%K = (Current Close – Lowest Low)/(Highest High – Lowest Low) * 100
%D = 3-day SMA of %K

You don’t have to fully understand the equation to use the indicator but it does help to have a basic idea of what it is showing you.

You may notice the %D line is actually a moving average of the %K line. That means the %D line will move slower than the %K line and as such, it gives us an opportunity for generating crossover signals for entries and exits.

To learn more about Stochastic oscillators take a look at:

  1. https://en.wikipedia.org/wiki/Stochastic_oscillator
  2. https://www.oanda.com/forex-trading/learn/technical-analysis-for-traders/stochastic/basics

Test Strategy

The stochastic indicator was tested using two slightly different methods. Both methods had the same entry criteria but different exit criteria. The first method follows the mantra that “the exit should be based on the same criteria that caused you to enter the trade.”. In other words, the exit is simply the opposite of the entry criteria. The second method aims to provide an opportunity to reverse the trade direction sooner and oscillate our positions with the indicator.

Entry

  • Enter Long:
    • When both the %K and %D lines are in the oversold area AND the %K line crosses above the %D line
  • Enter Short
    • When both the %K and %D lines are in the overbought area AND the %K line crosses below the %D line

Exit Method 1

  • Exit Long:
    • When both %K and %D reach the overbought area and AND the %K line crosses below the %D line
  • Exit Short:
    • When both the %K and %D lines are in the oversold area AND the %K line crosses above the %D line

Exit Method 2

  • Exit Long:
    • When both %K and %D reach the overbought area ONLY
    • Enter short if the %K line crosses below the %D line whilst still in the overbought area
  • Exit Short:
    • When both the %K and %D lines are in the oversold area ONLY
    • Enter Long if the %K line crosses above the %D line whilst still in the oversold area.

Stochastic Review – Results

Overall 1729 tests were performed across all four markets and parameter settings. Optimization was performed on each strategy for the following parameter ranges:

  • Period: 4 to 30
  • d_fast: 2 to 5
  • d_slow: 2 to 5

A summary, gallery of charts and series of statistic tables for each test method are below. The tables give an overview of the best parameter settings for each market along with the best / worst settings across all markets.

Each gallery shows 3 different charts:

  1. The 12 year annual returns of the best and worst performing tests
  2. The PnL curves for each market when altering the Period parameter. The d_slow and d_fast parameters remain as their defaults.
  3. The best, worst and average strike rates across all tests.

Method 1

This method of testing proved to be quite wild. As you will see in the PnL curves below, the performance is very choppy in all markets. Only small changes to the period parameter resulted in large swings from profitability to losses.

  • The most profitable test came from NZDUSD using 21,4,5 settings and had a strike rate of 46%
  • However, the best overall settings based on average PnL across all markets tested is 24, 3 ,3 with an average PnL of $255.
  • The default settings were profitable (but not optimal) across all markets.
  • A longer period (Above 16) provided the best settings in all markets.

You can download a cope of the test results here: Stochastic Review Method 1 Results

This slideshow requires JavaScript.

Best settings for each market

Marketperiodperiod_dfastperiod_dslow
AUDUSD1635
GBPUSD2734
EURUSD2532
NZDUSD2145

Default Settings Performance (14,3,3)

MarketPnLStrike Rate
AUDUSD108.6932.65
GBPUSD60.4330.21
EURUSD7.6835.64
NZDUSD398.9145.78

Best/worst settings on average across all markets

 PnLParam 1Param 2Param 3
Best255.35752433
Worst-263.022034

Method 2

The second strategy under test contrasted the wild results of method 1. For a start, the PnL curves actually resemble a curve (sort of!) and there seems to be less sensitivity to parameter changes. In addition, the stochastic indicator tested better in certain markets than others rather than being volatile in all markets.

  • This strategy is best suited to AUDUSD with consistent profits across most period parameter settings.
  • In contrast, NZDUSD appeared to generally perform poorly across the board.
  • Interestingly the best settings on average across all markets produce less average profit than method 1.
  • The best settings for each market all had a look back period of greater than 13.

A copy of the test results data can downloaded here: Stochastic review method 2 results

This slideshow requires JavaScript.

Best settings for each market

Marketperiodperiod_dfastperiod_dslow
AUDUSD1533
GBPUSD3034
EURUSD1354
NZDUSD1634

Default settings for each market

MarketPnLStrike Rate
AUDUSD472.7741.36
GBPUSD28.1242.65
EURUSD196.2241.75
NZDUSD-241.5433.48

Best/worst settings on average across all markets

 PnLParam 1Param 2Param 3
Best212.69751334
Worst-154.7752523