Balancer Simulations
  • Getting Started
  • Understanding Balancer AMMs
    • Balancer Basics
    • Use cases
  • Simulation Packages
    • Model Overview
    • Pool Functions
    • Pool Exploration
  • Code & Instructions
    • Dynamic weights changing
    • Arbitrage agent
    • Balancer: the Python edition
      • ActionDecoder
      • pool_state_updates.py
      • partial_state_update_blocks.py
      • Spot Price calculation
      • balancer_constants.py
      • balancer_math.py
      • balancer_pool.py
      • Verification & Limitations
    • Naming Convention
    • Adding On-chain Transaction Data and USD Prices
    • Modeling Time
Powered by GitBook
On this page

Was this helpful?

  1. Code & Instructions
  2. Balancer: the Python edition

Spot Price calculation

Previouspartial_state_update_blocks.pyNextbalancer_constants.py

Last updated 4 years ago

Was this helpful?

Balancer Pool's spot prices are the price of one token bound to the pool in relation with the rest (for example, in a WETH / DAI pool, the spot price of WETH will be in DAI. This is, for every WETH i'll get out XXX DAI from the pool (swap fees apply)

This logic is in a state update method, that will apply calc_spot_pricefrom

Every pool update in the policies (i.e. balance changes in the pool's bounded tokens) will signal the update of spot prices

def calculate_spot_prices(pool: dict, ref_token: str):
    swap_fee = pool['swap_fee']
    balance_in = pool['tokens'][ref_token].balance
    weight_in = pool['tokens'][ref_token].weight
    spot_prices = {}
    for token in pool['tokens']:
        if token == ref_token:
            continue
        balance_out = pool['tokens'][token].balance
        weight_out = pool['tokens'][token].weight

        price = BalancerMath.calc_spot_price(token_balance_in=Decimal(balance_in),
                                             token_weight_in=Decimal(weight_in),
                                             token_balance_out=Decimal(balance_out),
                                             token_weight_out=Decimal(weight_out),
                                             swap_fee=Decimal(swap_fee))
        spot_prices[token] = price
    return spot_prices


def s_update_spot_prices(params, substep, state_history, previous_state, policy_input):
    pool = policy_input.get('pool_update')
    if pool is None:
        return 'spot_prices', previous_state['spot_prices']

    ref_token = params[0]['spot_price_reference']

    spot_prices = calculate_spot_prices(pool, ref_token)
    return 'spot_prices', spot_prices

The spot price reference can be set as a cadCAD parameter in the Notebooks, allowing for parameter sweeps to run the simulation several times with different spot price result for each token.

The code below will generate 2 subsets of results in the dataframe output after the simulation is run:

import pprint
pp = pprint.PrettyPrinter(indent=4)

from decimal import Decimal

# Spot price reference must be a symbol of a token in the pool in ALL_CAPS, you can ignore the spot price parameter for the simulations in this notebook.
parameters = {
    'spot_price_reference': ['DAI', 'WETH'],
}

BMath