-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
39 lines (32 loc) · 1.52 KB
/
sample.py
File metadata and controls
39 lines (32 loc) · 1.52 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
import yfinance as yf
from utils.db_utils import db
from stock_app.models import Stock
from app import app
import json
def fetch_and_insert_stocks():
with app.app_context():
tickers = ['AAPL', 'GOOGL', 'AMZN', 'MSFT', 'META', 'TSLA', 'NVDA', 'JPM', 'V', 'WMT']
for ticker in tickers:
stock = yf.Ticker(ticker)
data = stock.history(period='1d')
info = stock.info
if not data.empty:
latest_price = data['Close'].iloc[-1]
previous_close = info.get('previousClose', latest_price)
price_change = latest_price - previous_close
# Convert Timestamp objects to string format for JSON serialization
historical_data = stock.history(period='1mo')['Close']
historical_dict = {date.strftime('%Y-%m-%d'): price
for date, price in historical_data.items()}
new_stock = Stock(
ticker=ticker,
name=info.get('longName', ticker),
price=latest_price,
change=price_change,
price_history=json.dumps(historical_dict)
)
db.session.add(new_stock)
db.session.commit()
print(f"Added {ticker} - {info.get('longName')} with price ${latest_price:.2f} (Change: ${price_change:.2f})")
if __name__ == '__main__':
fetch_and_insert_stocks()