Skip to content

Instantly share code, notes, and snippets.

@linuxluser
Created February 26, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxluser/32e670f7df99bcb4146a457f4772f909 to your computer and use it in GitHub Desktop.
Save linuxluser/32e670f7df99bcb4146a457f4772f909 to your computer and use it in GitHub Desktop.
def TrendAnalysis(series, smoothing):
"""Analyze direction of trend and for how many periods in that direction."""
# Get differences, smoothing them out to avoid insignificant "wiggles"
change = series.diff().rolling(window=smoothing).mean()
# Find the direction changes and get the current direction count
signs = list(reversed(np.sign(change).values))
direction = signs[0] # current direction
periods = 1
for sign in signs[1:]:
if sign != direction: break
periods += 1
# Sum the total change seen for the number of periods
total_change = abs(sum(list(series.values)[:periods]))
return direction, periods, total_change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment