【问题标题】:Calculate best compounding period (cryptocurrency)计算最佳复利期(加密货币)
【发布时间】:2021-03-03 12:41:26
【问题描述】:

我有一个有趣的问题,我不确定将其放在此处还是堆栈数学中的最佳位置,但由于我尝试以编程方式解决它(在 R 中,但任何编程语言都可以)也许 StackOverflow 是最好的地方。问题涉及复合加密货币。 问题如下: 您在流动性池中质押“K”数量的代币以获得利息。 流动性池为您的代币提供“年利率”(APR),即年利率,APR 不复利。 您可以随时复利,但每次都需要支付少量费用。

我最初尝试使用一些 for 循环来解决它,如果假设用户每“D”天复利一次,则估计最终回报。 当前示例使用 CAKE 和 BNB 令牌。一些简化解决方案的假设。假设 APR、bnb_value 和 cake_value 是 FIXED 值(它们在现实中并不存在)。

APR=1.349  ## the interest value showed in the pool, divided by 100
APR_day=APR/365.0 ## the daily APR
bnb_value=210 ## current value of the BNB token, in euro or dollar or any FIAT
fee_bnb=0.002*bnb_value ## current fee in euro or dollar or any FIAT
initial_cakes=10
cake_value=10

adf=data.frame(col1=NULL, col2=NULL, col3=NULL, col4=NULL)
## we generate a sequence containing fractional days up to the third day
## and then full days starting from the fourth day
comp_intervals=c(seq(0.1, 3, 0.1), 4:30)
for(j in comp_intervals){
  acquired_int=0  ## acquired interest in "j" days
  current_val=initial_cakes  ## current value of the capital
  all_fees=0  ## fees paid for the transactions
  aseq=seq(j, 365, j)  ## list of the compounding events in days
  
  # apr_seq is APR for the "j" period. If "j" is 1 it's the daily APR,
  # but for longer compounding intervals the APR for the "j" period
  # become larger
  apr_seq=APR_day*j
  for(i in aseq){
    acquired_int=current_val*apr_seq
    current_val=current_val+acquired_int
    all_fees=all_fees+fee_bnb
    acquired_int=0
  }
  
  ## we add the interest for the remaining days of the year, if any, to current_val
  acquired_int=(365-max(aseq))*APR_day*current_val
  current_val=current_val+acquired_int
  
  final_gain=round(current_val*cake_value - all_fees, 2)
  # msg=paste0("Final gain in Euro minus fees: ", final_gain)
  # print(msg)
  apy_i_day= round(final_gain/(initial_cakes*cake_value), 5)
  # msg=paste0("apy compounding every ", j, " days is: ", apy_i_day)
  # print(msg)
  # cat("\n")
  adf=rbind(adf, data.frame(fiat_value=final_gain,
      APY_val=100*apy_i_day, compounding_days=j, cakes=current_val))
  
}
# finally we show, who, among the various compounding, had the hiest yield
adf[adf$APY_val==max(adf$APY_val), ]

问题在于,您刚刚看到的代码并没有真正告诉您什么是复利的最佳时期。如果用户每“D”天复合一次,它会告诉更多的收益。它接近真正的解决方案,但事实并非如此! 您可以直观地理解延长时间是错误的。您从少量资本开始,因此为了获得良好的回报,您“很少”因为费用而复利,但是时间越长,您的资本增长就越多。您的资本增长越频繁,您就应该复利。

然后我尝试了不同的方法。

给出“年收益率”的一般公式如下:

APY = (1 + APR/N)^N -1

如果您还考虑费用和您拥有的初始资本:

Final_capital=Initial_capital*APY - single_fee*N

其中 APR 是百分比,N 是复合事件的数量(在这个公式中,它们在时间上是均匀分布的)。

通过“dN”对 Final_capital 进行微分并找到等式的零点,您可以获得最佳数量的复合事件。如果您将 365 除以复合事件的最佳数量,您应该在复合代币的天数后获得。 我从微分公式获得的结果与第一个解决方案不同,我不知道为什么。我也认为,但我不确定,后一种解决方案与前一种解决方案有相同的限制。

library(utils)
### interest APY formula minus fees
final_v=function(x, APR, fee_bnb, initial_value){
  return(  initial_value*( (1+APR/x)^x - 1 ) -fee_bnb*x   ) 
}

## differential respect to X of the interest APY formula minus the fees
a_diff_comp=function(x, APR, fee_bnb, initial_value){
  return( (initial_value*( (APR/x + 1)^x )*(  log(APR/x + 1) - APR/(  ((1/x)+1)*x  ) ) ) - fee_bnb )
}

x=3:40
y=sapply(x, a_diff_comp, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
plot(x,y)

y2=sapply(x, final_v, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
plot(x,y2)

xmin <- uniroot(a_diff_comp, c(1, 100), tol = 0.000001, APR=APR, fee_bnb=fee_bnb, initial_value=(initial_cakes*cake_value))
xmin$root

那么,如何正确计算最佳复利区间?

【问题讨论】:

    标签: r math optimization logic cryptocurrency


    【解决方案1】:

    我在GitHub页面https://github.com/amendez/cakecalc/issues/2#issuecomment-806364370发表评论。

    但我想我现在明白你的意思了。我同意你的观点,甚至我的亲身经历也证明了你的观点。 cakeCalc 为您提供最佳复合比的方式存在一些问题。在计算最终结果时,它认为你的余额在每次复利后都是固定的,这似乎是有问题的。因此,即使您完全按照 cakeCalc 建议的说明操作,每次制作蛋糕时,cakeCalc 都会为您提供不同的结果,因为您的余额现已更新

    我从来没有尝试写下方程式来推导出最佳复利区间,但我确信最好的复利区间不会固定。例如,您可能希望找到类似于以下答案的最佳结果:

    第 20 天复合,第 38 天复合,第 53 天复合,...

    因此,找到数字 N 作为复数不会给你最好的答案。 解决问题的一种方法可能是:考虑您在一年内找到最佳复合率。并假设您想在此期间复合“m”次。所以现在,你有“m”个变量和一个你希望最大化的表达式。我现在不确定,但也许你可以使用简单的凸优化方法来解决它(只需要在之前检查方程的凸性,因为变量之间的约束都是仿射的)。然后对不同数量的“m”尝试相同的操作,并找到其中的最大数量。 这个解决方案是实用的,因为数字“m”是有界的(显然它是一个整数!),并且您可能期望找到“m”与 cakeCalc 方法的结果如此接近。我猜答案不会有太大的不同。因此,您可以更改“m”,例如少于 10 次并找到最佳解决方案。

    【讨论】:

      【解决方案2】:

      让我们以秒为最小的时间单位。

      假设APR是我们的APR,我们的初始投资是I$,我们的复合区间是s秒。

      如果我们质押我们的I并在s秒后收获,那么我们的原始投资加上利润减去费用F

      I_2 = I + I * APR / 3155695200 * s - F

      再投资,

      I_3 = I_2 + I_2 * APR / 3155695200 * s - F

      等等……

      您可以轻松编写一个程序来针对各种s 值运行此程序,以确定最佳复合间隔是多少,例如使用分而治之的方法。

      【讨论】:

        猜你喜欢
        • 2019-03-09
        • 2018-11-12
        • 2022-06-23
        • 2016-08-25
        • 2021-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        相关资源
        最近更新 更多