Impermanent Loss Tracker
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © andrew.ryabchenko
//@version=4

study("Impermanent Loss")
unixTimeNow = time
timeAdded = input(defval = timestamp("January 1, 2022 00:00:00"), title = "Date and time the liquidity was added", type = input.time)
assetA = input(defval = "BINANCE:CAKEUSDT", title = "Asset A", type = input.symbol)
quanA = input(defval = 1.0, title = "Amount", type = input.float)
assetB = input(defval = "BINANCE:ETHUSDT", title = "Asset B", type = input.symbol)
quanB = input(defval = 1.0, title = "Amount", type = input.float)

offset = barssince(unixTimeNow <= timeAdded)

priceA = security(assetA, timeframe.period, close)
priceB = security(assetB, timeframe.period, close)

var k = quanA * quanB

rt = float(na)
deviationA = float(na)
deviationB = float(na)


//Initial Prices
var initialPriceA = if offset > 0
    priceA[1]

var initialPriceB = if offset > 0
    priceB[1]

var ri = if offset > 0 
    initialPriceA / initialPriceB

if offset > 0 
    rt := priceA / priceB
if offset > 0
    deviationA := ((quanA - sqrt(k / rt)) / quanA) * 100
    
if offset > 0 
    deviationB := ((quanB - sqrt(k * rt)) / quanB) * 100

plot(0, color = #00c96b)
plot(deviationA, title = "Percent difference of asset A", color = #00c96b, style = plot.style_histogram)
plot(deviationB, title = "Percent difference of asset B", color = #df3612, style = plot.style_histogram)







