Description of the system
I have a system where every interval of time (lets assume every day) a reward (n) is paid out.The amount n paid out every day/interval is constant. (i.e n=20$)
I have the option of claiming the reward, which has a fee for every withdrawal. (i.e fee=0.40$)
Once claimed the amount n can be reinvested and gain a yield y per interval. (i.e y=0.2/365)
Problem
Obviously claiming the reward too often will diminish the total yield. But also never claiming the reward will result in missed opportunity since reinvesting would give a yield 1+y. I am looking for the optimal number of intervals between each rewards claim and reinvestment.
Possible solution
I wrote a piece of code in python that supposed to solve this problem, but when trying to come up with a mathematical formula to validate the code I was stuck. In short, the function runs a simlation for d days, does the claim+reinvest procedure every restake_interval days using a modulo operator, and returns the amount accumulated at the end of d days.
def restaking(restake_interval, n, y, fee, d):
# restake_interval: interval between each reinvstment
# n reward amount on each interval
# y interval yeild %
# d days/intervals to run the simulation for
available = 0 # amount available for withdrawal in the system
amount_staked = 0 # amount already invested that's gaining yield
for i in range(d):
available += n
if (i+1)%restake_interval == 0:
# claim and reinvest
amount_staked += available-fee
available = 0
amount_staked *= 1+y
return amount_staked+available
By running this function in a loop like this I was able to find the optimal restaking interval. But I am unsure of the result.
# find best restake day interval
n=20
interval = 1
prev_result = -1
while True:
result = restaking(interval, n, 0.2/365, 0.4, 365)
if result < prev_result:
break
interval += 1
prev_result = result
print("\nSOLUTON: restake every {} days: {:.2f}$ gain at {}days".format(interval-1, prev_result, 365))
You can run this code online at https://www.tutorialspoint.com/execute_python3_online.php
Question
What is the mathematical formula/model for solving this problem? Is the logic behind the code correct?