Skip to content
Testing N-HiTS on Sector ETFs
  • AI Chat
  • Code
  • Report
  • Install Libraries

    !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 =,'%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
        # 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 as ple
    import matplotlib.pyplot as plt
    import darts

    quick check of etf_data


    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'] = etf['Adj Close'].pct_change() * 100
    etf_series = darts.TimeSeries.from_dataframe(etf, value_cols = 'Log Price', fill_missing_dates=True, freq=None)

    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