forked from larymak/Python-project-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockAnalysis.py
More file actions
66 lines (56 loc) · 2.24 KB
/
Copy pathStockAnalysis.py
File metadata and controls
66 lines (56 loc) · 2.24 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
59
60
61
62
63
64
65
66
import yfinance as yf
import pandas as pd
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
# Function to get a list of tickers for analysis
def get_ticker_list():
while True:
try:
option = int(input("You have 2 options:\n1. Analyze top stocks in S&P index\n2. Specific Stocks\nEnter option (1/2): "))
if option == 1:
stocknum = int(input("How many stocks do you want to analyze? Enter a number: "))
toptickers = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
return toptickers.head(stocknum).Symbol.to_list()
elif option == 2:
inputstr = input("Enter comma-separated ticker symbols of interested stocks: ")
return inputstr.upper().split(",")
else:
print("Please enter a valid option (1/2).")
except ValueError:
print("Please input a valid integer only.")
# Function to get the time period for analysis
def get_time_period():
try:
start_date = input("Enter the start date (YYYY-MM-DD): ")
end_date = input("Enter the end date (YYYY-MM-DD): ")
return start_date, end_date
except ValueError:
print("Invalid date format. Please use the format YYYY-MM-DD.")
# Main function
def main():
tickerlist = get_ticker_list()
start_date, end_date = get_time_period() # Get time period for analysis
data = pd.DataFrame()
# Loop through each ticker to fetch data
for ticker in tickerlist:
try:
y = yf.Ticker(ticker)
df = y.history(period="1d", start=start_date, end=end_date)
if not df.empty:
data[ticker] = df["Close"]
else:
print("No data found for", ticker)
except Exception as e:
print("An error occurred while fetching data for", ticker, ":", str(e))
if not data.empty:
# Plotting the data
data.plot(title="Stock Price Analysis")
plt.xlabel("Date")
plt.ylabel("Price")
plt.show()
print(data.head())
else:
print("No data available for analysis.")
# Entry point of the program
if __name__ == "__main__":
main()