forked from alonzorworks/streamlit_ask_data_app
-
Notifications
You must be signed in to change notification settings - Fork 2
/
eod-delta-data.py
37 lines (31 loc) · 1.39 KB
/
eod-delta-data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import pandas as pd
import requests
from datetime import datetime, timedelta
# Tiingo API token
TIINGO_API_TOKEN = '572ec329dc02c59f0d3ac6455cfd4b349570b540'
# Load tickers
tickers_df = pd.read_csv('eod-delta-data.csv')
tickers = tickers_df['ticker'].unique() # Assuming there's a 'ticker' column
# Load your existing data
df = pd.read_csv('your_file.csv', parse_dates=['date']) # Adjust the date column name if needed
# Function to fetch data from Tiingo
def fetch_data(ticker, start_date):
url = f"https://api.tiingo.com/tiingo/daily/{ticker}/prices?startDate={start_date}&token={TIINGO_API_TOKEN}"
headers = {'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return pd.DataFrame(response.json())
else:
return pd.DataFrame() # Empty DataFrame if the request fails
# Update data for each ticker
for ticker in tickers:
ticker_data = df[df['ticker'] == ticker] # Filter data for the current ticker
last_date = ticker_data['date'].max()
today = datetime.now().date()
if last_date.date() < today:
start_date = (last_date + timedelta(days=1)).strftime('%Y-%m-%d')
new_data = fetch_data(ticker, start_date)
if not new_data.empty:
df = pd.concat([df, new_data], ignore_index=True)
# Save the updated DataFrame to CSV
df.to_csv('your_updated_file.csv', index=False)