Created
February 26, 2018 19:06
-
-
Save linuxluser/32e670f7df99bcb4146a457f4772f909 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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