---
# System prepended metadata

title: APR and APY Calcuation for Margin

---

# APR and APY Calcuation for Margin

Our margin APR calculation is 
`APR = (value_of_interest_payment / value_in_liquidity_pool) * n`

and our margin APY calcuation is 

`APY = (1 + value_of_interest_payment / value_in_liquidity_pool) ^ n - 1`

where `n = number_of_payment_slots_per_year`

Unlike other calculations of APR or APY for rewards, etc, to get the exact APR and APY a pooler will earn in a period, we would need to know the quantity and sizes of the mtps and the pool depths for that upcoming period.

Since we cannot predict the future with certainty, we will calculate margin APR & APY by looking backward.

We will look at the APR and APY backward in a short window that closely reflects what is in the pool at that moment. That can be something in the 15 mins to 2 hours band but for the pseudocode, we will use 1 hour.  

Pseudocode for calcuation of one pool's APR starting with 1 hour backward looking window. Can change if needed. 

We will be able to look backwards once the first hour (or which ever period) from genesis passes, so it is probably not necessary to worry about the case of the if statement as in the old code below (where we had less than one month of mtp data).  

## APR
```
margin_genesis_block = ?   # set starting block of margin trading release
data_window = 600   # blocks per one hour period 
blocks_per_year = 5256000   # six seconds per block


def get_apr(current_block, margin_genesis_block, data_window):
        
    # Accumulate past int paid at each block i, back data_window # of blocks
    # On each block i, sum interest of each open mtp j 
    
    cumulative_int_sum = 0
    
    for i in range(current_block - data_window, current_block):
    
        # At each block i:
        sum_both = sum(non_rowan_int_payments(i)) / rowan_price(i) + sum(rowan_int_payments(i)) 
        block_aggregate_int_rate = sum_both / (pool_depth_X(i) / rowan_price(i) + pool_depth_Y(i))
        cumulative_int_sum += block_aggregate_int_rate

    return cumulative_int_sum * (blocks_per_year/data_window) 
```



## APY

```
margin_genesis_block = ?   # set starting block of margin trading release
data_window = 600   # blocks per one hour period 
blocks_per_year = 5256000   # six seconds per block


def get_apr(current_block, margin_genesis_block, data_window):
        
    # Accumulate past int paid at each block i, back data_window # of blocks
    # On each block i, sum interest of each open mtp j 
    
    cumulative_int_sum = 0
    
    for i in range(current_block - data_window, current_block):
    
        # At each block i:
        sum_both = sum(non_rowan_int_payments(i)) / rowan_price(i) + sum(rowan_int_payments(i)) 
        block_aggregate_int_rate = sum_both / (pool_depth_X(i) / rowan_price(i) + pool_depth_Y(i))
        cumulative_int_sum += block_aggregate_int_rate

    return (1 + cumulative_int_sum)^(blocks_per_year/data_window) - 1
```



### Old code
```
        for j in range(count(mtps_currently_open)):

            cumulative_interest += int_payment[i][j] / pool_depth[i]
```

### Older code:

- if statement: current_block to start of the margin trading platform has less than the 30 days we want to look backward
- else statement: Margin trading has existed for longer than 30 days, so we can look backward and get 30 days worth of mtps to calculate APR. 

```
margin_genesis_block = ?   # set starting block of margin trading release
apr_data_window = 14400 * 30 = 432000   # blocks per day * num of days
blocks_per_year = 5256000   # six seconds per block


def get_apr(current_block, margin_genesis_block, apr_data_window):

    number_of_blocks_to_genesis = current_block - margin_genesis_block
    
    if number_of_blocks_to_genesis < apr_data_window:
        cumulative_interest_pct = 0   # initialize variable
        
        # Accumulate past int payments for each block i back to margin_genesis_block
        # On each block i, sum interest of each open mtp j 
 
        for i in range(margin_genesis_block, current_block):
        
            for j in range(count(mtps_open_at_block_i)):
                
                cumulative_interest += int_payment[i][j] / pool_depth[i]
        
        return (1 + cumulative_interest)^(blocks_per_year/number_of_blocks_to_genesis) - 1
    
    
    else if number_of_blocks_to_genesis >= apr_data_window:   
        cumulative_interest_pct = 0 
        
        # Accumulate past int paid at each block i, back apr_data_window # of blocks
        # On each block i, sum interest of each open mtp j 
        
        for i in range(current_block - apr_data_window, current_block):
        
            for j in range(count(mtps_open_at_block_i)):

                cumulative_interest += int_payment[i][j] / pool_depth[i]
        
        return (1 + cumulative_interest)^(blocks_per_year/apr_data_window) - 1
        
        
        
    