-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicFinanceGraph+Averages.py
More file actions
79 lines (61 loc) · 2.22 KB
/
BasicFinanceGraph+Averages.py
File metadata and controls
79 lines (61 loc) · 2.22 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
67
68
69
70
71
72
73
74
75
76
77
78
79
from calendar import month
from pickle import TRUE
import pandas as pd
import numpy as np
import datetime
import matplotlib.pyplot as plt
from pandas_datareader import data as pdr
import yfinance as yf
yr = input("From what year do you want to start the analysis (Ex. 2022): ")
mon = input("From what month do you want to start the analysis (Ex. 8 or 12): ")
day = input("From what day do you want to start the analysis (Ex. 8 or 12): ")
yr = int(yr)
mon = int(mon)
day = int(day)
yf.pdr_override()
stockA = input("Please enter the ticker for the stock you wish to graph: ")
st = {}
st = pdr.get_data_yahoo(stockA,
start = datetime.datetime(yr,mon,day),
end = datetime.date.today().strftime('%Y-%m-%d'))
print("Here is a graph of the stock price:")
st['Close'].plot(grid=True)
plt.show()
daily_close = st[['Adj Close']]
daily_pct_c = daily_close.pct_change()
daily_pct_c.fillna(0,inplace=True)
print("Here is a list of the daily and total returns for your stock:")
print(daily_pct_c)
daily_log_returns = np.log(daily_close.pct_change()+1)
print(daily_log_returns)
print("Here is te data over the monthly and quarterly change in the stock:")
monthly = st.resample('BM').apply(lambda x: x[-1])
print(monthly.pct_change())
quarter = st.resample('4M').mean()
print(quarter.pct_change())
print("")
print("Here is the adjusted daily returns of the stock p:")
daily_pct_c = daily_close / daily_close.shift(1)-1
print(daily_pct_c)
print("Here is a histogram of the average daily return:")
daily_pct_c.hist(bins=50)
plt.show()
print(daily_pct_c.describe())
print()
print("Here is a graph of the cumulative return as well as some data about it: ")
cum_daily_return = (1+daily_pct_c).cumprod()
print(cum_daily_return)
cum_daily_return.plot(figsize=(15,8))
plt.show()
cum_monthly_return = cum_daily_return.resample("M").mean()
print(cum_monthly_return)
adj_close_px = st['Adj Close']
moving_avg = adj_close_px.rolling(window=40).mean()
moving_avg[-10:]
print("Here is a graph of the stock and the calculated moving averages:")
st['42'] = adj_close_px.rolling(window=40).mean()
st['252'] = adj_close_px.rolling(window=252).mean()
st[['Adj Close','42','252']].plot()
plt.show()
short_window = 40
long_window = 100