DCAv2Earn/libraries/balance_accounts.py

22 lines
873 B
Python

def balance_accounts(spot_balance, earn_balance, lower_limit, step_size, percentage):
target_spot_balance = (spot_balance + earn_balance) * 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]