-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
186 lines (118 loc) · 3.49 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pandas as pd
import plotly.express as px
import yfinance as yf
import numpy as np
from tabulate import tabulate
import statistics
import math
import os, json, hashlib
expense_ratio = 1/100
daily_expense_ratio = expense_ratio/365 # approximately
def get_data(ticker, period):
args = {
'tickers': ticker,
'interval': '1d',
'progress': False
}
if isinstance(period, str):
args['period'] = period
else:
args['start'] = period[0]
args['end'] = period[1]
args_json = json.dumps(args)
hash = hashlib.md5(args_json.encode('utf-8')).hexdigest()
filename = 'data/' + hash + '.csv'
if not os.path.exists(filename):
print('Downloading data...')
df = yf.download(**args)
# Save to CSV
df.to_csv(filename)
df['Date'] = df.index
else:
df = pd.read_csv(filename)
# Drop unnecessary columns
df = df.drop(columns=['Open', 'High', 'Low', 'Adj Close', 'Volume'])
return df
def calculate_leverage(data, leverage):
leverage_data = []
last_value = None
for index, value in enumerate(data):
leverage_value = value
if last_value:
appreciation = (value / last_value) - 1
leverarage_appreciation = appreciation * leverage
last_leverage_value = leverage_data[index-1]
leverage_value = last_leverage_value * (1 + leverarage_appreciation)
# Discount ER
leverage_value -= (leverage_value * daily_expense_ratio)
if leverage_value < 0:
leverage_value = 0
leverage_data.append(leverage_value)
last_value = value
return leverage_data
def max_drawdown(values):
mdd = 0
peak = values[0]
for x in values:
if x > peak:
peak = x
dd = (peak - x) / peak
if dd > mdd:
mdd = dd
return round(mdd * 100, 3)
def standart_deviation(values):
tmp = []
for index, value in enumerate(values):
if index == 0:
continue
tmp.append((value/values[index-1]) - 1)
stdev = statistics.stdev(tmp) * 16
return round(stdev * 100, 3)
def cagr(values):
l = len(values)
years = math.floor(l / 365)
appreciation = values[l-1] / values[0]
cagr = pow(appreciation, 1/years) - 1
return round(cagr * 100, 3)
def run(ticker, leverage_levels, period = 'max', plot = False):
data = get_data(ticker, period)
close_values = data['Close']
last_close = close_values.iloc[-1]
min_date = data['Date'].iloc[0]
max_date = data['Date'].iloc[-1]
table_data = [
['#', 'No Leveraged'],
['End Value (US$)', last_close],
['Appreciation (%)', '-'],
['Stdev (%)', standart_deviation(close_values)],
['Max. Drawdown (%)', max_drawdown(close_values)],
['CAGR (%)', cagr(close_values)],
]
plot_y = ['Close']
for level in leverage_levels:
c = 'Leverage_' + str(level) + 'x'
leveraged_data = calculate_leverage(data['Close'], level)
leveraged_last_close = round(leveraged_data[-1], 3)
leveraged_appreciation = round(((leveraged_last_close / last_close) - 1) * 100, 3)
stdev = standart_deviation(leveraged_data)
table_data[0].append(c)
table_data[1].append(leveraged_last_close)
table_data[2].append(leveraged_appreciation)
table_data[3].append(stdev)
table_data[4].append(max_drawdown(leveraged_data))
table_data[5].append(cagr(leveraged_data))
data[c] = leveraged_data
plot_y.append(c)
print(ticker, ':', min_date, 'to', max_date)
print(tabulate(table_data))
if plot:
fig = px.line(data, x = 'Date', y = plot_y, log_y = True)
fig.show()
fig.write_html('index.html')
run(
ticker = '^GSPC',
leverage_levels=[1.5, 2, 3],
#period = ['1980-01-01', '2022-12-31'],
period = 'max',
plot = True
)