One challenge in creating trading algorithms that are based on trends are how to represent the trend as a number. How can a number tell what direction a trend is moving and how do you compare the steepness of a trend with another.
You can compare starting values to ending values but this doesn't tell the story of what happens in the middle. Average values tend to be influenced by spikes or knifes that could dampen the strength of the trend.

One solution is to look at the slopes between points in price in the graph and calculate the derivatives of these lines. Upward slopes have positive values and downward have negative. Calculating the overall sum of the derivatives gives you the overall direction of the trend. The major problem with this is that it may give you a false signal if the trend reverses during the course of the graph and it is not a complete solution. However this solution should work for shorter timeframes.

Scipy has functions to easily calculate this. As this exercise is only to look at trends, it is worthwhile smoothing out the price movements to see the overall direction.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d, spline
from scipy.misc import derivative

plt.style.use('ggplot')

s = pd.Series.from_csv('SPY20180216.csv')
x = s.index
y = s.values

# Simple interpolation of x and y    
f = interp1d(x, y)
x_fake = np.arange(x.min()+1,x.max(),0.1)
y_smooth = spline(x, y, x_fake)

# derivative of y with respect to x
df_dx = derivative(f, x_fake, dx=1e-6)

# Plot
fig = plt.figure()

ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)

ax1.plot(x_fake, y_smooth)

ax1.set_title('Smoothed Intra Day Chart of SPX')
ax1.set_xlabel("Minutes After Open")
ax1.set_ylabel("Price")

ax2.errorbar(x_fake, df_dx, lw=2)
ax2.errorbar(x_fake, np.array([0 for i in x_fake]), ls="--", lw=2)
ax2.set_title('Derivative of Price Changes')
ax2.set_xlabel("Minutes after Open")
ax2.set_ylabel("dy/dx")

txt = 'Sum of Derivative: {}'.format(df_dx.sum())

ax2.text(0.95, 0.01, txt,
        verticalalignment='bottom',
        horizontalalignment='right',
        transform=ax2.transAxes,
        fontsize=12)

You get an overall smoothed price movement graph which ties in with the derivative graph that shows if you are moving up or down. The sum of the derivatives gives you the overall direction of the trend. As you can see there is no overall trend on this graph so the value is low.

Quantified Trend

Once you got a number to quantify a trend, you can apply it to different trend types for different time frames.

Quantified Trend

I am currently in the process of creating a dataset to analyse price movements to see if there are patterns that can be traded on. This will be one of the features in the dataset. The past overall market index sometimes determines the current movement of stocks. Hopefully with this, we can determine how much.

References

The inspiration for this post was a Stack Overflow post here.