-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from PavanTeja2005/Stocks
added stocks capability
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |