Skip to content

Instantly share code, notes, and snippets.

@scubamut
Last active October 17, 2024 17:39
Show Gist options
  • Save scubamut/0db58aafc737ec8bed5c81855bb0f24b to your computer and use it in GitHub Desktop.
Save scubamut/0db58aafc737ec8bed5c81855bb0f24b to your computer and use it in GitHub Desktop.
import pandas as pd
import yfinance as yf
etf = 'SPY'
# etf = 'QQQ
trend = "downtrend"
# trend = "uptrend"
window = 30
year = 2024
start_date = '2019-01-01'
end_date = '2024-10-01'
data = yf.download(etf, start=start_date, end=end_date)
filtered_data = data['Adj Close'].to_frame('close_price')
filtered_data.index.names = ['period']
# print(filtered_data)
trends_detected = pytrendseries.detecttrend(filtered_data, trend=trend, window=window)
pytrendseries.vizplot.plot_trend(filtered_data, trends_detected, trend, year)
# for a specific year
yr = 2019
df = trends_detected
trends = pd.DataFrame([df.iloc[i] for i in range(len(df)) if df.iloc[i]['from'].year==yr ])
trends
trends_detected = pytrendseries.detecttrend(filtered_data, trend=trend, window=window)
pytrendseries.vizplot.plot_trend(filtered_data, trends_detected, trend, year)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment