-
Notifications
You must be signed in to change notification settings - Fork 0
/
average_run.py
498 lines (319 loc) · 15.6 KB
/
average_run.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
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
import plotly.graph_objs as go
import plotly.offline as pyo
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import random
import time
from datetime import timedelta
# Machine learning tools
from sklearn.linear_model import LinearRegression, Ridge, MultiTaskLassoCV
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
from sklearn.model_selection import train_test_split, TimeSeriesSplit
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.multioutput import MultiOutputRegressor
from sklearn.feature_selection import mutual_info_regression
# Deep Learning tools
import tensorflow as tf
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout, Input
from keras.optimizers import Adam, RMSprop, SGD
from keras.callbacks import ReduceLROnPlateau, EarlyStopping
from keras.regularizers import l2
# Additional tools
from scipy import signal
from scipy.optimize import minimize
from itertools import combinations, product
# External data and APIs
import yfinance as yf
from dune_client.client import DuneClient
import requests
import streamlit as st
from scripts.data_processing import test_data_copy, test_data, targets, features, temporals, dai_ceilings
from scripts.rl_agent import RlAgent
from scripts.utils import mvo, historical_sortino, visualize_mvo_results, evaluate_predictions, generate_action_space, calc_cumulative_return
from scripts.simulation import RL_VaultSimulator
from scripts.environment import SimulationEnvironment
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
import matplotlib as mpl
# Disable scientific notation globally
mpl.rcParams['axes.formatter.useoffset'] = False
mpl.rcParams['axes.formatter.use_locale'] = False
mpl.rcParams['axes.formatter.limits'] = (-5, 6)
# Function to apply ScalarFormatter globally
def set_global_scalar_formatter():
for axis in ['x', 'y']:
plt.gca().ticklabel_format(axis=axis, style='plain', useOffset=False)
plt.gca().yaxis.set_major_formatter(ScalarFormatter())
plt.gca().xaxis.set_major_formatter(ScalarFormatter())
pd.options.display.float_format = '{:.0f}'.format
# Register the formatter with a default plot
random.seed(20)
np.random.seed(20)
tf.random.set_seed(20)
sim_cutoff = pd.to_datetime('2024-03-20 00:00:00').tz_localize(None)
test_data.index = pd.to_datetime(test_data.index).tz_localize(None)
historical_sim = test_data[test_data.index <= sim_cutoff]
historical_sim = historical_sim[targets].merge(test_data['RWA Vault_collateral_usd'], left_index=True, right_index=True, how='left')
historical_optimized_weights, historical_portfolio_returns, historical_portfolio_composition, historical_total_portfolio_value = mvo(historical_sim)
# In[912]:
historical_portfolio_daily_returns, historical_downside_returns, historical_excess_returns, historical_sortino_ratio = historical_sortino(historical_portfolio_returns,historical_portfolio_composition)
# In[913]:
historical_returns = calc_cumulative_return(historical_portfolio_daily_returns)
def run_simulation():
simulation_data = test_data_copy
portfolio_mvo_weights, portfolio_returns, portfolio_composition, total_portfolio_value = mvo(simulation_data)
# In[882]:
# Assuming you have these vault names as keys in optimized_weight_dict
vault_names = [
'BTC Vault_collateral_usd', 'ETH Vault_collateral_usd', 'stETH Vault_collateral_usd', 'Stablecoin Vault_collateral_usd',
'Altcoin Vault_collateral_usd', 'LP Vault_collateral_usd', 'RWA Vault_collateral_usd', 'PSM Vault_collateral_usd'
]
# Map from the detailed keys to simplified keys used in optimized_weight_dict
key_mapping = {
'BTC Vault_collateral_usd',
'ETH Vault_collateral_usd',
'stETH Vault_collateral_usd',
'Stablecoin Vault_collateral_usd'
'Altcoin Vault_collateral_usd',
'LP Vault_collateral_usd',
'RWA Vault_collateral_usd',
'PSM Vault_collateral_usd'
}
# Apply this mapping to the initial_weights to align with optimized_weight_dict
initial_weights = dict(portfolio_composition.loc['2022-05-20'].to_dict())
optimized_weight_dict = dict(zip(vault_names, portfolio_mvo_weights))
# Now both dictionaries use the same keys:
print("Initial Weights on 2022-05-20:", initial_weights)
print("Optimized Weights:", optimized_weight_dict)
# Your RlAgent can now be initialized and used with these dictionaries
# In[883]:
# ### Sets Up Action Space: for Some Reason, need to run this first for decent predictions on first try: think related to how some variables are initialized
# In[884]:
str(simulation_data.index.max())
# test_ceilings = test_data[dai_ceilings]
#
#
# start_date_dt = pd.to_datetime(start_date) # Convert string to datetime
# historical_cutoff = start_date_dt - pd.DateOffset(days=1)
# test_historical_data = test_ceilings[test_ceilings.index <= historical_cutoff]
#
# test_historical_data
# last_dai_ceiling = test_historical_data.iloc[-1]
# last_dai_ceiling
# In[885]:
#test_data_copy[['mcap_total_volume']].plot()
# ### Non Q Sim
# In[886]:
"""
vault_action_ranges = {
'stETH Vault_dai_ceiling': [-0.5, 0.5],
'ETH Vault_dai_ceiling': [-0.5, 0.5],
'BTC Vault_dai_ceiling': [-0.5, 0.5], # can try -1 for BTC
'Altcoin Vault_dai_ceiling': [-0.25, 0.25],
'Stablecoin Vault_dai_ceiling': [-0.25, 0.25],
'LP Vault_dai_ceiling': [-0.15, 0.15],
'RWA Vault_dai_ceiling': [0, 0], # If no changes are allowed
'PSM Vault_dai_ceiling': [-0.5, 0.5]
}
"""
# Define action space as a list of dictionaries
vault_action_ranges = {
'stETH Vault_dai_ceiling': [-0.5, 0.5],
'ETH Vault_dai_ceiling': [-0.5, 0.5],
'BTC Vault_dai_ceiling': [-0.5, 0.5], # can try -1 for BTC
'Altcoin Vault_dai_ceiling': [-0.25, 0.25],
'Stablecoin Vault_dai_ceiling': [-0.25, 0.25],
'LP Vault_dai_ceiling': [0 , 0], # The volatility causes abnormal daily returns
'RWA Vault_dai_ceiling': [0, 0], # If no changes are allowed
'PSM Vault_dai_ceiling': [-0.5, 0.5]
}
action_space = generate_action_space(vault_action_ranges)
# Assuming `optimized_weight_dict` and `initial_weights` are defined elsewhere in your script
agent = RlAgent(action_space, optimized_weight_dict, vault_action_ranges, initial_strategy_period=1)
initial_action = agent.initial_strategy(initial_weights)
print("Initial Action Computed:", initial_action)
simulation_data = test_data_copy
simulation_data.index = simulation_data.index.tz_localize(None) # Remove timezone information
#05-20-2022
start_date = '2022-05-20'
end_date = '2024-03-20'
#end_date = '2022-07-20'
simulator = RL_VaultSimulator(simulation_data, test_data_copy, features, targets, temporals, start_date, end_date, scale_factor=300000000, minimum_value_factor=0.05, volatility_window=250)
simulator.train_model()
#simulator.set_parameters()
environment = SimulationEnvironment(simulator, start_date, end_date, agent)
environment.reset()
state, reward, done, info = environment.run()
#simulator.plot_simulation_results()
action_df = environment.generate_action_dataframe()
# ### DAI Ceilings
#
# In[888]:
sim_dai_ceilings = simulator.dai_ceilings_history
# In[889]:
# ### Actions Log
# In[890]:
historical = test_data_copy[targets]
start_date_dt = pd.to_datetime(start_date) # Convert string to datetime
historical_cutoff = start_date_dt - pd.DateOffset(days=1)
historical_data = historical[historical.index <= historical_cutoff]
test_data.index = pd.to_datetime(test_data.index).tz_localize(None)
historical_data = historical_data.merge(test_data['RWA Vault_collateral_usd'], left_index=True, right_index=True, how='left')
historical_data
historical_optimized_weights, historical_portfolio_returns, historical_portfolio_composition, historical_total_portfolio_value = mvo(historical_data)
historical_portfolio_daily_returns, historical_downside_returns, historical_excess_returns, historical_sortino_ratio = historical_sortino(historical_portfolio_returns,historical_portfolio_composition)
# In[891]:
action_df.set_index('date', inplace=True)
sortino_timeseries = action_df['current sortino ratio'].dropna()
# In[892]:
# In[898]:
# In[900]:
rl_sim_results = simulator.results
#evaluate_predictions(rl_sim_results, historical)
# ### Data Prep for MVO
# In[901]:
start_date_dt = pd.to_datetime(start_date) # Convert string to datetime
historical_cutoff = start_date_dt - pd.DateOffset(days=1)
# In[902]:
historical_ceilings = test_data_copy[dai_ceilings]
ceiling_historical_cutoff = historical_cutoff.tz_localize(None)
historical_ceilings_for_sim = historical_ceilings[historical_ceilings.index <= ceiling_historical_cutoff]
historical_ceilings_for_sim
combined_ceiling_data = pd.concat([historical_ceilings_for_sim, sim_dai_ceilings])
# In[906]:
historical_data = historical[historical.index <= historical_cutoff]
rl_combined_data = pd.concat([historical_data, rl_sim_results])
#mvo_combined_data = pd.concat([historical_data, mvo_sim_results])
#print(mvo_combined_data)
print(rl_combined_data)
# In[907]:
rl_combined_data.index = rl_combined_data.index.tz_localize(None)
#mvo_combined_data.index = mvo_combined_data.index.tz_localize(None)
test_data['RWA Vault_collateral_usd'].index = test_data['RWA Vault_collateral_usd'].index.tz_localize(None)
#Since RWA is not a target, we need to add back in for MVO calculations
rl_sim_w_RWA = rl_combined_data.merge(test_data['RWA Vault_collateral_usd'], left_index=True, right_index=True, how='left')
#mvo_sim_w_RWA = mvo_combined_data.merge(test_data['RWA Vault_collateral_usd'], left_index=True, right_index=True, how='left')
# Optional: Sort the DataFrame by index if it's not already sorted
rl_sim_w_RWA.sort_index(inplace=True)
#mvo_sim_w_RWA.sort_index(inplace=True)
# In[909]:
sim_cutoff = rl_sim_w_RWA.index.max()
sim_cutoff
# In[910]:
# Assuming 'test_data' is the DataFrame with the timezone-aware index
#test_data.index = pd.to_datetime(test_data.index).tz_localize(None)
# Now perform the merge
historical_sim = test_data[test_data.index <= sim_cutoff]
historical_sim = historical_sim[targets].merge(test_data['RWA Vault_collateral_usd'], left_index=True, right_index=True, how='left')
historical_optimized_weights, historical_portfolio_returns, historical_portfolio_composition, historical_total_portfolio_value = mvo(historical_sim)
# In[912]:
historical_portfolio_daily_returns, historical_downside_returns, historical_excess_returns, historical_sortino_ratio = historical_sortino(historical_portfolio_returns,historical_portfolio_composition)
print(f'historical tvl as of {historical_total_portfolio_value.index.max()} : {historical_total_portfolio_value.iloc[-1]}')
print(f'historical soritno ratio as of {historical_total_portfolio_value.index.max()} : {historical_sortino_ratio}')
# In[913]:
historical_returns = calc_cumulative_return(historical_portfolio_daily_returns)
print(f'historical cumulative return as of {historical_returns.index.max()} : {historical_returns.iloc[-1]}')
rl_portfolio_mvo_weights, rl_portfolio_returns, rl_portfolio_composition, rl_total_portfolio_value = mvo(rl_sim_w_RWA)
rl_sim_portfolio_daily_returns, rl_sim_downside_returns, rl_sim_excess_returns, rl_sim_sortino_ratio = historical_sortino(rl_portfolio_returns,rl_portfolio_composition)
# In[918]:
rl_optimized_returns = calc_cumulative_return(rl_sim_portfolio_daily_returns)
# Drop duplicates, keeping the first occurrence
rl_optimized_returns = rl_optimized_returns[~rl_optimized_returns.index.duplicated(keep='first')]
#
return rl_optimized_returns, rl_total_portfolio_value, rl_sim_sortino_ratio, sortino_timeseries
def main():
num_runs = 5
cumulative_returns = []
tvls = []
sortino_ratios = []
sortino_timeseries_list = []
for _ in range(num_runs):
cumulative_return, tvl, sortino_ratio, sortino_timeseries = run_simulation()
cumulative_returns.append(cumulative_return)
tvls.append(tvl)
sortino_ratios.append(sortino_ratio)
sortino_timeseries_list.append(sortino_timeseries)
avg_cumulative_return = np.mean(cumulative_returns)
avg_tvl = np.mean(tvls)
avg_sortino_ratio = np.mean(sortino_ratios)
print(f"Average Cumulative Return: {avg_cumulative_return}")
print(f"Average TVL: {avg_tvl}")
print(f"Average Sortino Ratio: {avg_sortino_ratio}")
# Plot the results using Plotly
colors = sns.color_palette("husl", num_runs)
# Plot cumulative returns
traces = []
for i in range(num_runs):
traces.append(go.Scatter(
x=cumulative_returns[i].index,
y=cumulative_returns[i].values,
mode='lines+markers',
name=f'Run {i + 1}',
line=dict(color=f'rgb{colors[i]}')
))
traces.append(go.Scatter(
x=historical_returns.index,
y=historical_returns.values,
mode='lines',
name='Historical Cumulative Return',
line=dict(color='black', dash='dash')
))
layout = go.Layout(
title='Cumulative Returns per Run',
xaxis=dict(title='Run'),
yaxis=dict(title='Cumulative Return'),
legend=dict(x=0.1, y=0.9)
)
fig = go.Figure(data=traces, layout=layout)
pyo.iplot(fig)
# Plot TVL
traces = []
for i in range(num_runs):
traces.append(go.Scatter(
x=tvls[i].index,
y=tvls[i].values,
mode='lines+markers',
name=f'Run {i + 1}',
line=dict(color=f'rgb{colors[i]}')
))
traces.append(go.Scatter(
x=historical_total_portfolio_value.index,
y=historical_total_portfolio_value.values,
mode='lines',
name='Historical TVL',
line=dict(color='black', dash='dash')
))
layout = go.Layout(
title='TVL per Run',
xaxis=dict(title='Run'),
yaxis=dict(title='TVL'),
legend=dict(x=0.1, y=0.9)
)
fig = go.Figure(data=traces, layout=layout)
pyo.iplot(fig)
# Plot Sortino ratios
traces = []
for i in range(num_runs):
traces.append(go.Scatter(
x=sortino_timeseries_list[i].index,
y=sortino_timeseries_list[i].values,
mode='lines+markers',
name=f'Run {i + 1}',
line=dict(color=f'rgb{colors[i]}')
))
layout = go.Layout(
title='Sortino Ratio Timeseries per Run',
xaxis=dict(title='Date'),
yaxis=dict(title='Sortino Ratio'),
legend=dict(x=0.1, y=0.9)
)
fig = go.Figure(data=traces, layout=layout)
pyo.iplot(fig)
if __name__ == "__main__":
main()