import requests import pandas as pd import plotly.graph_objects as go from datetime import datetime, timedelta import numpy as np # Your API key api_key = "" # Stock symbols and outstanding shares stocks = ["CATO", "CHS", "AEO", "GPS", "DBI", "HBI", "LE", "TJX", "UAA", "VNCE", "LEVI", "COLM", "KTB", "ROST", "TPR", "RL", "PVH", "RVLV", "ANF", "TLYS", "DLTH", "LAKE"] shares_outstanding = [20420000, 125050000, 197340000, 367830000, 65360000, 349530000, 32460000, 1150000000, 29880000, 443590000, 12340000, 396380000, 62080000, 55520000, 342050000, 236080000, 65980000, 62710000, 73400000, 50050000, 29880000, 34480000, 7370000] # Initialize an empty DataFrame to store the data df = pd.DataFrame() # Get the stock quotes for the past 5 years end_date = datetime.now() start_date = end_date - timedelta(days=5*365) for i, stock in enumerate(stocks): url = f"https://api.polygon.io/v2/aggs/ticker/{stock}/range/1/day/{start_date.strftime('%Y-%m-%d')}/{end_date.strftime('%Y-%m-%d')}?apiKey={api_key}" response = requests.get(url) data = response.json() if 'results' in data: stock_df = pd.DataFrame(data['results']) stock_df['time'] = pd.to_datetime(stock_df['t'], unit='ms') stock_df.set_index('time', inplace=True) stock_df[f'{stock}_weighted'] = stock_df['c'] * shares_outstanding[i] # 'c' corresponds to closing price if df.empty: df = stock_df[[f'{stock}_weighted']] else: df = df.join(stock_df[[f'{stock}_weighted']]) # Sum up the weighted closing prices to compute the ARFI index df['ARFI'] = df.sum(axis=1) # Plot the ARFI index fig = go.Figure(data=go.Scatter(x=df.index, y=df['ARFI'])) fig.update_layout(title='American Retail Fashion Index (ARFI) - 5 Year History', xaxis_title='Date', yaxis_title='Index Value') fig.show() # Compute the year-end values of the ARFI index df_year_end = df['ARFI'].resample('Y').last() # Compute the year-over-year change df_year_end_change = df_year_end.pct_change() * 100 # Plot the year-over-year change fig_change = go.Figure(data=go.Scatter(x=df_year_end_change.index, y=df_year_end_change)) fig_change.update_layout(title='American Retail Fashion Index (ARFI) - Year-over-Year Change', xaxis_title='Date', yaxis_title='Change (%)') fig_change.show()