-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
595 lines (534 loc) · 21 KB
/
main.py
File metadata and controls
595 lines (534 loc) · 21 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import streamlit as st
from shroomdk import ShroomDK
import pandas as pd
import streamlit.components.v1 as components
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
from scipy import optimize
# ! TODOs:
# factor in withdrawal costs (swap fees)
# fix colors of 3d plots
# idk
# set page to white theme
st.set_page_config(
layout="wide",
page_title="simulatooor",
page_icon="🧠",
initial_sidebar_state="collapsed",
)
# st.markdown(df### ['gas_used'].mean())
# st.write(df)
st.header("Strategy Simulator")
st.image("limitless.gif")
with st.expander("wtf?", expanded=False):
st.markdown(
"""
this app simulates defi strategies similar to some Yearn strategies or Notional Leveraged Vaults
***
in general, those are strats where you:
- deposit a token (e.g. ETH) as collateral
- then borrow the same token according to the leverage (e.g. borrow 2x the deposited amount if leverage = 2)
- then convert part of your assets into a secondary token (e.g. wstETH)
- the amount converted is determined by the liquidity pool ratio (e.g. 50% of the deposited token and 50% of the secondary token)
- then deposit the tokens into a liquidity pool (e.g. wstETH/ETH)
- the tokens will accrue interest over time, determined by the APY (e.g. 4.75%/year)
- and will also pay interest on the borrowed tokens (e.g. 2%/year)
- when exit the strategy, the LP tokens will be redeemed for the two tokens (deposit and secondary token)
- the secondary token will be converted back into the deposited token
- you'll pay off your debt and keep the rest
- your profit will be given by the amount left after paying off the debt - the amount initially deposited
***
you can also use it to simulate simpler strategies:
- set the leverage to 0 to ignore the lending and borrowing part (e.g. liquidity pool farming with no leverage)
- set the liquidity pool ratio to 10% to ignore the conversion to liquidity pool tokens (e.g. leveraged farming a single token)
- set the APY to 0% to simulate only the borrowing costs
***
the default parameters are a real world example using the Notional wstETH/ETH leveraged vaults strategy. Notional built
~the code is messy af, but you can find it [here](https://github.com/0xrdt/defi_simulatooo~)~
by [@0xDoing](https://twitter.com/0xDoing)
"""
)
st.subheader("Inputs:")
col1, col2 = st.columns(2)
deposited_amount = col1.number_input(
"Deposited Amount",
value=125.0,
step=0.0001,
help="E.g. 100 if you're going to deposit 100 ETH in the strategy",
)
leverage = col2.number_input(
"Leverage (total asset amount = 1 + leverage)",
value=2.0,
step=0.01,
help="E.g. 2 if you're going to borrow 2x the deposited amount and your total assets will be 3x the deposited amount",
)
initial_price = col1.number_input(
"Initial Price (secondary token / deposited token price)",
value=0.985,
step=0.0001,
help="E.g. 0.985 if the wstETH price is 0.985 ETH, you deposited ETH and the strategy uses the wstETH/ETH liquidity pool",
)
final_price = col2.number_input(
"Estimated Final Price (secondary token / deposited token price)",
value=0.970,
step=0.0001,
help="E.g. 0.985 if the wstETH price is 0.985 ETH, you deposited ETH and the strategy uses the wstETH/ETH liquidity pool",
)
estimated_apy = col1.number_input(
"Estimated average APY (%)",
value=5.75,
step=0.01,
help="E.g. 5.75 if you expect to receive a time-weighted average yield of 5.75%",
)
borrowing_interest_rate = col2.number_input(
"Estimated average borrowing interest rate (%)",
value=5.06,
step=0.01,
help="E.g. 5.06 if you expect to pay a time-weighted average interest rate of 5.06% on your borrowed assets. If there's a one-time borrow fee, add it in the advanced inputs section",
)
liquidity_pool_composition = col1.number_input(
"Liquidity Pool Ratio (use the % of the deposited token)",
value=50.0,
step=0.01,
help="E.g. 50 if you expect to deposit 50% of the deposited token and 50% of the secondary token in the liquidity pool",
)
days_invested = col2.number_input(
"Days Invested",
value=60,
step=1,
help="E.g. 60 if you want to see your profits/costs/etc after 60 days",
)
st.subheader("Optional Tx Cost Inputs:")
col1, col2 = st.columns(2)
gas_price = col1.number_input(
"gas price when depositing",
value=16.0,
step=0.01,
min_value=1.0,
max_value=1000.0,
help="E.g. 16 if the gas price is 16 gwei",
)
eth_price_vs_deposit_token = col2.number_input(
"ETH price relative to the deposited token",
value=1.0,
step=0.000001,
help="E.g. 1400 if the ETH price is 1400 USDC and your deposit token is USDC",
)
gas_used_entry = col1.number_input(
"gas cost of entering the strategy",
value=1_250_000,
step=1,
help="E.g. 1_250_000 if the gas cost is 1.25 million gas units (you can set it to 0 to ignore it)",
)
gas_used_exit = col2.number_input(
"gas cost of exiting the strategy",
value=1_250_000,
step=1,
help="E.g. 1_250_000 if the gas cost is 1.25 million gas units (you can set it to 0 to ignore it)",
)
st.subheader("Advanced Inputs:")
col1, col2 = st.columns(2)
deposit_to_secondary_token_conversion_fee = col1.number_input(
"LP Swap Fee (deposit token to secondary token conversion) (%)",
value=0.04,
step=0.001,
help="E.g. 0.04 if you expect to pay a 0.04% fee when swapping the deposited token to the secondary token before entering the liquidity pool",
)
borrow_fee = col2.number_input(
"Borrow fee (%)",
value=0.03,
step=0.01,
help="E.g. 0.03 if you expect to pay a 0.03% one-time fee when borrowing the deposited token",
)
base_parameters = {
"Deposited Amount": deposited_amount,
"Leverage": leverage,
"Initial Price": initial_price,
"Final Price": final_price,
"Estimated APY (%)": estimated_apy,
"Borrowing Interest Rate": borrowing_interest_rate,
"Liquidity Pool Ratio": liquidity_pool_composition,
"Days Invested": days_invested,
"Gas Price": gas_price,
"ETH Price vs Deposit Token": eth_price_vs_deposit_token,
"Gas Used Entry": gas_used_entry,
"Gas Used Exit": gas_used_exit,
"Gas Cost Entry": gas_used_entry * gas_price * eth_price_vs_deposit_token,
"Gas Cost Exit": gas_used_exit * gas_price * eth_price_vs_deposit_token,
"LP Swap Fee": deposit_to_secondary_token_conversion_fee,
"Borrow Fee": borrow_fee,
}
def simulate(parameters: dict):
deposited_amount = parameters["Deposited Amount"]
leverage = parameters["Leverage"]
initial_price = parameters["Initial Price"]
final_price = parameters["Final Price"]
estimated_apy = parameters["Estimated APY (%)"]
borrowing_interest_rate = parameters["Borrowing Interest Rate"]
liquidity_pool_composition = parameters["Liquidity Pool Ratio"]
days_invested = parameters["Days Invested"]
gas_price = parameters["Gas Price"]
eth_price_vs_deposit_token = parameters[
"ETH Price vs Deposit Token"
] # eg ETH/USDC = 1400
gas_used_entry = parameters["Gas Used Entry"]
gas_used_exit = parameters["Gas Used Exit"]
gas_cost_entry = parameters["Gas Cost Entry"]
gas_cost_exit = parameters["Gas Cost Exit"]
deposit_to_secondary_token_conversion_fee = parameters["LP Swap Fee"]
borrow_fee = parameters["Borrow Fee"]
# apy (yield compounds)
daily_gain = (1 + estimated_apy / 100) ** (1 / 365) - 1
total_gain_pct = ((1 + daily_gain) ** days_invested - 1) * 100
# apr (interest doesn't compound)
# daily_borrowing_interest_rate = ((1+borrowing_interest_rate/100)**(1/365)-1)
# total_interest_pct = ((1+daily_borrowing_interest_rate)**days_invested-1)*100
daily_borrowing_interest_rate = (borrowing_interest_rate / 100) / 365
total_interest_pct = (daily_borrowing_interest_rate * days_invested) * 100
initial_asset_amount = deposited_amount * (1 + leverage)
initial_liability_amount = (deposited_amount * leverage) * (
1 + total_interest_pct / 100
)
initial_deposited_token_pool_amount = (
initial_asset_amount * liquidity_pool_composition / 100
)
initial_secondary_token_pool_amount = (
initial_asset_amount
* (100 - deposit_to_secondary_token_conversion_fee)
/ 100
* (100 - liquidity_pool_composition)
/ 100
) / initial_price
final_deposited_token_pool_amount = initial_deposited_token_pool_amount * (
1 + total_gain_pct / 100
)
final_secondary_token_pool_amount = initial_secondary_token_pool_amount * (
1 + total_gain_pct / 100
)
final_asset_amount = (
final_deposited_token_pool_amount
+ (final_secondary_token_pool_amount * final_price)
* (100 - deposit_to_secondary_token_conversion_fee)
/ 100
)
final_liability_amount = initial_liability_amount
returns = (
deposited_amount
* (leverage + 1)
* (total_gain_pct / 100)
* (
liquidity_pool_composition / 100
+ (final_price / initial_price) * (100 - liquidity_pool_composition) / 100
)
)
costs = deposited_amount * leverage * (total_interest_pct / 100)
profits = returns - costs - initial_liability_amount * borrow_fee / 100
profit_pct = profits / deposited_amount * 100
gas_cost_entry = gas_price * (gas_used_entry) / 1e9
gas_cost_exit = gas_price * (gas_used_exit) / 1e9
profits_after_gas = (
profits - (gas_cost_entry + gas_cost_exit) * eth_price_vs_deposit_token
)
profit_pct_after_gas = profits_after_gas / deposited_amount * 100
return {
"Initial Asset Amount": initial_asset_amount,
"Initial Liability Amount": initial_liability_amount,
"Initial Deposited Token Pool Amount": initial_deposited_token_pool_amount,
"Initial Secondary Token Pool Amount": initial_secondary_token_pool_amount,
"Final Deposited Token Pool Amount": final_deposited_token_pool_amount,
"Final Secondary Token Pool Amount": final_secondary_token_pool_amount,
"Final Asset Amount": final_asset_amount,
"Final Liability Amount": final_liability_amount,
"Costs": costs,
"Returns": returns,
"Profits": profits,
"Profit %": profit_pct,
"Gas Cost Entry": gas_cost_entry,
"Gas Cost Exit": gas_cost_exit,
"Profits After Gas": profits_after_gas,
"Profit % After Gas": profit_pct_after_gas,
}
st.subheader("Results:")
results = simulate(base_parameters)
initial_asset_amount = results["Initial Asset Amount"]
initial_liability_amount = results["Initial Liability Amount"]
initial_deposited_token_pool_amount = results["Initial Deposited Token Pool Amount"]
initial_secondary_token_pool_amount = results["Initial Secondary Token Pool Amount"]
final_deposited_token_pool_amount = results["Final Deposited Token Pool Amount"]
final_secondary_token_pool_amount = results["Final Secondary Token Pool Amount"]
final_asset_amount = results["Final Asset Amount"]
final_liability_amount = results["Final Liability Amount"]
costs = results["Costs"]
returns = results["Returns"]
profits = results["Profits"]
profit_pct = results["Profit %"]
gas_cost_entry = results["Gas Cost Entry"]
gas_cost_exit = results["Gas Cost Exit"]
profits_after_gas = results["Profits After Gas"]
profit_pct_after_gas = results["Profit % After Gas"]
if not st.checkbox("Show compact results"):
# st.markdown("##### Initial Amounts:")
with st.expander("Initial Amounts:"):
col1, col2 = st.columns(2)
col1.metric("initial asset amount", f"{initial_asset_amount:.4f}")
col2.metric("initial liability amount", f"{initial_liability_amount:.4f}")
# st.markdown("##### After converting to secondary token (to add it to the pool):")
with st.expander("After converting to secondary token (to add it to the pool):"):
col1, col2 = st.columns(2)
col1.metric(
"initial deposited token pool amount",
f"{initial_deposited_token_pool_amount:.4f}",
)
col2.metric(
"initial secondary token pool amount",
f"{initial_secondary_token_pool_amount:.4f}",
)
# st.markdown("##### After staking returns, rewards and secondary token price change:")
with st.expander(
"After staking returns, rewards and secondary token price change:"
):
col1, col2 = st.columns(2)
col1.metric(
"final deposited token pool amount",
f"{final_deposited_token_pool_amount:.4f}",
)
col2.metric(
"final secondary token pool amount",
f"{final_secondary_token_pool_amount:.4f}",
)
# st.markdown("##### Final Amounts:")
with st.expander("Final Amounts:"):
col1, col2 = st.columns(2)
col1.metric("final asset amount", f"{final_asset_amount:.4f}")
col2.metric(
"final liability amount (same as initial)", f"{final_liability_amount:.4f}"
)
# st.markdown("##### Results:")
with st.expander("Results:"):
col1, col2 = st.columns(2)
col1.metric("returns", f"{returns:.4f}")
col2.metric("costs", f"{costs:.4f}")
# st.markdown("##### Profits (before tx costs):")
with st.expander("Profits (before tx costs):"):
col1, col2 = st.columns(2)
col1.metric("profits", f"{profits:.4f}")
col2.metric("profits (% of the deposited amount)", f"{profit_pct:.4f}")
else:
st.write(results)
st.subheader("Tx Cost Results:")
# st.markdown("##### Estimated tx costs:")
with st.expander("Estimated tx costs:"):
col1, col2 = st.columns(2)
col1.metric(f"estimated tx cost to enter the strategy:", f"{gas_cost_entry} ETH")
col2.metric(f"estimated tx cost to exit the strategy:", f"{gas_cost_exit} ETH")
# st.markdown("##### Profits (after tx costs):")
with st.expander("Profits (after tx costs):"):
col1, col2 = st.columns(2)
col1.metric(
"profits (after tx costs)", f"{profits - gas_cost_entry - gas_cost_exit:.4f}"
)
col2.metric(
"profits (% of the deposited amount)",
f"{(profits - gas_cost_entry - gas_cost_exit) / deposited_amount * 100:.4f}",
)
# st.markdown("##### Time to breakeven (in days):")
with st.expander("Time to breakeven (in days):"):
try:
def profit_per_day(day):
parameters = base_parameters.copy()
parameters["Days Invested"] = day
return simulate(parameters)["Profits After Gas"]
days_to_breakeven = optimize.brenth(lambda x: profit_per_day(x), 0, 10_000)
st.metric("days to breakeven", f"{days_to_breakeven:.4f}")
except Exception as e:
st.metric("days to breakeven", f"never")
st.subheader("Visualizing the impact of the parameters:")
with st.expander("1 output vs 1 parameter:"):
st.warning(f"all other parameters will be set to the inputs you gave earlier")
st.error(
f"some plots may look linear, if that happens it's because the parameters are not high enough to show the exponential growth"
)
col1, col2 = st.columns(2)
choosen_parameter = col1.selectbox(
"Choose a parameter",
list(base_parameters.keys()),
index=list(base_parameters.keys()).index("Estimated APY (%)"),
)
choosen_output = col2.selectbox(
"Choose an output",
list(results.keys()),
index=list(results.keys()).index("Profit % After Gas"),
)
def output_per_parameter(parameter, choosen_parameter, choosen_output):
parameters = base_parameters.copy()
parameters[choosen_parameter] = parameter
return simulate(parameters)[choosen_output]
# try:
# days_to_breakeven = optimize.brenth(lambda x: profit_per_apr(x), 0, 10_000)
# st.metric(f"APY to breakeven after {days_invested} days", f'{days_to_breakeven:.4f}')
# except Exception as e:
# st.metric("days to breakeven", f'never')
col1, col2, col3 = st.columns(3)
min_param = col1.number_input(
f"Min {choosen_parameter} to simulate", 0.0, 250_000.0, 0.0
)
max_param = col2.number_input(
f"Max {choosen_parameter} to simulate", 0.0, 250_000.0, 12.5
)
step_size = col3.number_input(
"Step Size", 0.0, 250_000.0, 0.01, key="first_step_size"
)
fig = go.Figure()
vec = np.arange(min_param, max_param, step_size)
fig.add_trace(
go.Scatter(
x=vec,
y=[output_per_parameter(x, choosen_parameter, choosen_output) for x in vec],
name="profit",
)
)
fig.update_layout(
title=f"{choosen_output} vs {choosen_parameter}",
xaxis_title=f"{choosen_parameter}",
yaxis_title=f"{choosen_output}",
)
st.plotly_chart(fig, use_container_width=True)
with st.expander("1 output vs 2 parameters:"):
st.warning(f"all other parameters will be set to the inputs you gave earlier")
st.error(
f"some plots may look linear, if that happens it's because the parameters are not high enough to show the exponential growth"
)
col1, col2, col3 = st.columns(3)
choosen_parameter_1 = col1.selectbox(
"Choose the first parameter",
list(base_parameters.keys()),
index=list(base_parameters.keys()).index("Estimated APY (%)"),
)
choosen_parameter_2 = col2.selectbox(
"Choose the second parameter",
list(base_parameters.keys()),
index=list(base_parameters.keys()).index("Leverage"),
)
choosen_output = col3.selectbox(
"Choose an output",
list(results.keys()),
index=list(results.keys()).index("Profit % After Gas"),
key="second_output",
)
min_param_1 = col1.number_input(
f"Min {choosen_parameter_1} to simulate",
0.0,
250_000.0,
0.0,
key="first_min_param",
)
max_param_1 = col2.number_input(
f"Max {choosen_parameter_1} to simulate",
0.0,
250_000.0,
12.5,
key="first_max_param",
)
step_size_1 = col3.number_input(
"Step Size",
0.0,
250_000.0,
0.1,
key="second_step_size",
)
min_param_2 = col1.number_input(
f"Min {choosen_parameter_2} to simulate",
0.0,
250_000.0,
0.0,
key="second_min_param",
)
max_param_2 = col2.number_input(
f"Max {choosen_parameter_2} to simulate",
0.0,
250_000.0,
12.5,
key="second_max_param",
)
step_size_2 = col3.number_input(
"Step Size", 0.0, 250_000.0, 0.1, key="third_step_size"
)
def output_per_parameters(
parameter_1,
parameter_2,
choosen_parameter_1,
choosen_parameter_2,
choosen_output,
):
parameters = base_parameters.copy()
parameters[choosen_parameter_1] = parameter_1
parameters[choosen_parameter_2] = parameter_2
return simulate(parameters)[choosen_output]
vec_param_1 = np.arange(min_param_1, max_param_1, step_size_1)
vec_param_2 = np.arange(min_param_2, max_param_2, step_size_2)
z = [
[
output_per_parameters(
x, y, choosen_parameter_1, choosen_parameter_2, choosen_output
)
for x in vec_param_1
]
for y in vec_param_2
]
if st.checkbox("Show 3D plot", value=True):
# plot surface
fig = go.Figure()
fig.add_trace(
go.Surface(
x=vec_param_1,
y=vec_param_2,
z=z,
colorscale="RdBu",
)
)
fig.update_layout(
title=f"[surface plot] {choosen_output} after {days_invested} days vs {choosen_parameter_1} and {choosen_parameter_2}",
scene=dict(
xaxis_title=f"{choosen_parameter_1}",
yaxis_title=f"{choosen_parameter_2}",
zaxis_title=f"{choosen_output}",
),
height=600,
)
# add surface opacity
fig.update_traces(opacity=0.75)
fig.update_traces(
contours_z=dict(
show=True, usecolormap=True, project_z=True, highlightcolor="black"
)
)
st.plotly_chart(fig, use_container_width=True)
if st.checkbox("Show contour plot", value=True):
# plot contour
fig = go.Figure()
fig.add_trace(
go.Contour(
x=vec_param_1,
y=vec_param_2,
z=z,
colorscale="RdBu",
contours_coloring=None, # can also be 'lines', or 'none' or 'heatmap'
line_smoothing=0,
),
)
fig.update_layout(
title=f"[countour plot] {choosen_output} after {days_invested} days vs {choosen_parameter_1} vs {choosen_parameter_2}",
scene=dict(
xaxis_title=f"{choosen_parameter_1}",
yaxis_title=f"{choosen_parameter_2}",
zaxis_title=f"{choosen_output}",
),
# height=800,
)
# set title
fig.update_xaxes(title_text=f"{choosen_parameter_1}", nticks=20)
fig.update_yaxes(title_text=f"{choosen_parameter_2}", nticks=20)
# showlabels=True
st.plotly_chart(fig, use_container_width=True)