-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_stock_v2.py
32 lines (28 loc) · 1.04 KB
/
update_stock_v2.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
import sqlite3
import yfinance as yf
import time
# SQLite 데이터베이스 연결
conn = sqlite3.connect('stocks.db')
#table이 없으면 테이블 생성해서 주가 업데이트
if not conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=stock_prices").fetchone():
conn.execute("""CREATE TABLE "stock_prices" (
"symbol" TEXT,
"date" DATE,
"open" REAL,
"high" REAL,
"low" REAL,
"close" REAL,
"adj_close" REAL,
"volume" INTEGER)""")
conn.commit()
else:
for name in ['AAPL','MSFT']:
print (name)
rec = yf.Ticker(name)
rec_h = rec.history(start='2023-01-01',end='2023-12-31')
for idx, row in rec_h.iterrows():
cursor = conn.cursor()
cursor.execute("INSERT INTO stock_prices (symbol, date, open, high, low, close, adj_close, volume) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (name, idx.strftime('%Y-%m-%d'), row['Open'], row['High'], row['Low'], row['Close'], row['Adj Close'], row['Volume']))
conn.commit()
# SQLite 연결 종료
conn.close()