Last active
February 4, 2023 10:00
-
-
Save alvarobartt/98f94dcfec59f78a16ad2edbf464ce75 to your computer and use it in GitHub Desktop.
trendet - is a Python package for trend detection on stock time series data
This file contains 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
import trendet | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
sns.set(style='darkgrid') | |
df = identify_all_trends(equity='bbva', | |
country='spain', | |
from_date='01/01/2018', | |
to_date='01/01/2019', | |
window_size=5) | |
df.reset_index(inplace=True) | |
with plt.style.context('paper'): | |
plt.figure(figsize=(20, 10)) | |
ax = sns.lineplot(x=df['Date'], y=df['Close']) | |
labels = df['Up Trend'].dropna().unique().tolist() | |
for label in labels: | |
sns.lineplot(x=df[df['Up Trend'] == label]['Date'], | |
y=df[df['Up Trend'] == label]['Close'], | |
color='green') | |
ax.axvspan(df[df['Up Trend'] == label]['Date'].iloc[0], | |
df[df['Up Trend'] == label]['Date'].iloc[-1], | |
alpha=0.2, | |
color='green') | |
labels = df['Down Trend'].dropna().unique().tolist() | |
for label in labels: | |
sns.lineplot(x=df[df['Down Trend'] == label]['Date'], | |
y=df[df['Down Trend'] == label]['Close'], | |
color='red') | |
ax.axvspan(df[df['Down Trend'] == label]['Date'].iloc[0], | |
df[df['Down Trend'] == label]['Date'].iloc[-1], | |
alpha=0.2, | |
color='red') | |
plt.show() |
This file contains 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
import trendet | |
import investpy | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
sns.set(style='darkgrid') | |
test = investpy.get_historical_data(equity='repsol', | |
country='spain', | |
from_date='01/01/2018', | |
to_date='01/01/2019') | |
res = identify_df_trends(df=test, column='Close') | |
res.reset_index(inplace=True) | |
with plt.style.context('paper'): | |
plt.figure(figsize=(20, 10)) | |
ax = sns.lineplot(x=res['Date'], y=res['Close']) | |
labels = res['Up Trend'].dropna().unique().tolist() | |
for label in labels: | |
sns.lineplot(x=res[res['Up Trend'] == label]['Date'], | |
y=res[res['Up Trend'] == label]['Close'], | |
color='green') | |
ax.axvspan(res[res['Up Trend'] == label]['Date'].iloc[0], | |
res[res['Up Trend'] == label]['Date'].iloc[-1], | |
alpha=0.2, | |
color='green') | |
labels = res['Down Trend'].dropna().unique().tolist() | |
for label in labels: | |
sns.lineplot(x=res[res['Down Trend'] == label]['Date'], | |
y=res[res['Down Trend'] == label]['Close'], | |
color='red') | |
ax.axvspan(res[res['Down Trend'] == label]['Date'].iloc[0], | |
res[res['Down Trend'] == label]['Date'].iloc[-1], | |
alpha=0.2, | |
color='red') | |
plt.show() |
This file contains 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
import trendet | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
sns.set(style='darkgrid') | |
df = trendet.identify_trends(equity='bbva', | |
country='spain', | |
from_date='01/01/2018', | |
to_date='01/01/2019', | |
window_size=5, | |
trend_limit=3, | |
labels=['A', 'B', 'C']) | |
df.reset_index(inplace=True) | |
with plt.style.context('paper'): | |
plt.figure(figsize=(20, 10)) | |
ax = sns.lineplot(x=df['Date'], y=df['Close']) | |
values = list() | |
value = { | |
'trend': 'Up Trend', | |
'color': 'green', | |
} | |
values.append(value) | |
value = { | |
'trend': 'Down Trend', | |
'color': 'red', | |
} | |
values.append(value) | |
for label in ['A', 'B', 'C']: | |
for value in values: | |
sns.lineplot(x=df[df[value['trend']] == label]['Date'], | |
y=df[df[value['trend']] == label]['Close'], | |
color=value['color']) | |
ax.axvspan(df[df[value['trend']] == label]['Date'].iloc[0], | |
df[df[value['trend']] == label]['Date'].iloc[-1], | |
alpha=0.1, | |
color=value['color']) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The generated plot by the
identify_trends
script via trendet is presented below:The resulting plot of
identify_all_trends
script via trendet looks like:And the resulting plot of
identify_df_trends
script via trendet looks like:Please check trendet and STAR it if you found it useful!
NOTE
This is just the Alpha version, which just covers the basic needs and there is still a lot of work to do.