Skip to content

Commit

Permalink
added stocks capability
Browse files Browse the repository at this point in the history
  • Loading branch information
PavanTeja2005 committed Oct 9, 2024
1 parent 9d3244a commit 56e043b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions stocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import yfinance as yf
from datetime import datetime, timedelta

def get_stock_data(symbols, start_date=None, end_date=None):
"""
Fetch important stock data for given symbols.
Parameters:
- symbols: List of stock symbols (e.g., ['AAPL', 'MSFT'])
- start_date: Start date for data retrieval in 'YYYY-MM-DD' format (default: 30 days ago)
- end_date: End date for data retrieval in 'YYYY-MM-DD' format (default: today)
Returns:
- A dictionary containing stock data for each symbol
"""
# Set default dates if not provided
if start_date is None:
start_date = (datetime.now() - timedelta(days=30)).strftime('%Y-%m-%d')
if end_date is None:
end_date = datetime.now().strftime('%Y-%m-%d')

stock_data = {}

for symbol in symbols:
stock = yf.Ticker(symbol)
data = stock.history(start=start_date, end=end_date)

if not data.empty:
stock_data[symbol] = {
'Closing Price': data['Close'].iloc[-1],
'Volume': data['Volume'].iloc[-1],
'Market Cap': stock.info.get('marketCap', 'N/A')
}
else:
stock_data[symbol] = 'No data available for this period'

return stock_data



if __name__ == "__main__":
stocks = ["AAPL", "GOOGL"]
print(get_stock_data(stocks))

0 comments on commit 56e043b

Please sign in to comment.