From b2dab3acfb8cb23bdb54298c88aecfb7042fb8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 14 Jan 2018 03:34:42 +0100 Subject: Include fees in computation --- script.py | 53 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/script.py b/script.py index c5ac4aa..187ff73 100644 --- a/script.py +++ b/script.py @@ -31,13 +31,25 @@ def get_ticker(c1, c2, market): "ask": float(1/ticker["bid"]), "bidA": float(1/ticker["askA"]), "askA": float(1/ticker["bidA"]), + "bidE": float(1/ticker["askE"]), + "askE": float(1/ticker["bidE"]), } def augment_ticker(ticker): + bid_factor = 1.01 + ask_factor = 0.99 + fees = fetch_fees(market) # FIXME: need to do better than just a multiplier ticker.update({ "inverted": False, - "bidA": ticker["bid"] * 0.99, - "askA": ticker["ask"] * 1.01, + # Adjusted + "bidA": ticker["bid"] * bid_factor, + "askA": ticker["ask"] * ask_factor, + # Expected in the end + "bidE": ticker["bid"] * bid_factor * (1 - fees["maker"]), + "askE": ticker["ask"] * ask_factor * (1 - fees["maker"]), + # fees + "bidF": ticker["bid"] * bid_factor * fees["maker"], + "askF": ticker["ask"] * ask_factor * fees["maker"], }) if (c1, c2, market.__class__) in get_ticker.cache: @@ -64,16 +76,26 @@ def fetch_balances(market): balances[key] = int(value * 10**max_digits) return balances +@static_var("cache", {}) +def fetch_fees(market): + if market.__class__ not in fetch_fees.cache: + fetch_fees.cache[market.__class__] = market.fetch_fees() + return fetch_fees.cache[market.__class__] + def assets_value(assets, market, base_currency="BTC"): repartition_in_base_currency = {} for currency, asset_value in assets.items(): if currency == base_currency: - repartition_in_base_currency[currency] = asset_value + repartition_in_base_currency[currency] = [asset_value, 0] else: asset_ticker = get_ticker(currency, base_currency, market) if asset_ticker is None: raise Exception("This asset is not available in the chosen market") - repartition_in_base_currency[currency] = int(asset_ticker["bidA"] * asset_value) + repartition_in_base_currency[currency] = [ + int(asset_ticker["bidE"] * asset_value), + int(asset_ticker["bidF"] * asset_value) + ] + return repartition_in_base_currency def dispatch_assets(base_currency_value, repartition_pertenthousand, market, base_currency="BTC"): @@ -83,15 +105,20 @@ def dispatch_assets(base_currency_value, repartition_pertenthousand, market, bas repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand) return repartition_in_base_currency -def compute_moves(current_assets, repartition_pertenthousand, market, base_currency="BTC"): +def compute_moves(current_assets, repartition_pertenthousand, market, no_fees=True, base_currency="BTC"): value_in_base = assets_value(current_assets, market, base_currency=base_currency) - total_base_value = sum([ v for k, v in value_in_base.items()]) + total_base_value = sum([ v[0] for k, v in value_in_base.items()]) new_repartition = dispatch_assets(total_base_value, repartition_pertenthousand, market, base_currency=base_currency) mouvements = {} - for key in set(value_in_base.keys()).union(set(new_repartition.keys())): - mouvements[key] = value_in_base.get(key, 0) - new_repartition.get(key, 0) + if no_fees: + for key in set(value_in_base.keys()).union(set(new_repartition.keys())): + mouvements[key] = value_in_base.get(key, [0, 0])[0] - new_repartition.get(key, 0) + else: + for key in set(value_in_base.keys()).union(set(new_repartition.keys())): + value, fee = value_in_base.get(key, [0, 0]) + mouvements[key] = [value - new_repartition.get(key, 0), fee] return mouvements @@ -148,7 +175,7 @@ def print_assets(assets, indent="", market=None, base_currency="BTC"): for currency, value in assets.items(): if market is not None: asset_ticker = get_ticker(currency, base_currency, market) - base_currency_price = asset_ticker["bidA"] * value + base_currency_price = asset_ticker["bidE"] * value print(format_string.format( indent, formatted_price(value), @@ -161,9 +188,10 @@ def print_orders(current_assets, repartition_pertenthousand, market, base_curren current_assets, repartition_pertenthousand, market, + no_fees=False, base_currency=base_currency) - for currency, value in mouvements.items(): + for currency, [value, fee] in mouvements.items(): action, rate, inverted = compute_order( currency, value, @@ -179,11 +207,12 @@ def print_orders(current_assets, repartition_pertenthousand, market, base_curren c1, c2 = [currency, base_currency] v1, v2 = [currency_price, value] - print("need to {} {} {}'s worth of {}, i.e. {} {}".format( + print("need to {} {} {}'s worth of {}, i.e. {} {} ( + {} {} fee)".format( action, formatted_price(abs(v1)), c1, c2, - formatted_price(abs(v2)), c2)) + formatted_price(abs(v2)), c2, + formatted_price(fee), c2)) current_assets = fetch_balances(market) print_orders(current_assets, repartition_pertenthousand, market) -- cgit v1.2.3