title: Automated Market Makers - Liquidity Pool - Question about the calculation
body: <p>Esteemed,
  I would like to code all the steps for the correct calculation of a pool balance according to Uniswap V2 logic.<br/>
  Anyone who knew how to help can write in any programming language (Python, Javascript etc.), in this example I used R.<br/>
  The balancing process for a liquidity pool can be seen here: <a href="https://miro.medium.com/max/2000/1*iSxWe1qvy1yZPKuPlebpkQ.png" rel="nofollow noreferrer">example1</a> and <a href="https://2eguue18ymh52albtja2v96c-wpengine.netdna-ssl.com/wp-content/uploads/2020/06/image_2020-06-29_17-39-02.png" rel="nofollow noreferrer">example2</a>.
  However, it is not clear how to do it taking into account the Uniswap fee which is 0.3% for every trade.<br/><br/></p>
  <pre><code>library(tidyverse)
  ##### INITIAL PARAMETERS
  #Uniswap charges users a flat 0.30% fee for
  #every trade that takes place on the platform and 
  #automatically sends it to a liquidity reserve
  Uniswap.fee &lt;- 0.30 / 100
  
  ETH.initial.price &lt;- 100
  ETH.pool.price    &lt;- 100
  
  BNT.initial.price &lt;- 1
  BNT.pool.price    &lt;- 1
  
  ##### INITIAL SITUATION
  
  BNT.units &lt;- 1000
  BNT.total &lt;- BNT.units * BNT.initial.price
  
  ETH.units &lt;- BNT.total / ETH.initial.price
  ETH.total &lt;- ETH.units * ETH.initial.price
  
  Pool.Value &lt;- BNT.total + ETH.total
  Pool.DF &lt;- data.frame(Symbol = c(&quot;BNT&quot;, &quot;ETH&quot;), Share = c(BNT.total,ETH.total))
  ggplot(Pool.DF, aes(x = Symbol, y=Share, fill=Symbol)) +
    geom_bar(width = 1, position = &quot;dodge&quot;, stat=&quot;identity&quot;) + labs(title=&quot;Initial POOL&quot;)
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Be16L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Be16L.png" alt="enter image description here" /></a></p>
  <pre><code>##### FINAL SITUATION
  
  ETH.final.price   &lt;- 120
  ETH.pool.price    &lt;- 100
  BNT.final.price   &lt;- 1
  BNT.pool.price    &lt;- 1
  
  ##### Imbalanced.Pool
  Imbalanced.Pool &lt;- data.frame(Symbol = c(&quot;BNT&quot;, &quot;ETH&quot;), 
                                Share = c(BNT.total,ETH.units * ETH.final.price ))
  ggplot(Imbalanced.Pool, aes(x = Symbol, y=Share, fill=Symbol)) +
    geom_bar(width = 1, position = &quot;dodge&quot;, stat=&quot;identity&quot;)+ labs(title=&quot;Imbalanced POOL: ETH valorization&quot;)
  
  #### need to balance:
  </code></pre>
  <p><a href="https://i.stack.imgur.com/rRVgb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rRVgb.png" alt="enter image description here" /></a></p>
  <p>...
  Now I don't know how to continue the steps to correctly balance and obtain the impermanent loss and arb profit values.</p>
  <p>Thank you very much,</p>