45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
def balance_accounts(spot_balance, earn_balance, lower_limit, step_size, percentage):
|
|
'''
|
|
args:
|
|
spot_balance: float
|
|
Current spot balance
|
|
earn_balance: float
|
|
Current earn balance
|
|
lower_limit: float
|
|
Lower limit for spot balance
|
|
step_size: float
|
|
Step size for balance adjustment
|
|
percentage: float
|
|
Percentage of funds to be allocated to the earn account
|
|
|
|
returns:
|
|
spot_balance: float
|
|
Updated spot balance
|
|
earn_balance: float
|
|
Updated earn balance
|
|
'''
|
|
|
|
spot_balance+=earn_balance
|
|
earn_balance=0
|
|
|
|
target_spot_balance = spot_balance * (1-percentage)
|
|
|
|
while abs(spot_balance - target_spot_balance) >= step_size:
|
|
if spot_balance < target_spot_balance:
|
|
spot_balance += step_size
|
|
earn_balance -= step_size
|
|
elif spot_balance > target_spot_balance:
|
|
spot_balance -= step_size
|
|
earn_balance += step_size
|
|
|
|
# Ensure spot_balance is above lower_limit
|
|
while spot_balance < lower_limit and earn_balance >= step_size:
|
|
spot_balance += step_size
|
|
earn_balance -= step_size
|
|
|
|
# If earn_balance is not enough, transfer the remaining amount
|
|
if spot_balance < lower_limit:
|
|
spot_balance += earn_balance
|
|
earn_balance = 0
|
|
|
|
return [spot_balance, earn_balance] |