-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_fetch.py
More file actions
58 lines (44 loc) · 1.45 KB
/
data_fetch.py
File metadata and controls
58 lines (44 loc) · 1.45 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import yfinance as yf
import psycopg2
from psycopg2.extras import execute_batch
import logging
# Logging configuration
logging.basicConfig(level=logging.INFO)
# Database connection details
db_config = {
'dbname': 'stock_data',
'user': 'postgres',
'password': '',
'host': 'localhost'
}
def fetch_stock_data(ticker, start, end):
logging.info(f'Fetching data for {ticker}')
data = yf.download(ticker, start=start, end=end)
return data
def store_stock_data(ticker, data):
conn = psycopg2.connect(**db_config)
cursor = conn.cursor()
# Prepare data for insertion
records = [(ticker, index.date(), row['Open'], row['High'], row['Low'], row['Close'], row['Volume'])
for index, row in data.iterrows()]
# Insert data into the table
insert_query = """
INSERT INTO stock_prices (ticker, date, open, high, low, close, volume)
VALUES (%s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (ticker, date) DO NOTHING;
"""
execute_batch(cursor, insert_query, records)
conn.commit()
cursor.close()
conn.close()
def fetch_and_store_stock_data(ticker, start, end):
data = fetch_stock_data(ticker, start, end)
if not data.empty:
store_stock_data(ticker, data)
return data
if __name__ == '__main__':
tickers = ['AAPL', 'MSFT', 'GOOGL']
start_date = '2020-01-01'
end_date = '2024-07-25'
for ticker in tickers:
fetch_and_store_stock_data(ticker, start_date, end_date)