repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand)
return repartition_in_base_currency
-def give_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
+def compute_moves(current_assets, repartition_pertenthousand, market, 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()])
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)
- print("assets before repartition:")
- for currency, value in current_assets.items():
- print("holding {} {}".format(
- formatted_price(value),
- currency))
- print("------------")
- for currency, value in mouvements.items():
- if currency == base_currency:
- continue
- asset_ticker = get_ticker(currency, base_currency, market)
+ return mouvements
+
+def compute_order(currency, value, market, base_currency="BTC"):
+ if currency == base_currency or value == 0:
+ return [None, 0, False]
+
+ asset_ticker = get_ticker(currency, base_currency, market)
+ if asset_ticker["inverted"]:
+ asset_ticker = get_ticker(base_currency, currency, market)
+ if value > 0:
+ rate = asset_ticker["askA"]
+ return ["buy", rate, True]
+ else:
+ rate = asset_ticker["bidA"]
+ return ["sell", rate, True]
+ else:
if value > 0:
- action = "sell"
- currency_price = int(value / asset_ticker["bid"])
+ rate = asset_ticker["bidA"]
+ return ["sell", rate, False]
else:
- action = "buy"
- currency_price = int(value / asset_ticker["ask"])
- if value != 0:
+ rate = asset_ticker["askA"]
+ return ["buy", rate, False]
+
+def make_order(currency, value, market, base_currency="BTC"):
+ action, rate, inverted = compute_order(currency, value, market, base_currency=base_currency)
+ amount = formatted_price(abs(value))
+ if not inverted:
+ symbol = "{}/{}".format(currency, base_currency)
+ else:
+ symbol = "{}/{}".format(base_currency, currency)
+ return market.create_order(symbol, 'limit', action, amount, price=rate)
+
+def make_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
+ mouvements = compute_moves(
+ current_assets,
+ repartition_pertenthousand,
+ market,
+ base_currency=base_currency)
+
+ results = []
+ for currency, value in sorted(mouvements.items(), key=lambda x: x[1]):
+ # FIXME: wait for sales to finish
+ results.append(make_order(currency, value, market, base_currency=base_currency))
+ return results
+
+def print_assets(assets, indent="", market=None, base_currency="BTC"):
+ if market is not None:
+ format_string = "{}{} {} ({} {})"
+ else:
+ format_string = "{}{} {}"
+ base_currency_price = 0
+
+ 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
+ print(format_string.format(
+ indent,
+ formatted_price(value),
+ currency,
+ formatted_price(base_currency_price),
+ base_currency))
+
+def print_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
+ mouvements = compute_moves(
+ current_assets,
+ repartition_pertenthousand,
+ market,
+ base_currency=base_currency)
+
+ for currency, value in mouvements.items():
+ action, rate, inverted = compute_order(
+ currency,
+ value,
+ market,
+ base_currency=base_currency)
+ if action is not None:
+ currency_price = int(value / rate)
+
+ if not inverted:
+ c1, c2 = [base_currency, currency]
+ v1, v2 = [value, currency_price]
+ else:
+ c1, c2 = [currency, base_currency]
+ v1, v2 = [currency_price, value]
+
print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
action,
- formatted_price(abs(value)),
- base_currency,
- currency,
- formatted_price(abs(currency_price)),
- currency))
- print("------------\nassets after repartition:")
- for currency, value in new_repartition.items():
- print("holding {} {}".format(formatted_price(value), currency))
+ formatted_price(abs(v1)), c1,
+ c2,
+ formatted_price(abs(v2)), c2))
current_assets = fetch_balances(market)
-give_orders(current_assets, repartition_pertenthousand, market)
+print_orders(current_assets, repartition_pertenthousand, market)