Skip to content

Commit 5f99866

Browse files
authored
Merge pull request #274 from shreeup/StockAnalysis
Stock analysis
2 parents f41a498 + b11d632 commit 5f99866

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
StockAnalysis
2+
3+
This program has ability to analyse stock and compare its price over today,yesterday,lastmonth,lastyear and plot the trend.
4+
Program allows to input stock tickers explcitly or get 'n' stock based on S&P index.
5+
6+
Sample Output
7+
8+
For eg: Here it can analyse how AAPL is performed over yesterday,lastmonth,lastyear and plot
9+
10+
11+
12+
![image](https://user-images.githubusercontent.com/16798480/225775468-f9332a85-8181-47eb-a48f-8d36f85e9d5a.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import yfinance as yf
2+
import pandas as pd
3+
import datetime
4+
from datetime import date
5+
from pandas_datareader import data as pdr
6+
import pandas as pd
7+
import matplotlib.pyplot as plt
8+
9+
stocknum=10
10+
option=1
11+
tickerlist = []
12+
while True:
13+
try:
14+
option = int(input("You have 2 options 1: Do you want to analyse top stocks in S&P index 2: Specific Stocks, 1/2 ?"))
15+
16+
if option==1:
17+
stocknum=int(input("How many stocks you want to analyse? give a number"))
18+
toptickers = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
19+
tickerlist= toptickers.head(stocknum).Symbol.to_list()
20+
print(tickerlist)
21+
elif option==2:
22+
inputstr = input("Enter comma separated ticker symbols of interested stocks ")
23+
print("Input string: ", inputstr)
24+
25+
# conver to the list
26+
tickerlist = inputstr.upper().split (",")
27+
print("list: ", list)
28+
else:
29+
raise ValueError
30+
break
31+
except ValueError:
32+
print("Please input valid integer only...")
33+
continue
34+
35+
data = pd.DataFrame(columns=tickerlist)
36+
37+
for ticker in tickerlist:
38+
y = yf.Ticker(ticker)
39+
if(y==0):
40+
print("No data found for ",ticker)
41+
break
42+
try:
43+
today=date.today()
44+
yesterday=today + datetime.timedelta(days=-1)
45+
lastmonth=today + datetime.timedelta(days=-30)
46+
lastyear=today + datetime.timedelta(days=-365)
47+
df2=yf.download(tickerlist,today,today+datetime.timedelta(days=1), auto_adjust=True)['Close']
48+
df3=yf.download(tickerlist,yesterday,yesterday+datetime.timedelta(days=1), auto_adjust=True)['Close']
49+
df4=yf.download(tickerlist,lastmonth,lastmonth+datetime.timedelta(days=1), auto_adjust=True)['Close']
50+
df5=yf.download(tickerlist,lastyear,lastyear+datetime.timedelta(days=1), auto_adjust=True)['Close']
51+
data=data.append(df2)
52+
data=data.append(df3)
53+
data=data.append(df4)
54+
data=data.append(df5)
55+
data.plot()
56+
57+
print(data.head())
58+
except:
59+
print("Error occured while processing. Please try again.")
60+
61+
62+
63+
64+

0 commit comments

Comments
 (0)