Skip to content
Testing Fast Fourier Transfrom on Sector ETFs
  • AI Chat
  • Code
  • Report
  • Install Libraries

    More on Darts library forecasting models can be found: https://unit8co.github.io/darts/generated_api/darts.models.forecasting.html

    !pip install yfinance
    !pip install darts
    Hidden output

    Import libraries

    import yfinance as yf
    import numpy as np
    import pandas as pd
    from datetime import datetime

    Create sector dataframe

    #Vanguard Sector ETF List for Trend Rotation
    sectors = ['SPY', 'VGLT', 'VGIT', 'VGSH', 'VCSH', 'VCIT', 'VCLT', 'BND', 'VTI', 'VOO',	'VV', 'VIG', 'VFH',	'VGT', 'VCR', 'VHT', 'VIS', 'VAW', 'VDC', 'VPU', 'VOX', 'VDE', 'VNQ']
    
    # Function to fetch and process data for all ETFs
    def fetch_process_data(tickers):
        # Use yfinance to download data for all tickers at once
        data = yf.download(tickers, end=datetime.now().strftime('%Y-%m-%d'), period='max', 
                           progress=False, interval='1wk', auto_adjust=False, group_by='ticker')
        
        # Initialize an empty list to store processed data
        processed_data = []
    
        # Process data for each ticker
        for ticker in tickers:
            # Extract individual ticker data
            df = data[ticker].dropna()  # Drop NaN value rows that might appear during download
            df['Ticker'] = ticker  # Add ticker column
            # Append processed data to the list
            processed_data.append(df)
    
        # Concatenate all dataframes into a single dataframe
        final_data = pd.concat(processed_data)
    
        return final_data
    
    # Function call
    etf_data = fetch_process_data(sectors)

    import plotly for quick EDA checks

    import pyplot for more analysis

    import darts for timeseries objects

    import plotly.express as ple
    import matplotlib.pyplot as plt
    import darts

    quick check of etf_data

    display(etf_data.tail(100))

    Choice of ETF to test along with log & percent change normalizations

    sector_choice = 'SPY'
    etf = etf_data[etf_data['Ticker']==sector_choice][['Adj Close']]
    etf.index = pd.to_datetime(etf.index)
    etf['Log_Price'] = np.log(etf['Adj Close'])
    etf['Percent_Diff_Wk'] = etf['Adj Close'].pct_change() * 100
    etf['Percent_Diff_Mnth'] = etf['Adj Close'].pct_change(periods=4) * 100
    etf['Percent_Diff_Qtr'] = etf['Adj Close'].pct_change(periods=16) * 100
    etf['Percent_Diff_Yr'] = etf['Adj Close'].pct_change(periods=52) * 100
    print(len(etf))
    display(etf.tail(100))
    etf_series = darts.TimeSeries.from_dataframe(etf, value_cols = 'Percent_Diff_Qtr', fill_missing_dates=True, freq=None)
    etf_series.plot()

    Simple seasonality check

    from darts.utils.statistics import check_seasonality
    
    is_monthly_seasonal, monthly_period = darts.utils.statistics.check_seasonality(etf_series, m=4, max_lag=400, alpha=0.05)
    is_quarterly_seasonal, quarter_period = darts.utils.statistics.check_seasonality(etf_series, m=12, max_lag=400, alpha=0.05)
    is_annually_seasonal, annual_period = darts.utils.statistics.check_seasonality(etf_series, m=52, max_lag=400, alpha=0.05)
    
    print(f'Montly seasonality: {is_monthly_seasonal} - period = {monthly_period}')
    print(f'Quarterly seasonality: {is_quarterly_seasonal} - period = {quarter_period}')
    print(f'Annually seasonality: {is_annually_seasonal} - period = {annual_period}')

    Visualization of Train and Test datasets