The Balanced Tradertradingbalance.co.uk
Research · 2023-01-05

Double tops and back-testing in Python

By 2023-01-05

What a double top is, and how to back-test a double-top strategy on the ES futures in Python, step by step, with example code. A back-test is not a promise: test in a demo account before you risk real money.

Double topBack-testingPython

The idea

A double top is a bearish reversal pattern that forms after an uptrend. It is characterised by two consecutive peaks at approximately the same price level, with a moderate trough in between them. The pattern is completed when price falls below the low point of the trough.

The test

To back-test a double-top trading strategy on the ES futures using Python, follow these steps:

  1. Gather historical data for the ES futures. Get it from official sources: the exchange or a licensed data provider. Get data for a sufficiently long period, as back-testing over a shorter period may not give accurate results.
  2. Clean and prepare the data. This may include formatting the data, handling missing values and removing outliers.
  3. Identify double-top patterns in the data. Write a script that searches for two consecutive peaks at approximately the same price level, with a moderate trough in between them.
  4. Determine your entry and exit points. You may choose to enter a short position as soon as price breaks below the low point of the trough, and exit when price reaches a certain level of support or when a trailing stop-loss is triggered.
  5. Simulate trades on the data. Use your entry and exit points to simulate trades on the historical data and track the performance of your strategy.
  6. Evaluate the results. Calculate key performance metrics, such as net profit, return on investment and risk-to-reward ratio, to assess the viability of your strategy.

Here is some example code, written in a Jupyter notebook.

The code defines a function, backtest_double_bottom, that takes three arguments: the data to be used for back-testing, an entry threshold and an exit threshold. The entry threshold is the maximum allowed distance between the entry point and the high point of the trough, expressed as a percentage. The exit threshold is the minimum allowed distance between the exit point and the close of the trough, also expressed as a percentage.

The function then cleans and prepares the data, identifies double-bottom patterns, determines the entry and exit points, and simulates a trade.

You can then use this function to back-test the strategy with different variables and timeframes, by calling it with different arguments and data subsets. In the example, the strategy is back-tested on the entire data set, on the data for the first half of 2022 and on the data for the second half of 2022, with different entry and exit thresholds for each time frame.

It's worth noting that this is just one example of how you might back-test a double-bottom reversal strategy on the ES futures using Python in a Jupyter notebook, with the ability to change the variables and timeframe.

Example code: the back-test run on the whole data set, on the first half of 2022 and on the second half of 2022, with different entry and exit thresholds.

What it means for trading

Keep in mind that back-testing is not a perfect representation of how a strategy will perform in the future. Market conditions and investor sentiment change over time, so it's always a good idea to test your strategy in a demo account before risking real money.

Key takeaways

TakeawayBack-testing is not a perfect representation of how a strategy will perform in the future. Market conditions and investor sentiment change over time, so test your strategy in a demo account before risking real money.
TakeawayGet historical data for a sufficiently long period: back-testing over a shorter period may not give accurate results.
TakeawayAssess the viability of a strategy with key performance metrics, such as net profit, return on investment and risk-to-reward ratio.

Get the journal in your inbox

The daily journal, the week reviews and the Weekly Ahead, by email. Subscribers get the pre-market update first.