Last active
December 15, 2023 14:09
-
-
Save Charlesmendez/f38942b6d5f13bd8e6e374590b117cac to your computer and use it in GitHub Desktop.
Public IEX code
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 pandas as pd | |
import requests | |
import time | |
from datetime import datetime | |
from ide_utils.input_tables import load_dask | |
from ide_utils.output_table import send_to_output_table | |
API_TOKEN = "YOUR TOKEN" | |
base_url = "https://cloud.iexapis.com/v1" | |
endpoint = "/stock/{}/chart" | |
params = {"token": API_TOKEN, "range": 'previous'} | |
ticker_symbols = ['KVYO', 'CART', 'A', 'AAL', 'AAP', 'AAPL', 'ABBV', 'ABNB', 'ABT', 'ACGL', 'ACN', 'ADBE', 'ADI', 'ADM', 'ADP', 'ADSK', 'AEE', 'AEP', 'AES', 'AFL', 'AIG', 'AIZ', 'AJG', 'AKAM', 'ALB', 'ALGN', 'ALK', 'ALL', 'ALLE', 'AMAT', 'AMCR', 'AMD', 'AME', 'AMGN', 'AMP', 'AMT', 'AMZN', 'ANET', 'ANSS', 'AON', 'AOS', 'APA', 'APD', 'APH', 'APTV', 'ARE', 'ASML', 'ATO', 'ATVI', 'AVB', 'AVGO', 'AVY', 'AWK', 'AXON', 'AXP', 'AZO', 'BA', 'BAC', 'BALL', 'BAX', 'BBWI', 'BBY', 'BDX', 'BEN', 'BF.B', 'BG', 'BIIB', 'BIO', 'BK', 'BKNG', 'BKR', 'BLK', 'BMY', 'BR', 'BRK.B', 'BRO', 'BROS', 'BSX', 'BWA', 'BXP', 'C', 'CAG', 'CAH', 'CARR', 'CAT', 'CB', 'CBOE', 'CBRE', 'CCI', 'CCL', 'CDAY', 'CDNS', 'CDW', 'CE', 'CEG', 'CF', 'CFG', 'CHD', 'CHRW', 'CHTR', 'CI', 'CINF', 'CL', 'CLX', 'CMA', 'CMCSA', 'CME', 'CMG', 'CMI', 'CMS', 'CNC', 'CNP', 'COF', 'COO', 'COP', 'COST', 'CPB', 'CPRT', 'CPT', 'CRL', 'CRM', 'CSCO', 'CSGP', 'CSX', 'CTAS', 'CTLT', 'CTRA', 'CTSH', 'CTVA', 'CVS', 'CVX', 'CZR', 'D', 'DAL', 'DD', 'DE', 'DFS', 'DG', 'DGX', 'DHI', 'DHR', 'DIS', 'DLR', 'DLTR', 'DOV', 'DOW', 'DPZ', 'DRI', 'DTE', 'DUK', 'DVA', 'DVN', 'DXC', 'DXCM', 'EA', 'EBAY', 'ECL', 'ED', 'EFX', 'EG', 'EIX', 'EL', 'ELV', 'EMN', 'EMR', 'ENPH', 'EOG', 'EPAM', 'EQIX', 'EQR', 'EQT', 'ES', 'ESS', 'ETN', 'ETR', 'ETSY', 'EVRG', 'EW', 'EXC', 'EXPD', 'EXPE', 'EXR', 'F', 'FANG', 'FAST', 'FCX', 'FDS', 'FDX', 'FE', 'FFIV', 'FI', 'FICO', 'FIS', 'FITB', 'FLT', 'FMC', 'FOX', 'FOXA', 'FRT', 'FSLR', 'FTNT', 'FTV', 'GD', 'GE', 'GEHC', 'GEN', 'GILD', 'GIS', 'GL', 'GLW', 'GM', 'GNRC', 'GOOG', 'GOOGL', 'GPC', 'GPN', 'GRMN', 'GS', 'GWW', 'HAL', 'HAS', 'HBAN', 'HCA', 'HD', 'HES', 'HIG', 'HII', 'HLT', 'HOLX', 'HON', 'HPE', 'HPQ', 'HRL', 'HSIC', 'HST', 'HSY', 'HUM', 'HWM', 'IBM', 'ICE', 'IDXX', 'IEX', 'IFF', 'ILMN', 'INCY', 'INTC', 'INTU', 'INVH', 'IP', 'IPG', 'IQV', 'IR', 'IRM', 'ISRG', 'IT', 'ITW', 'IVZ', 'J', 'JBHT', 'JCI', 'JKHY', 'JNJ', 'JNPR', 'JPM', 'K', 'KDP', 'KEY', 'KEYS', 'KHC', 'KIM', 'KLAC', 'KMB', 'KMI', 'KMX', 'KO', 'KR', 'L', 'LCID', 'LDOS', 'LEN', 'LH', 'LHX', 'LIN', 'LKQ', 'LLY', 'LMT', 'LNC', 'LNT', 'LOW', 'LRCX', 'LULU', 'LUV', 'LVS', 'LW', 'LYB', 'LYV', 'MA', 'MAA', 'MAR', 'MAS', 'MCD', 'MCHP', 'MCK', 'MCO', 'MDLZ', 'MDT', 'MET', 'META', 'MGM', 'MHK', 'MKC', 'MKTX', 'MLM', 'MMC', 'MMM', 'MNST', 'MO', 'MOH', 'MOS', 'MPC', 'MPWR', 'MRK', 'MRNA', 'MRO', 'MS', 'MSCI', 'MSFT', 'MSI', 'MTB', 'MTCH', 'MTD', 'MU', 'NCLH', 'NDAQ', 'NDSN', 'NEE', 'NEM', 'NFLX', 'NI', 'NKE', 'NOC', 'NOW', 'NRG', 'NSC', 'NTAP', 'NTRS', 'NUE', 'NVDA', 'NVR', 'NWL', 'NWS', 'NWSA', 'NXPI', 'O', 'ODFL', 'OGN', 'OKE', 'OMC', 'ON', 'ORCL', 'ORLY', 'OTIS', 'OXY', 'PANW', 'PARA', 'PAYC', 'PAYX', 'PCAR', 'PCG', 'PEAK', 'PEG', 'PEP', 'PFE', 'PFG', 'PG', 'PGR', 'PH', 'PHM', 'PKG', 'PLD', 'PLTR', 'PM', 'PNC', 'PNR', 'PNW', 'PODD', 'POOL', 'PPG', 'PPL', 'PRU', 'PSA', 'PSX', 'PTC', 'PTON', 'PWR', 'PXD', 'PYPL', 'QCOM', 'QRVO', 'RCL', 'REG', 'REGN', 'RF', 'RHI', 'RIVN', 'RJF', 'RL', 'RMD', 'ROK', 'ROL', 'ROP', 'ROST', 'RSG', 'RTX', 'RVTY', 'SBAC', 'SBUX', 'SCHW', 'SEDG', 'SEE', 'SHW', 'SJM', 'SLB', 'SNA', 'SNPS', 'SO', 'SPG', 'SPGI', 'SRE', 'STE', 'STLD', 'STT', 'STX', 'STZ', 'SWK', 'SWKS', 'SYF', 'SYK', 'SYY', 'T', 'TAP', 'TDG', 'TDY', 'TECH', 'TEL', 'TER', 'TFC', 'TFX', 'TGT', 'TJX', 'TMO', 'TMUS', 'TPR', 'TRGP', 'TRMB', 'TROW', 'TRV', 'TSCO', 'TSLA', 'TSM', 'TSN', 'TT', 'TTWO', 'TWLO', 'TXN', 'TXT', 'TYL', 'UAL', 'UBER', 'UDR', 'UHS', 'ULTA', 'UNH', 'UNP', 'UPS', 'URI', 'USB', 'V', 'VFC', 'VICI', 'VLO', 'VMC', 'VRSK', 'VRSN', 'VRTX', 'VTR', 'VTRS', 'VZ', 'WAB', 'WAT', 'WBA', 'WBD', 'WDAY', 'WDC', 'WEC', 'WELL', 'WFC', 'WHR', 'WM', 'WMB', 'WMT', 'WRB', 'WRK', 'WST', 'WTW', 'WY', 'WYNN', 'XEL', 'XOM', 'XRAY', 'XYL', 'YUM', 'ZBH', 'ZBRA', 'ZION', 'ZM', 'ZTS'] | |
today = datetime.today().weekday() | |
if today != 0 and today != 6: | |
max_retries = 20 | |
upload_retries = 100 | |
for ticker in ticker_symbols: | |
upload_retry_count = 0 | |
while upload_retry_count < upload_retries: | |
response = requests.get(base_url + endpoint.format(ticker), params=params) | |
time.sleep(0.2) | |
if response.status_code != 200: | |
print(f"Download failed with status code {response.status_code}") | |
upload_retry_count += 1 | |
continue | |
data = response.json() | |
df = pd.DataFrame(data) | |
print(f"Downloaded data for {ticker}") | |
column_types = { | |
'close': float, | |
'high': float, | |
'low': float, | |
'open': float, | |
'priceDate': str, | |
'symbol': str, | |
'volume': 'Int64', # 'Int64' (capital "I") supports NaN | |
'id': str, | |
'key': str, | |
'subkey': str, | |
'date': 'datetime64[ns]', | |
'updated': 'Int64', | |
'changeOverTime': float, | |
'marketChangeOverTime': float, | |
'uOpen': float, | |
'uClose': float, | |
'uHigh': float, | |
'uLow': float, | |
'uVolume': 'Int64', | |
'fOpen': float, | |
'key': str, | |
'subkey': str, | |
'fClose': float, | |
'fHigh': float, | |
'fLow': float, | |
'fVolume': 'Int64', | |
'label': str, | |
'change': float, | |
'changePercent': float, | |
'__index_level_0__': 'Int64' | |
} | |
for column, dtype in column_types.items(): | |
if column in df.columns: | |
if dtype in ['Int64']: | |
df[column] = df[column].apply(lambda x: int(x)) | |
else: | |
df[column] = df[column].astype(dtype) | |
if 'date' in df.columns: | |
df['date'] = pd.to_datetime(df['date']) | |
df = df.sort_values(by='date', ascending=False) | |
else: | |
print(f"Data for {ticker} does not contain 'date' column.") | |
df.to_parquet(f'{ticker}_stock_data.parquet') | |
integration_id = "YOUR ID" #You can find this in the Integration settings. It is the URL and sepcifically the numbers in the URL. | |
stream = "iex_data" | |
api_key = "YOUR API KEY" | |
response = requests.post( | |
url=f"https://api.v2.datagran.io/v2/integrations/{integration_id}/push_data/{stream}", | |
headers={"Authorization": api_key}, | |
files={'data-file': open(f'{ticker}_stock_data.parquet', 'rb')} | |
) | |
if response.status_code != 200: | |
print(f"Upload failed with status code {response.status_code}") | |
upload_retry_count += 1 | |
continue | |
task_id = response.json()["bg_task_id"] | |
retry_count = 0 | |
while retry_count < max_retries: | |
response = requests.get( | |
url=f"https://api.v2.datagran.io/v2/integrations/{integration_id}/push_data_status/{task_id}", | |
headers={"Authorization": api_key}, | |
) | |
if response.status_code != 200: | |
print(f"Followup failed with status code {response.status_code}") | |
retry_count += 1 | |
continue | |
result = response.json() | |
if '_error' in result or 'status' not in result: | |
retry_count += 1 | |
continue | |
if result['status'] in ['CANCELLED', 'ERROR', 'SUCCESSFUL']: | |
print(f"Upload for {ticker} was {result['status']}") | |
upload_retry_count = upload_retries # Exit while loop | |
break | |
time.sleep(5) | |
retry_count += 1 | |
if retry_count == max_retries: | |
upload_retry_count += 1 | |
else: | |
print("Today is either Sunday or Monday. The script did not run.") | |
dummy_df = pd.DataFrame({"x": [2, 3, 5, 7, 11], "y": ["Dummy data"] * 5}) | |
send_to_output_table(dummy_df, table="dummy_table") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment