repl.it
Python

No description

fork
loading
Files
  • main.py
  • SPY.csv
main.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
import csv
from datetime import date

firstVal=0 #will be changed later
lastVal=0 #will be changed later
leveraged3ETF=1 #will be updated in loop
directETF=1 #will be updated in loop
row_count=0 #will be changed later
firstDate="" #will be changed later
lastDate="" #will be changed later
starti=2 #2 by default (row 1 is a header)
with open('SPY.csv', newline='') as csvfile:
  row_count = sum(1 for row in csvfile)
with open('SPY.csv', newline='') as csvfile:
  spamreader = csv.reader(csvfile, delimiter=' ',quotechar='|')
  i=1
  previousOpen=0 #will be changed later
  for row in spamreader:
    if(i==starti):
      firstVal=float(str(row[0]).split(',')[1])
      firstDate=str(row[0]).split(',')[0].split("-")
      previousOpen=firstVal
    if(i==row_count):
      lastVal=float(str(row[0]).split(',')[1])
      lastDate=str(row[0]).split(',')[0].split("-")
    if(i>starti):
      rowList=str(row[0]).split(',')
      openPrice=float(rowList[1])
      percentageGain=(openPrice-previousOpen)/previousOpen
      directETF*=(percentageGain+1)
      leveraged3ETFpercentageGain=percentageGain*3
      if(leveraged3ETFpercentageGain<=-1):
        leveraged3ETF=0
      else:
        leveraged3ETF*=(leveraged3ETFpercentageGain+1)
      previousOpen=openPrice
    i+=1


d0 = date(int(firstDate[0]), int(firstDate[1]), int(firstDate[2]))
d1 = date(int(lastDate[0]), int(lastDate[1]), int(lastDate[2]))
delta = d1 - d0
totalDays=delta.days



def findCAGRvariableDays(growth,days):
  return (growth+1)**(365/days)-1
def findCAGR(growth):
  return findCAGRvariableDays(growth,totalDays)


spyGrowth=(lastVal-firstVal)/firstVal
print("\nSPY CAGR:\n"+str(findCAGR(spyGrowth)*100)+"%\n")
directEtfGrowth=directETF-1
print("Direct ETF CAGR (for testing):\n"+str(findCAGR(directEtfGrowth)*100)+"%\n")
leveraged3EtfGrowth=leveraged3ETF-1
print("3x Daily Leveraged ETF (assuming no expenses):\n"+str(findCAGR(leveraged3EtfGrowth)*100)+"%")
print("\n\n")
print(findCAGRvariableDays(1.5601,3263))