aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-11 02:48:38 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-01-11 02:48:38 +0100
commit269ec87c7f2a9b9b8fd6a707881c5477680abf31 (patch)
treea8e10f5939435d08a77d3e39bf9118e5d663f1d7
downloadTrader-269ec87c7f2a9b9b8fd6a707881c5477680abf31.tar.gz
Trader-269ec87c7f2a9b9b8fd6a707881c5477680abf31.tar.zst
Trader-269ec87c7f2a9b9b8fd6a707881c5477680abf31.zip
First script skeleton
-rw-r--r--script.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/script.py b/script.py
new file mode 100644
index 0000000..4732533
--- /dev/null
+++ b/script.py
@@ -0,0 +1,94 @@
1import ccxt
2
3max_digits = 18
4current_assets = {
5 "ETH": int(2.00000000 * 10**max_digits),
6 "BTC": int(1.23000000 * 10**max_digits),
7 "ZEC": int(2.00000000 * 10**max_digits),
8 "SC" : int(2.000000000 * 10**max_digits),
9 }
10
11repartition_pertenthousand = {
12 "BTC": 2857,
13 "ZEC": 3701,
14 "DOGE": 1805,
15 "DGB": 1015,
16 "SC": 623,
17 }
18
19market = ccxt.poloniex()
20
21def formatted_price(value):
22 return round(value / 10**max_digits, 8)
23
24def get_ticker(c1, c2, market):
25 try:
26 return market.fetch_ticker("{}/{}".format(c1, c2))
27 except ccxt.ExchangeError:
28 try:
29 ticker = market.fetch_ticker("{}/{}".format(c2, c1))
30 return {
31 "bid": float(1/ticker["ask"]),
32 "ask": float(1/ticker["bid"]),
33 }
34 except ccxt.ExchangeError:
35 return None
36
37def assets_value(assets, market, base_currency="BTC"):
38 repartition_in_base_currency = {}
39 for currency, asset_value in assets.items():
40 if currency == base_currency:
41 repartition_in_base_currency[currency] = asset_value
42 else:
43 asset_ticker = get_ticker(currency, base_currency, market)
44 if asset_ticker is None:
45 raise Exception("This asset is not available in the chosen market")
46 repartition_in_base_currency[currency] = int(asset_ticker["bid"] * asset_value)
47 return repartition_in_base_currency
48
49def dispatch_assets(base_currency_value, repartition_pertenthousand, market, base_currency="BTC"):
50 sum_pertenthousand = sum([v for k, v in repartition_pertenthousand.items()])
51 repartition_in_base_currency = {}
52 for currency, ptt in repartition_pertenthousand.items():
53 repartition_in_base_currency[currency] = int(ptt * base_currency_value / sum_pertenthousand)
54 return repartition_in_base_currency
55
56def give_orders(current_assets, repartition_pertenthousand, market, base_currency="BTC"):
57 value_in_base = assets_value(current_assets, market, base_currency=base_currency)
58 total_base_value = sum([ v for k, v in value_in_base.items()])
59
60 new_repartition = dispatch_assets(total_base_value, repartition_pertenthousand, market, base_currency=base_currency)
61 mouvements = {}
62
63 for key in set(value_in_base.keys()).union(set(new_repartition.keys())):
64 mouvements[key] = value_in_base.get(key, 0) - new_repartition.get(key, 0)
65
66 print("assets before repartition:")
67 for currency, value in current_assets.items():
68 print("holding {} {}".format(
69 formatted_price(value),
70 currency))
71 print("------------")
72 for currency, value in mouvements.items():
73 if currency == base_currency:
74 continue
75 asset_ticker = get_ticker(currency, base_currency, market)
76 if value > 0:
77 action = "sell"
78 currency_price = int(value / asset_ticker["bid"])
79 else:
80 action = "buy"
81 currency_price = int(value / asset_ticker["ask"])
82 if value != 0:
83 print("need to {} {} {}'s worth of {}, i.e. {} {}".format(
84 action,
85 formatted_price(abs(value)),
86 base_currency,
87 currency,
88 formatted_price(abs(currency_price)),
89 currency))
90 print("------------\nassets after repartition:")
91 for currency, value in new_repartition.items():
92 print("holding {} {}".format(formatted_price(value), currency))
93
94give_orders(current_assets, repartition_pertenthousand, market)