# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: vpython
#     language: python
#     name: vpython
# ---

# ## Run Model

# +
# Dependences
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Experiments
from model import run
from model.parts.utils import *
from model.plot_utils import *

pd.options.display.float_format = '{:.2f}'.format

# %matplotlib inline

df = run.run()
rdf = run.postprocessing(df)
# -

rdf

# +
hydra = rdf[['Q', 'H', 'Sq']]

hydra.columns = ['Hydra_Q', 'Hydra_H', 'Hydra_Sq']

rdf.columns
# -

print(rdf.pool[rdf.first_valid_index()])
# !!! does not run

rdf.pool[3].pool


# ### Define Arbitrage Wedge Plot in Code

def AW_plot(experiments,test_title,T):

    """
    This function plots the Arbitrage Wedge (AW) a metric that defines 
    overall potential IL in the pool due to external market prices.
    """
    
    AW = []
    V1 = []
    VH = []
     
    #Define timesteps as seen in plot_utils.py
    df = experiments
    df = df[df['substep'] == df.substep.max()]
    df.fillna(0,inplace=True)
    df.reset_index()

    #Iterate through timesteps and calculate AW for each time step.
    for i in range(df.substep.max(),T, df.substep.max()): 
        
        #Get Quantities for all assets in the pool and in the fee revenue basket.
        Ri = df.pool[i].pool['i']['R'] #+ df['fee_revenue'][i]['i']
        Rj = df.pool[i].pool['j']['R'] #+ df['fee_revenue'][i]['j']
        Q = rdf['Q'][i] #+ df['fee_revenue'][i]['Q']
        
        #Get Quantities for all shares.
        Si = df.pool[i].pool['i']['S']
        Sj = df.pool[i].pool['j']['S']           
        Sq = rdf['Sq'][i]
        
        #Get external market prices.
        #oracles = rdf[['oracle_price_i', 'oracle_price_j', 'oracle_price_Q']]
        MPi = 1 #oracles['oracle_price_i']
        MPj = 2 #oracles['oracle_price_j']
        MPq = 1 #oracles['oracle_price_Q']
        
        #Calculate Arbitrage Wedge.
        Total_V1t = Ri*MPi + Rj*MPj + Q*MPq
        Total_VHt = Si*MPi + Sj*MPj + Sq*MPq
        
        V1.append(Total_V1t)
        VH.append(Total_VHt)
        
        AWt = (Total_V1t -Total_VHt) / Total_VHt
        AW.append(AWt)
    
    mathAW = np.array(AW)

    #Plot Arbitrage Wedge across timesteps.
    plt.figure(figsize=(20,6))
    plt.subplot(141)
    plt.plot(mathAW)
    plt.xlabel('Timestep')
    plt.ylabel('Arbitrage Wedge')
    plt.legend()
    
    plt.subplot(142)
    plt.plot(V1)
    
    plt.subplot(143)
    plt.plot(VH)
    


# ### Execute Arbitrage Plot

print(len(rdf['timestep']))
AW_plot(rdf,'Arbitrage Wedge Omnipool', 300)



StarcoderdataJupyterScriptsDedupFiltered