Premium Rolling APY Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fuyeiwk

//@version=4
study("Premium Rolling APY Calculator", precision=2, format=format.percent)
//study("FTX BTC Premium Rolling APY")
resolution = input(title="Resolution", type=input.resolution, defval="1440")
apy = input(title="Show numbers in annual", type=input.bool, defval=true)

base_symbol = input(title="Base Symbol"  , type=input.symbol, defval="FTX:BTCUSD")
future_1    = input(title="", type=input.symbol, defval="FTX:BTC0624", group="Future Symbol 1", inline="1")
future_2    = input(title="", type=input.symbol, defval="FTX:BTC0930", group="Future Symbol 2", inline="2")

p1 = security(future_1 , resolution, close)
p2 = security(future_2 , resolution, close)
spot = security(base_symbol, resolution, close)

t1 = time
t2 = input(title="Expire at", type=input.time, defval=timestamp("24 Jun 2022 16:00 +0800"), group="Future Symbol 1", inline="1")  // expire date time of q3 future
t3 = input(title="Expire at", type=input.time, defval=timestamp("30 Sep 2022 16:00 +0800"), group="Future Symbol 2", inline="2") // expire date time of q4 future

premium_1 = ((p1 - spot)/spot) 
premium_2 = ((p2 - spot)/spot) 

daysBetween(t1,t2) => float((t2-t1)/(1000*60*60*24))

d1 = daysBetween(t1, t2)
d2 = daysBetween(t1, t3)

rolling_1 = premium_1*(apy ? 365/d1 : 1)*100
rolling_2 = premium_2*(apy ? 365/d2 : 1)*100
plot(rolling_1, title="Q3 0924")
plot(rolling_2, title="Q4 1231", color=#ff3399)
hline(5, title="5% Line", color=#ffaa99, linestyle=hline.style_dashed)

