Skip to content

Stock analysis #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions MachineLearning Projects/Stock-Analysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
StockAnalysis

This program has ability to analyse stock and compare its price over today,yesterday,lastmonth,lastyear and plot the trend.
Program allows to input stock tickers explcitly or get 'n' stock based on S&P index.

Sample Output

For eg: Here it can analyse how AAPL is performed over yesterday,lastmonth,lastyear and plot



![image](https://user-images.githubusercontent.com/16798480/225775468-f9332a85-8181-47eb-a48f-8d36f85e9d5a.png)
64 changes: 64 additions & 0 deletions MachineLearning Projects/Stock-Analysis/StockAnalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import yfinance as yf
import pandas as pd
import datetime
from datetime import date
from pandas_datareader import data as pdr
import pandas as pd
import matplotlib.pyplot as plt

stocknum=10
option=1
tickerlist = []
while True:
try:
option = int(input("You have 2 options 1: Do you want to analyse top stocks in S&P index 2: Specific Stocks, 1/2 ?"))

if option==1:
stocknum=int(input("How many stocks you want to analyse? give a number"))
toptickers = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
tickerlist= toptickers.head(stocknum).Symbol.to_list()
print(tickerlist)
elif option==2:
inputstr = input("Enter comma separated ticker symbols of interested stocks ")
print("Input string: ", inputstr)

# conver to the list
tickerlist = inputstr.upper().split (",")
print("list: ", list)
else:
raise ValueError
break
except ValueError:
print("Please input valid integer only...")
continue

data = pd.DataFrame(columns=tickerlist)

for ticker in tickerlist:
y = yf.Ticker(ticker)
if(y==0):
print("No data found for ",ticker)
break
try:
today=date.today()
yesterday=today + datetime.timedelta(days=-1)
lastmonth=today + datetime.timedelta(days=-30)
lastyear=today + datetime.timedelta(days=-365)
df2=yf.download(tickerlist,today,today+datetime.timedelta(days=1), auto_adjust=True)['Close']
df3=yf.download(tickerlist,yesterday,yesterday+datetime.timedelta(days=1), auto_adjust=True)['Close']
df4=yf.download(tickerlist,lastmonth,lastmonth+datetime.timedelta(days=1), auto_adjust=True)['Close']
df5=yf.download(tickerlist,lastyear,lastyear+datetime.timedelta(days=1), auto_adjust=True)['Close']
data=data.append(df2)
data=data.append(df3)
data=data.append(df4)
data=data.append(df5)
data.plot()

print(data.head())
except:
print("Error occured while processing. Please try again.")