Skip to content

Instantly share code, notes, and snippets.

@ZeldaZach
Created November 17, 2019 19:08
Show Gist options
  • Select an option

  • Save ZeldaZach/37e3c623553fb84a6583a44a37c7c347 to your computer and use it in GitHub Desktop.

Select an option

Save ZeldaZach/37e3c623553fb84a6583a44a37c7c347 to your computer and use it in GitHub Desktop.
import numpy
import pandas
def trend_line(index, data, order=1):
coefficients = numpy.polyfit(index, list(data), order)
slope = coefficients[-2]
return float(slope)
def get_trend(price_list):
data = {"prices": price_list}
df = pandas.DataFrame(data)
rolling_averages = df.rolling(window=2).mean()
trend_averages = [x[0] for x in rolling_averages.values][1:]
index = range(1, len(trend_averages) + 1)
resultant = trend_line(index, trend_averages)
print(resultant)
if resultant < -0.1:
return "Downward Trend"
if -0.1 < resultant < 0.1:
return "No Trend"
if 0.1 < resultant:
return "Upward Trend"
if __name__ == "__main__":
print(get_trend([11.30, 12.0, 12.0, 11.80, 12.40]))
print(get_trend([10.00, 9.99, 9.99, 10.00, 10.00, 9.99, 9.87]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment