-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuy 1 Share Per Week Simulation - Final Code.py
113 lines (73 loc) · 3.48 KB
/
Buy 1 Share Per Week Simulation - Final Code.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#package imports and graph style
import matplotlib.pyplot as plt
import warnings
import pandas as pd
import yfinance as yf
import datetime
plt.style.use('seaborn-whitegrid')
warnings.simplefilter(action='ignore', category=FutureWarning)
# In[4]:
#function to calculate and store results for 1 stock at a time. Output is a Dataframe
def calculateLongTermReturn(stock, startDate, endDate):
#pull data for yfinance (stock = text, Date format = "yyyy-mm-dd")
ticker_df = yf.download(stock, start = startDate, end = endDate)
#add in day of the week column
ticker_df["day of the week"] = ticker_df.index.strftime("%A")
#create empty Data Frame to store results
temp = stock + "DataFrame"
temp = pd.DataFrame(columns = ["Current Total Value", "Current Average Price",
"Number of Shares Purchased", "Current Profit Loss",
"Current Percent Profit Loss"])
#current total value of all stock purchases
currentTotalValue = 0
#current total holdings average price
currentAvgPrice = 0
#number of shares bought
numSharesPurchased = 0
#current profit/loss value
currentProfitLoss = 0
#current percent gain on account value
currentPercentPL = 0
#iterating over rows of the dataframe
for index, row in ticker_df.iterrows():
#print(index, row["day of the week"])
if(row["day of the week"] == "Tuesday"):
currentTotalValue += row["Open"]
numSharesPurchased += 1
currentAvgPrice = currentTotalValue/numSharesPurchased
currentProfitLoss = (row["Close"] - currentAvgPrice)*numSharesPurchased
currentPercentPL = currentProfitLoss/currentTotalValue
#current percent PL is in decimal form, not percent form for graphing purposes
#adding running values to dataframe
newRow = pd.Series(data = {"Current Total Value": currentTotalValue, "Current Average Price": currentAvgPrice,
"Number of Shares Purchased": numSharesPurchased, "Current Profit Loss": currentProfitLoss,
"Current Percent Profit Loss": currentPercentPL}, name = index)
temp = temp.append(newRow, ignore_index = False)
return temp
#results = calculateLongTermReturn("SPY", "2010-01-01", "2020-07-31")
# In[5]:
#creating a dictionary of dataframes that can be accessed by the stock ticker name
#run for multiple stocks. Produces multiple dataframes
allStocks = {}
#if you want to run the code for different stocks, then edit the list of stocks below and the Start and End dates
stocks = ["SPY", "VTI", "AAPL", "MSFT", "AMZN", "WMT", "KO", "MCD"]
officialStartDate = "2010-01-01"
officialEndDate = "2020-08-31"
for i in stocks:
allStocks.update({i: calculateLongTermReturn(i, officialStartDate, officialEndDate)})
# In[7]:
#plotting Profit/Loss Percent % column of all stocks
x = allStocks["SPY"].index
plt.xlabel("Year")
plt.ylabel("Profit Loss %")
plt.title("Buy 1 Share Per Week - Profit/Loss Percentage from " + officialStartDate + " to " + officialEndDate)
for i in stocks:
y = allStocks[i]["Current Percent Profit Loss"]
plt.plot(x, y, label = i, linewidth = 1.0)
#writes each stock Percent Profit Loss to an Excel
allStocks[i]["Current Percent Profit Loss"].to_excel(i+"EXCEL.xlsx")
plt.legend()
plt.savefig("graphResults1.png", dpi = 300)