]>
Commit | Line | Data |
---|---|---|
dd359bc0 IB |
1 | import ccxt |
2 | import time | |
5ab23e1c | 3 | from decimal import Decimal as D |
dd359bc0 IB |
4 | # Put your poloniex api key in market.py |
5 | from market import market | |
6 | ||
7 | # FIXME: Améliorer le bid/ask | |
8 | # FIXME: J'essayais d'utiliser plus de bitcoins que j'en avais à disposition | |
9 | # FIXME: better compute moves to avoid rounding errors | |
10 | ||
dd359bc0 IB |
11 | class Portfolio: |
12 | URL = "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json" | |
13 | liquidities = {} | |
14 | data = None | |
15 | ||
16 | @classmethod | |
17 | def repartition_pertenthousand(cls, liquidity="medium"): | |
18 | cls.parse_cryptoportfolio() | |
19 | liquidities = cls.liquidities[liquidity] | |
20 | last_date = sorted(liquidities.keys())[-1] | |
21 | return liquidities[last_date] | |
22 | ||
23 | @classmethod | |
24 | def get_cryptoportfolio(cls): | |
25 | import json | |
26 | import urllib3 | |
27 | urllib3.disable_warnings() | |
28 | http = urllib3.PoolManager() | |
29 | ||
183a53e3 IB |
30 | try: |
31 | r = http.request("GET", cls.URL) | |
32 | except Exception: | |
33 | return | |
34 | try: | |
5ab23e1c IB |
35 | cls.data = json.loads(r.data, |
36 | parse_int=D, | |
37 | parse_float=D) | |
183a53e3 IB |
38 | except json.JSONDecodeError: |
39 | cls.data = None | |
dd359bc0 IB |
40 | |
41 | @classmethod | |
42 | def parse_cryptoportfolio(cls): | |
43 | if cls.data is None: | |
44 | cls.get_cryptoportfolio() | |
45 | ||
46 | def filter_weights(weight_hash): | |
47 | if weight_hash[1] == 0: | |
48 | return False | |
49 | if weight_hash[0] == "_row": | |
50 | return False | |
51 | return True | |
52 | ||
53 | def clean_weights(i): | |
54 | def clean_weights_(h): | |
55 | if type(h[1][i]) == str: | |
56 | return [h[0], h[1][i]] | |
57 | else: | |
58 | return [h[0], int(h[1][i] * 10000)] | |
59 | return clean_weights_ | |
60 | ||
61 | def parse_weights(portfolio_hash): | |
62 | # FIXME: we'll need shorts at some point | |
63 | assert all(map(lambda x: x == "long", portfolio_hash["holding"]["direction"])) | |
64 | weights_hash = portfolio_hash["weights"] | |
65 | weights = {} | |
66 | for i in range(len(weights_hash["_row"])): | |
67 | weights[weights_hash["_row"][i]] = dict(filter( | |
68 | filter_weights, | |
69 | map(clean_weights(i), weights_hash.items()))) | |
70 | return weights | |
71 | ||
72 | high_liquidity = parse_weights(cls.data["portfolio_1"]) | |
73 | medium_liquidity = parse_weights(cls.data["portfolio_2"]) | |
74 | ||
75 | cls.liquidities = { | |
76 | "medium": medium_liquidity, | |
77 | "high": high_liquidity, | |
78 | } | |
79 | ||
80 | class Amount: | |
81 | MAX_DIGITS = 18 | |
82 | ||
c2644ba8 | 83 | def __init__(self, currency, value, linked_to=None, ticker=None, rate=None): |
dd359bc0 | 84 | self.currency = currency |
5ab23e1c | 85 | self.value = D(value) |
dd359bc0 IB |
86 | self.linked_to = linked_to |
87 | self.ticker = ticker | |
c2644ba8 | 88 | self.rate = rate |
dd359bc0 IB |
89 | |
90 | self.ticker_cache = {} | |
91 | self.ticker_cache_timestamp = time.time() | |
92 | ||
c2644ba8 | 93 | def in_currency(self, other_currency, market, rate=None, action=None, compute_value="average"): |
dd359bc0 IB |
94 | if other_currency == self.currency: |
95 | return self | |
c2644ba8 IB |
96 | if rate is not None: |
97 | return Amount( | |
98 | other_currency, | |
99 | self.value * rate, | |
100 | linked_to=self, | |
101 | rate=rate) | |
cfab619d | 102 | asset_ticker = Trade.get_ticker(self.currency, other_currency, market) |
dd359bc0 | 103 | if asset_ticker is not None: |
c2644ba8 | 104 | rate = Trade.compute_value(asset_ticker, action, compute_value=compute_value) |
dd359bc0 IB |
105 | return Amount( |
106 | other_currency, | |
c2644ba8 | 107 | self.value * rate, |
dd359bc0 | 108 | linked_to=self, |
c2644ba8 IB |
109 | ticker=asset_ticker, |
110 | rate=rate) | |
dd359bc0 IB |
111 | else: |
112 | raise Exception("This asset is not available in the chosen market") | |
113 | ||
dd359bc0 | 114 | def __abs__(self): |
5ab23e1c | 115 | return Amount(self.currency, abs(self.value)) |
dd359bc0 IB |
116 | |
117 | def __add__(self, other): | |
5ab23e1c | 118 | if other.currency != self.currency and other.value * self.value != 0: |
dd359bc0 | 119 | raise Exception("Summing amounts must be done with same currencies") |
5ab23e1c | 120 | return Amount(self.currency, self.value + other.value) |
dd359bc0 IB |
121 | |
122 | def __radd__(self, other): | |
123 | if other == 0: | |
124 | return self | |
125 | else: | |
126 | return self.__add__(other) | |
127 | ||
128 | def __sub__(self, other): | |
5ab23e1c | 129 | if other.currency != self.currency and other.value * self.value != 0: |
dd359bc0 | 130 | raise Exception("Summing amounts must be done with same currencies") |
5ab23e1c | 131 | return Amount(self.currency, self.value - other.value) |
dd359bc0 IB |
132 | |
133 | def __mul__(self, value): | |
5ab23e1c | 134 | if type(value) != int and type(value) != float and type(value) != D: |
dd359bc0 | 135 | raise TypeError("Amount may only be multiplied by numbers") |
5ab23e1c | 136 | return Amount(self.currency, self.value * value) |
dd359bc0 IB |
137 | |
138 | def __rmul__(self, value): | |
139 | return self.__mul__(value) | |
140 | ||
141 | def __floordiv__(self, value): | |
5ab23e1c | 142 | if type(value) != int and type(value) != float and type(value) != D: |
dd359bc0 | 143 | raise TypeError("Amount may only be multiplied by integers") |
5ab23e1c | 144 | return Amount(self.currency, self.value / value) |
dd359bc0 IB |
145 | |
146 | def __truediv__(self, value): | |
147 | return self.__floordiv__(value) | |
148 | ||
149 | def __lt__(self, other): | |
150 | if self.currency != other.currency: | |
151 | raise Exception("Comparing amounts must be done with same currencies") | |
5ab23e1c | 152 | return self.value < other.value |
dd359bc0 IB |
153 | |
154 | def __eq__(self, other): | |
155 | if other == 0: | |
5ab23e1c | 156 | return self.value == 0 |
dd359bc0 IB |
157 | if self.currency != other.currency: |
158 | raise Exception("Comparing amounts must be done with same currencies") | |
5ab23e1c | 159 | return self.value == other.value |
dd359bc0 IB |
160 | |
161 | def __str__(self): | |
162 | if self.linked_to is None: | |
163 | return "{:.8f} {}".format(self.value, self.currency) | |
164 | else: | |
165 | return "{:.8f} {} [{}]".format(self.value, self.currency, self.linked_to) | |
166 | ||
167 | def __repr__(self): | |
168 | if self.linked_to is None: | |
169 | return "Amount({:.8f} {})".format(self.value, self.currency) | |
170 | else: | |
171 | return "Amount({:.8f} {} -> {})".format(self.value, self.currency, repr(self.linked_to)) | |
172 | ||
173 | class Balance: | |
174 | known_balances = {} | |
dd359bc0 IB |
175 | |
176 | def __init__(self, currency, total_value, free_value, used_value): | |
177 | self.currency = currency | |
178 | self.total = Amount(currency, total_value) | |
179 | self.free = Amount(currency, free_value) | |
180 | self.used = Amount(currency, used_value) | |
181 | ||
f2da6589 IB |
182 | @classmethod |
183 | def from_hash(cls, currency, hash_): | |
184 | return cls(currency, hash_["total"], hash_["free"], hash_["used"]) | |
185 | ||
dd359bc0 | 186 | @classmethod |
deb8924c | 187 | def in_currency(cls, other_currency, market, compute_value="average", type="total"): |
dd359bc0 IB |
188 | amounts = {} |
189 | for currency in cls.known_balances: | |
190 | balance = cls.known_balances[currency] | |
191 | other_currency_amount = getattr(balance, type)\ | |
deb8924c | 192 | .in_currency(other_currency, market, compute_value=compute_value) |
dd359bc0 IB |
193 | amounts[currency] = other_currency_amount |
194 | return amounts | |
195 | ||
196 | @classmethod | |
197 | def currencies(cls): | |
198 | return cls.known_balances.keys() | |
199 | ||
dd359bc0 IB |
200 | @classmethod |
201 | def _fill_balances(cls, hash_): | |
202 | for key in hash_: | |
203 | if key in ["info", "free", "used", "total"]: | |
204 | continue | |
205 | if hash_[key]["total"] > 0: | |
206 | cls.known_balances[key] = cls.from_hash(key, hash_[key]) | |
207 | ||
208 | @classmethod | |
209 | def fetch_balances(cls, market): | |
210 | cls._fill_balances(market.fetch_balance()) | |
211 | return cls.known_balances | |
212 | ||
213 | @classmethod | |
214 | def dispatch_assets(cls, amount): | |
215 | repartition_pertenthousand = Portfolio.repartition_pertenthousand() | |
216 | sum_pertenthousand = sum([v for k, v in repartition_pertenthousand.items()]) | |
217 | amounts = {} | |
218 | for currency, ptt in repartition_pertenthousand.items(): | |
219 | amounts[currency] = ptt * amount / sum_pertenthousand | |
220 | if currency not in cls.known_balances: | |
221 | cls.known_balances[currency] = cls(currency, 0, 0, 0) | |
222 | return amounts | |
223 | ||
224 | @classmethod | |
deb8924c | 225 | def prepare_trades(cls, market, base_currency="BTC", compute_value=None): |
dd359bc0 IB |
226 | cls.fetch_balances(market) |
227 | values_in_base = cls.in_currency(base_currency, market) | |
228 | total_base_value = sum(values_in_base.values()) | |
229 | new_repartition = cls.dispatch_assets(total_base_value) | |
deb8924c IB |
230 | # Recompute it in case we have new currencies |
231 | values_in_base = cls.in_currency(base_currency, market) | |
232 | Trade.compute_trades(values_in_base, new_repartition, market=market, compute_value=compute_value) | |
dd359bc0 | 233 | |
dd359bc0 IB |
234 | def __repr__(self): |
235 | return "Balance({} [{}/{}/{}])".format(self.currency, str(self.free), str(self.used), str(self.total)) | |
236 | ||
deb8924c | 237 | class Computation: |
deb8924c IB |
238 | computations = { |
239 | "default": lambda x, y: x[y], | |
deb8924c IB |
240 | "average": lambda x, y: x["average"], |
241 | "bid": lambda x, y: x["bid"], | |
242 | "ask": lambda x, y: x["ask"], | |
243 | } | |
244 | ||
245 | ||
dd359bc0 IB |
246 | class Trade: |
247 | trades = {} | |
248 | ||
249 | def __init__(self, value_from, value_to, currency, market=None): | |
250 | # We have value_from of currency, and want to finish with value_to of | |
251 | # that currency. value_* may not be in currency's terms | |
252 | self.currency = currency | |
253 | self.value_from = value_from | |
254 | self.value_to = value_to | |
255 | self.orders = [] | |
089d5d9d | 256 | self.market = market |
dd359bc0 IB |
257 | assert self.value_from.currency == self.value_to.currency |
258 | assert self.value_from.linked_to is not None and self.value_from.linked_to.currency == self.currency | |
259 | self.base_currency = self.value_from.currency | |
260 | ||
cfab619d IB |
261 | fees_cache = {} |
262 | @classmethod | |
263 | def fetch_fees(cls, market): | |
264 | if market.__class__ not in cls.fees_cache: | |
265 | cls.fees_cache[market.__class__] = market.fetch_fees() | |
266 | return cls.fees_cache[market.__class__] | |
267 | ||
268 | ticker_cache = {} | |
269 | ticker_cache_timestamp = time.time() | |
270 | @classmethod | |
271 | def get_ticker(cls, c1, c2, market, refresh=False): | |
272 | def invert(ticker): | |
273 | return { | |
274 | "inverted": True, | |
5ab23e1c | 275 | "average": (1/ticker["bid"] + 1/ticker["ask"]) / 2, |
cfab619d IB |
276 | "original": ticker, |
277 | } | |
278 | def augment_ticker(ticker): | |
279 | ticker.update({ | |
280 | "inverted": False, | |
281 | "average": (ticker["bid"] + ticker["ask"] ) / 2, | |
282 | }) | |
283 | ||
284 | if time.time() - cls.ticker_cache_timestamp > 5: | |
285 | cls.ticker_cache = {} | |
286 | cls.ticker_cache_timestamp = time.time() | |
287 | elif not refresh: | |
288 | if (c1, c2, market.__class__) in cls.ticker_cache: | |
289 | return cls.ticker_cache[(c1, c2, market.__class__)] | |
290 | if (c2, c1, market.__class__) in cls.ticker_cache: | |
291 | return invert(cls.ticker_cache[(c2, c1, market.__class__)]) | |
292 | ||
293 | try: | |
294 | cls.ticker_cache[(c1, c2, market.__class__)] = market.fetch_ticker("{}/{}".format(c1, c2)) | |
295 | augment_ticker(cls.ticker_cache[(c1, c2, market.__class__)]) | |
296 | except ccxt.ExchangeError: | |
297 | try: | |
298 | cls.ticker_cache[(c2, c1, market.__class__)] = market.fetch_ticker("{}/{}".format(c2, c1)) | |
299 | augment_ticker(cls.ticker_cache[(c2, c1, market.__class__)]) | |
300 | except ccxt.ExchangeError: | |
301 | cls.ticker_cache[(c1, c2, market.__class__)] = None | |
302 | return cls.get_ticker(c1, c2, market) | |
303 | ||
dd359bc0 | 304 | @classmethod |
deb8924c | 305 | def compute_trades(cls, values_in_base, new_repartition, market=None, compute_value=None): |
dd359bc0 IB |
306 | base_currency = sum(values_in_base.values()).currency |
307 | for currency in Balance.currencies(): | |
308 | if currency == base_currency: | |
309 | continue | |
310 | cls.trades[currency] = cls( | |
311 | values_in_base.get(currency, Amount(base_currency, 0)), | |
312 | new_repartition.get(currency, Amount(base_currency, 0)), | |
313 | currency, | |
314 | market=market | |
315 | ) | |
deb8924c IB |
316 | if compute_value is not None: |
317 | cls.trades[currency].prepare_order(compute_value=compute_value) | |
dd359bc0 IB |
318 | return cls.trades |
319 | ||
320 | @property | |
321 | def action(self): | |
322 | if self.value_from == self.value_to: | |
323 | return None | |
324 | if self.base_currency == self.currency: | |
325 | return None | |
326 | ||
327 | if self.value_from < self.value_to: | |
328 | return "buy" | |
329 | else: | |
330 | return "sell" | |
331 | ||
cfab619d | 332 | def order_action(self, inverted): |
dd359bc0 IB |
333 | if self.value_from < self.value_to: |
334 | return "ask" if not inverted else "bid" | |
335 | else: | |
336 | return "bid" if not inverted else "ask" | |
337 | ||
deb8924c | 338 | def prepare_order(self, compute_value="default"): |
dd359bc0 IB |
339 | if self.action is None: |
340 | return | |
341 | ticker = self.value_from.ticker | |
342 | inverted = ticker["inverted"] | |
343 | ||
344 | if not inverted: | |
345 | value_from = self.value_from.linked_to | |
c2644ba8 | 346 | value_to = self.value_to.in_currency(self.currency, self.market, rate=1/self.value_from.rate) |
dd359bc0 IB |
347 | delta = abs(value_to - value_from) |
348 | currency = self.base_currency | |
349 | else: | |
cfab619d | 350 | ticker = ticker["original"] |
dd359bc0 IB |
351 | delta = abs(self.value_to - self.value_from) |
352 | currency = self.currency | |
353 | ||
deb8924c | 354 | rate = Trade.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) |
dd359bc0 | 355 | |
cfab619d | 356 | self.orders.append(Order(self.order_action(inverted), delta, rate, currency)) |
dd359bc0 | 357 | |
deb8924c IB |
358 | @classmethod |
359 | def compute_value(cls, ticker, action, compute_value="default"): | |
360 | if type(compute_value) == str: | |
361 | compute_value = Computation.computations[compute_value] | |
362 | return compute_value(ticker, action) | |
363 | ||
dd359bc0 IB |
364 | @classmethod |
365 | def all_orders(cls): | |
366 | return sum(map(lambda v: v.orders, cls.trades.values()), []) | |
367 | ||
368 | @classmethod | |
369 | def follow_orders(cls, market): | |
370 | orders = cls.all_orders() | |
371 | finished_orders = [] | |
372 | while len(orders) != len(finished_orders): | |
373 | time.sleep(30) | |
374 | for order in orders: | |
375 | if order in finished_orders: | |
376 | continue | |
377 | if order.get_status(market) != "open": | |
378 | finished_orders.append(order) | |
379 | print("finished {}".format(order)) | |
380 | print("All orders finished") | |
381 | ||
382 | def __repr__(self): | |
383 | return "Trade({} -> {} in {}, {})".format( | |
384 | self.value_from, | |
385 | self.value_to, | |
386 | self.currency, | |
387 | self.action) | |
388 | ||
389 | class Order: | |
390 | DEBUG = True | |
391 | ||
392 | def __init__(self, action, amount, rate, base_currency): | |
393 | self.action = action | |
394 | self.amount = amount | |
395 | self.rate = rate | |
396 | self.base_currency = base_currency | |
397 | self.result = None | |
398 | self.status = "not run" | |
399 | ||
400 | def __repr__(self): | |
401 | return "Order({} {} at {} {} [{}])".format( | |
402 | self.action, | |
403 | self.amount, | |
404 | self.rate, | |
405 | self.base_currency, | |
406 | self.status | |
407 | ) | |
408 | ||
409 | def run(self, market): | |
410 | symbol = "{}/{}".format(self.amount.currency, self.base_currency) | |
411 | amount = self.amount.value | |
412 | ||
413 | if self.DEBUG: | |
414 | print("market.create_order('{}', 'limit', '{}', {}, price={})".format( | |
415 | symbol, self.action, amount, self.rate)) | |
416 | else: | |
417 | try: | |
418 | self.result = market.create_order(symbol, 'limit', self.action, amount, price=self.rate) | |
419 | self.status = "open" | |
420 | except Exception: | |
421 | pass | |
422 | ||
423 | def get_status(self, market): | |
424 | # other states are "closed" and "canceled" | |
425 | if self.status == "open": | |
426 | result = market.fetch_order(self.result['id']) | |
427 | self.status = result["status"] | |
428 | return self.status | |
429 | ||
dd359bc0 | 430 | def print_orders(market, base_currency="BTC"): |
deb8924c | 431 | Balance.prepare_trades(market, base_currency=base_currency, compute_value="average") |
5ab23e1c IB |
432 | for currency, balance in Balance.known_balances.items(): |
433 | print(balance) | |
dd359bc0 IB |
434 | for currency, trade in Trade.trades.items(): |
435 | print(trade) | |
436 | for order in trade.orders: | |
437 | print("\t", order, sep="") | |
438 | ||
439 | def make_orders(market, base_currency="BTC"): | |
440 | Balance.prepare_trades(market, base_currency=base_currency) | |
441 | for currency, trade in Trade.trades.items(): | |
442 | print(trade) | |
443 | for order in trade.orders: | |
444 | print("\t", order, sep="") | |
445 | order.run(market) | |
446 | ||
447 | if __name__ == '__main__': | |
448 | print_orders(market) |