Skip to content

Commit

Permalink
Look up fees from exchange and calc dynamically;
Browse files Browse the repository at this point in the history
  • Loading branch information
hammertoe committed Feb 25, 2019
1 parent b3ed42a commit b870f6a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
3 changes: 2 additions & 1 deletion crypto_balancer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def exchange_choices():
print()

rates = fetch_rates(exch, targets.keys())
balancer = SimpleBalancer(targets, args.valuebase,
fee = exch.fees['trading']['maker']
balancer = SimpleBalancer(targets, args.valuebase, fee,
threshold=float(config['threshold']))

base_values = balancer.calc_base_values(balances, rates)
Expand Down
8 changes: 4 additions & 4 deletions crypto_balancer/simple_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@


class SimpleBalancer():
def __init__(self, targets, base, rounds=6, threshold=1):
def __init__(self, targets, base, fee=0.001, rounds=6, attempts=10000, threshold=1):
self.targets = targets
self.base = base
self.fee = fee
self.rounds = rounds
self.attempts = 10000
self.attempts = attempts
self.threshold = threshold

def __call__(self, amounts, rates, force=False):
new_amounts = amounts.copy()
pairs_processed = set()
attempts = []
fee = 0.1

# exit if don't need to balance and not forcing
if not self.needs_balancing(new_amounts, rates) and not force:
Expand Down Expand Up @@ -103,7 +103,7 @@ def __call__(self, amounts, rates, force=False):
orders.append(order)
pairs_processed.add(trade_pair)
# keep track of the total fee of these orders
total_fee += trade_amount_base * (fee/100.0)
total_fee += trade_amount_base * self.fee

# Check the at the end we have no differences outstanding
# and that none of the new amounts have gone negative
Expand Down
30 changes: 30 additions & 0 deletions crypto_balancer/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,36 @@ def test_zero_balance(self):
expected = []
self.assertEqual(res['orders'], expected)

def test_fees1(self):
targets = {'XRP': 50,
'XLM': 40,
'USDT': 10, }
current = {'XRP': 0,
'XLM': 0,
'USDT': 1000, }
base = "USDT"
rates = {'XRP/USDT': 1.0,
'XLM/USDT': 1.0,
}
balancer = SimpleBalancer(targets, base)
res = balancer(current, rates)
self.assertEqual(res['total_fee'], 0.9)

def test_fees2(self):
targets = {'XRP': 50,
'XLM': 40,
'USDT': 10, }
current = {'XRP': 0,
'XLM': 0,
'USDT': 1000, }
base = "USDT"
rates = {'XRP/USDT': 1.0,
'XLM/USDT': 1.0,
}
balancer = SimpleBalancer(targets, base, fee=0.005)
res = balancer(current, rates)
self.assertEqual(res['total_fee'], 4.5)


if __name__ == '__main__': # pragma: no cover
unittest.main()

0 comments on commit b870f6a

Please sign in to comment.