2 from datetime
import datetime
, timedelta
3 from decimal
import Decimal
as D
, ROUND_DOWN
4 # Put your poloniex api key in market.py
5 from json
import JSONDecodeError
6 from ccxt
import ExchangeError
, ExchangeNotAvailable
11 # FIXME: correctly handle web call timeouts
14 URL
= "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
20 def wait_for_recent(cls
, delta
=4):
21 cls
.repartition(refetch
=True)
22 while cls
.last_date
is None or datetime
.now() - cls
.last_date
> timedelta(delta
):
24 cls
.repartition(refetch
=True)
27 def repartition(cls
, liquidity
="medium", refetch
=False):
28 cls
.parse_cryptoportfolio(refetch
=refetch
)
29 liquidities
= cls
.liquidities
[liquidity
]
30 return liquidities
[cls
.last_date
]
33 def get_cryptoportfolio(cls
):
35 r
= requests
.get(cls
.URL
)
39 cls
.data
= r
.json(parse_int
=D
, parse_float
=D
)
40 except JSONDecodeError
:
44 def parse_cryptoportfolio(cls
, refetch
=False):
45 if refetch
or cls
.data
is None:
46 cls
.get_cryptoportfolio()
48 def filter_weights(weight_hash
):
49 if weight_hash
[1][0] == 0:
51 if weight_hash
[0] == "_row":
56 def clean_weights_(h
):
57 if h
[0].endswith("s"):
58 return [h
[0][0:-1], (h
[1][i
], "short")]
60 return [h
[0], (h
[1][i
], "long")]
63 def parse_weights(portfolio_hash
):
64 weights_hash
= portfolio_hash
["weights"]
66 for i
in range(len(weights_hash
["_row"])):
67 date
= datetime
.strptime(weights_hash
["_row"][i
], "%Y-%m-%d")
68 weights
[date
] = dict(filter(
70 map(clean_weights(i
), weights_hash
.items())))
73 high_liquidity
= parse_weights(cls
.data
["portfolio_1"])
74 medium_liquidity
= parse_weights(cls
.data
["portfolio_2"])
77 "medium": medium_liquidity
,
78 "high": high_liquidity
,
80 cls
.last_date
= max(max(medium_liquidity
.keys()), max(high_liquidity
.keys()))
84 "default": lambda x
, y
: x
[y
],
85 "average": lambda x
, y
: x
["average"],
86 "bid": lambda x
, y
: x
["bid"],
87 "ask": lambda x
, y
: x
["ask"],
91 def compute_value(cls
, ticker
, action
, compute_value
="default"):
96 if isinstance(compute_value
, str):
97 compute_value
= cls
.computations
[compute_value
]
98 return compute_value(ticker
, action
)
101 def __init__(self
, currency
, value
, linked_to
=None, ticker
=None, rate
=None):
102 self
.currency
= currency
103 self
.value
= D(value
)
104 self
.linked_to
= linked_to
108 def in_currency(self
, other_currency
, market
, rate
=None, action
=None, compute_value
="average"):
109 if other_currency
== self
.currency
:
117 asset_ticker
= h
.get_ticker(self
.currency
, other_currency
, market
)
118 if asset_ticker
is not None:
119 rate
= Computation
.compute_value(asset_ticker
, action
, compute_value
=compute_value
)
127 raise Exception("This asset is not available in the chosen market")
129 def __round__(self
, n
=8):
130 return Amount(self
.currency
, self
.value
.quantize(D(1)/D(10**n
), rounding
=ROUND_DOWN
))
133 return Amount(self
.currency
, abs(self
.value
))
135 def __add__(self
, other
):
138 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
139 raise Exception("Summing amounts must be done with same currencies")
140 return Amount(self
.currency
, self
.value
+ other
.value
)
142 def __radd__(self
, other
):
146 return self
.__add
__(other
)
148 def __sub__(self
, other
):
151 if other
.currency
!= self
.currency
and other
.value
* self
.value
!= 0:
152 raise Exception("Summing amounts must be done with same currencies")
153 return Amount(self
.currency
, self
.value
- other
.value
)
155 def __rsub__(self
, other
):
159 return -self
.__sub
__(other
)
161 def __mul__(self
, value
):
162 if not isinstance(value
, (int, float, D
)):
163 raise TypeError("Amount may only be multiplied by numbers")
164 return Amount(self
.currency
, self
.value
* value
)
166 def __rmul__(self
, value
):
167 return self
.__mul
__(value
)
169 def __floordiv__(self
, value
):
170 if not isinstance(value
, (int, float, D
)):
171 raise TypeError("Amount may only be divided by numbers")
172 return Amount(self
.currency
, self
.value
/ value
)
174 def __truediv__(self
, value
):
175 return self
.__floordiv
__(value
)
177 def __lt__(self
, other
):
179 return self
.value
< 0
180 if self
.currency
!= other
.currency
:
181 raise Exception("Comparing amounts must be done with same currencies")
182 return self
.value
< other
.value
184 def __le__(self
, other
):
185 return self
== other
or self
< other
187 def __gt__(self
, other
):
188 return not self
<= other
190 def __ge__(self
, other
):
191 return not self
< other
193 def __eq__(self
, other
):
195 return self
.value
== 0
196 if self
.currency
!= other
.currency
:
197 raise Exception("Comparing amounts must be done with same currencies")
198 return self
.value
== other
.value
200 def __ne__(self
, other
):
201 return not self
== other
204 return Amount(self
.currency
, - self
.value
)
207 if self
.linked_to
is None:
208 return "{:.8f} {}".format(self
.value
, self
.currency
)
210 return "{:.8f} {} [{}]".format(self
.value
, self
.currency
, self
.linked_to
)
213 if self
.linked_to
is None:
214 return "Amount({:.8f} {})".format(self
.value
, self
.currency
)
216 return "Amount({:.8f} {} -> {})".format(self
.value
, self
.currency
, repr(self
.linked_to
))
220 def __init__(self
, currency
, hash_
):
221 self
.currency
= currency
223 "exchange_total", "exchange_used", "exchange_free",
224 "margin_total", "margin_borrowed", "margin_free"]:
225 setattr(self
, key
, Amount(currency
, hash_
.get(key
, 0)))
227 self
.margin_position_type
= hash_
.get("margin_position_type")
229 if hash_
.get("margin_borrowed_base_currency") is not None:
230 base_currency
= hash_
["margin_borrowed_base_currency"]
232 "margin_liquidation_price",
233 "margin_pending_gain",
234 "margin_lending_fees",
235 "margin_borrowed_base_price"
237 setattr(self
, key
, Amount(base_currency
, hash_
.get(key
, 0)))
240 if self
.exchange_total
> 0:
241 if self
.exchange_free
> 0 and self
.exchange_used
> 0:
242 exchange
= " Exch: [✔{} + ❌{} = {}]".format(str(self
.exchange_free
), str(self
.exchange_used
), str(self
.exchange_total
))
243 elif self
.exchange_free
> 0:
244 exchange
= " Exch: [✔{}]".format(str(self
.exchange_free
))
246 exchange
= " Exch: [❌{}]".format(str(self
.exchange_used
))
250 if self
.margin_total
> 0:
251 if self
.margin_free
!= 0 and self
.margin_borrowed
!= 0:
252 margin
= " Margin: [✔{} + borrowed {} = {}]".format(str(self
.margin_free
), str(self
.margin_borrowed
), str(self
.margin_total
))
253 elif self
.margin_free
!= 0:
254 margin
= " Margin: [✔{}]".format(str(self
.margin_free
))
256 margin
= " Margin: [borrowed {}]".format(str(self
.margin_borrowed
))
257 elif self
.margin_total
< 0:
258 margin
= " Margin: [{} @@ {}/{}]".format(str(self
.margin_total
),
259 str(self
.margin_borrowed_base_price
),
260 str(self
.margin_lending_fees
))
264 if self
.margin_total
!= 0 and self
.exchange_total
!= 0:
265 total
= " Total: [{}]".format(str(self
.total
))
269 return "Balance({}".format(self
.currency
) + "".join([exchange
, margin
, total
]) + ")"
272 def __init__(self
, value_from
, value_to
, currency
, market
=None):
273 # We have value_from of currency, and want to finish with value_to of
274 # that currency. value_* may not be in currency's terms
275 self
.currency
= currency
276 self
.value_from
= value_from
277 self
.value_to
= value_to
280 assert self
.value_from
.currency
== self
.value_to
.currency
281 if self
.value_from
!= 0:
282 assert self
.value_from
.linked_to
is not None and self
.value_from
.linked_to
.currency
== self
.currency
283 elif self
.value_from
.linked_to
is None:
284 self
.value_from
.linked_to
= Amount(self
.currency
, 0)
285 self
.base_currency
= self
.value_from
.currency
289 if self
.value_from
== self
.value_to
:
291 if self
.base_currency
== self
.currency
:
294 if abs(self
.value_from
) < abs(self
.value_to
):
299 def order_action(self
, inverted
):
300 if (self
.value_from
< self
.value_to
) != inverted
:
306 def trade_type(self
):
307 if self
.value_from
+ self
.value_to
< 0:
312 def filled_amount(self
, in_base_currency
=False):
314 for order
in self
.orders
:
315 filled_amount
+= order
.filled_amount(in_base_currency
=in_base_currency
)
318 def update_order(self
, order
, tick
):
320 if tick
in [0, 1, 3, 4, 6]:
321 print("{}, tick {}, waiting".format(order
, tick
))
323 new_order
= self
.prepare_order(compute_value
=lambda x
, y
: (x
[y
] + x
["average"]) / 2)
324 print("{}, tick {}, cancelling and adjusting to {}".format(order
, tick
, new_order
))
326 new_order
= self
.prepare_order(compute_value
=lambda x
, y
: (x
[y
]*2 + x
["average"]) / 3)
327 print("{}, tick {}, cancelling and adjusting to {}".format(order
, tick
, new_order
))
330 print("{}, tick {}, fallbacking to market value".format(order
, tick
))
331 if (tick
- 7) % 3 == 0:
332 new_order
= self
.prepare_order(compute_value
="default")
333 print("{}, tick {}, market value, cancelling and adjusting to {}".format(order
, tick
, new_order
))
335 if new_order
is not None:
339 def prepare_order(self
, compute_value
="default"):
340 if self
.action
is None:
342 ticker
= h
.get_ticker(self
.currency
, self
.base_currency
, self
.market
)
343 inverted
= ticker
["inverted"]
345 ticker
= ticker
["original"]
346 rate
= Computation
.compute_value(ticker
, self
.order_action(inverted
), compute_value
=compute_value
)
348 # FIXME: Dust amount should be removed from there if they werent
349 # honored in other sales
350 delta_in_base
= abs(self
.value_from
- self
.value_to
)
351 # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case)
354 base_currency
= self
.base_currency
356 if self
.action
== "dispose":
357 filled
= self
.filled_amount(in_base_currency
=False)
358 delta
= delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
359 # I have 10 BTC worth of FOO, and I want to sell 9 BTC
360 # worth of it, computed first with rate 10 FOO = 1 BTC.
361 # -> I "sell" "90" FOO at proposed rate "rate".
363 delta
= delta
- filled
364 # I already sold 60 FOO, 30 left
366 filled
= self
.filled_amount(in_base_currency
=True)
367 delta
= (delta_in_base
- filled
).in_currency(self
.currency
, self
.market
, rate
=1/rate
)
368 # I want to buy 9 BTC worth of FOO, computed with rate
370 # -> I "buy" "9 / rate" FOO at proposed rate "rate"
372 # I already bought 3 / rate FOO, 6 / rate left
374 base_currency
= self
.currency
376 if self
.action
== "dispose":
377 filled
= self
.filled_amount(in_base_currency
=True)
380 delta
= (delta_in_base
.in_currency(self
.currency
, self
.market
, rate
=1/self
.value_from
.rate
)
381 - filled
).in_currency(self
.base_currency
, self
.market
, rate
=1/rate
)
382 # I have 10 BTC worth of FOO, and I want to sell 9 BTC worth of it
383 # computed at rate 1 Foo = 0.01 BTC
384 # Computation says I should sell it at 125 FOO / BTC
385 # -> delta_in_base = 9 BTC
386 # -> delta = (9 * 1/0.01 FOO) * 1/125 = 7.2 BTC
387 # Action: "buy" "7.2 BTC" at rate "125" "FOO" on market
389 # I already bought 300/125 BTC, only 600/125 left
391 filled
= self
.filled_amount(in_base_currency
=False)
394 delta
= delta_in_base
395 # I have 1 BTC worth of FOO, and I want to buy 9 BTC worth of it
396 # At rate 100 Foo / BTC
397 # Computation says I should buy it at 125 FOO / BTC
398 # -> delta_in_base = 9 BTC
399 # Action: "sell" "9 BTC" at rate "125" "FOO" on market
401 delta
= delta
- filled
402 # I already sold 4 BTC, only 5 left
404 close_if_possible
= (self
.value_to
== 0)
407 print("Less to do than already filled: {}".format(delta
))
410 order
= Order(self
.order_action(inverted
),
411 delta
, rate
, base_currency
, self
.trade_type
,
412 self
.market
, self
, close_if_possible
=close_if_possible
)
413 self
.orders
.append(order
)
417 return "Trade({} -> {} in {}, {})".format(
423 def print_with_order(self
):
425 for order
in self
.orders
:
426 print("\t", order
, sep
="")
429 def __init__(self
, action
, amount
, rate
, base_currency
, trade_type
, market
,
430 trade
, close_if_possible
=False):
434 self
.base_currency
= base_currency
436 self
.trade_type
= trade_type
439 self
.status
= "pending"
441 self
.close_if_possible
= close_if_possible
443 self
.fetch_cache_timestamp
= None
446 return "Order({} {} {} at {} {} [{}]{})".format(
453 " ✂" if self
.close_if_possible
else "",
458 if self
.trade_type
== "long":
465 return self
.status
== "open"
469 return self
.status
== "pending"
473 return self
.status
== "closed" or self
.status
== "canceled" or self
.status
== "error"
476 symbol
= "{}/{}".format(self
.amount
.currency
, self
.base_currency
)
477 amount
= round(self
.amount
, self
.market
.order_precision(symbol
)).value
480 print("market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
481 symbol
, self
.action
, amount
, self
.rate
, self
.account
))
482 self
.results
.append({"debug": True, "id": -1}
)
485 self
.results
.append(self
.market
.create_order(symbol
, 'limit', self
.action
, amount
, price
=self
.rate
, account
=self
.account
))
486 except ExchangeNotAvailable
:
487 # Impossible to honor the order (dust amount)
488 self
.status
= "closed"
489 self
.mark_finished_order()
491 except Exception as e
:
492 self
.status
= "error"
493 print("error when running market.create_order('{}', 'limit', '{}', {}, price={}, account={})".format(
494 symbol
, self
.action
, amount
, self
.rate
, self
.account
))
495 self
.error_message
= str("{}: {}".format(e
.__class
__.__name
__, e
))
496 print(self
.error_message
)
498 self
.id = self
.results
[0]["id"]
501 def get_status(self
):
504 # other states are "closed" and "canceled"
505 if not self
.finished
:
508 self
.mark_finished_order()
511 def mark_finished_order(self
):
514 if self
.status
== "closed":
515 if self
.trade_type
== "short" and self
.action
== "buy" and self
.close_if_possible
:
516 self
.market
.close_margin_position(self
.amount
.currency
, self
.base_currency
)
518 def fetch(self
, force
=False):
519 if TradeStore
.debug
or (not force
and self
.fetch_cache_timestamp
is not None
520 and time
.time() - self
.fetch_cache_timestamp
< 10):
522 self
.fetch_cache_timestamp
= time
.time()
524 result
= self
.market
.fetch_order(self
.id)
525 self
.results
.append(result
)
527 self
.status
= result
["status"]
528 # Time at which the order started
529 self
.timestamp
= result
["datetime"]
530 self
.fetch_mouvements()
532 # FIXME: consider open order with dust remaining as closed
534 def dust_amount_remaining(self
):
535 return self
.remaining_amount() < Amount(self
.amount
.currency
, D("0.001"))
537 def remaining_amount(self
):
538 if self
.status
== "open":
540 return self
.amount
- self
.filled_amount()
542 def filled_amount(self
, in_base_currency
=False):
543 if self
.status
== "open":
546 for mouvement
in self
.mouvements
:
548 filled_amount
+= mouvement
.total_in_base
550 filled_amount
+= mouvement
.total
553 def fetch_mouvements(self
):
555 mouvements
= self
.market
.privatePostReturnOrderTrades({"orderNumber": self.id}
)
556 except ExchangeError
:
560 for mouvement_hash
in mouvements
:
561 self
.mouvements
.append(Mouvement(self
.amount
.currency
,
562 self
.base_currency
, mouvement_hash
))
566 self
.status
= "canceled"
568 self
.market
.cancel_order(self
.id)
572 def __init__(self
, currency
, base_currency
, hash_
):
573 self
.currency
= currency
574 self
.base_currency
= base_currency
575 self
.id = hash_
.get("tradeID")
576 self
.action
= hash_
.get("type")
577 self
.fee_rate
= D(hash_
.get("fee", -1))
579 self
.date
= datetime
.strptime(hash_
.get("date", ""), '%Y-%m-%d %H:%M:%S')
582 self
.rate
= D(hash_
.get("rate", 0))
583 self
.total
= Amount(currency
, hash_
.get("amount", 0))
584 # rate * total = total_in_base
585 self
.total_in_base
= Amount(base_currency
, hash_
.get("total", 0))
587 if __name__
== '__main__': # pragma: no cover
588 from market
import market
589 h
.print_orders(market
)