Automated Trading — Dynamic Trend Analysis (with Python)

B/O Trading Blog
7 min readJul 28, 2022

Photo by Ayadi Ghaith on Unsplash.com

A lot of trading strategies use moving average like SMA or EMA to identify trends. The strategy rules go something like “When the shorter period moving average crosses the longer period moving average, it is an upward trend” and vice versa.

This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.

Unfortunately this over simplification has several issues:

  1. First, we are reducing the trend to single point of information. In the process we are losing valuable information about the trend, e.g. length or strength.
  2. Since moving averages are used, we now have to deal with lag due to the moving average window.
  3. The moving average system is prone to false positives in case the lines cross multiple times. This is especially a problem when using moving averages with short windows as you can see from the chart below.

--

--

B/O Trading Blog

Blogging about algorithmic trading, Python utilities and passive income opportunities.