4 import simplejson
as json
5 from decimal
import Decimal
as D
, ROUND_DOWN
8 from json
import JSONDecodeError
9 from simplejson
.errors
import JSONDecodeError
as SimpleJSONDecodeError
11 __all__
= ["Portfolio", "BalanceStore", "ReportStore", "TradeStore"]
14 def __init__(self
, market
, verbose_print
=True, no_http_dup
=False):
16 self
.verbose_print
= verbose_print
21 self
.no_http_dup
= no_http_dup
24 def merge(self
, other_report
):
25 self
.logs
+= other_report
.logs
26 self
.logs
.sort(key
=lambda x
: x
["date"])
28 self
.print_logs
+= other_report
.print_logs
29 self
.print_logs
.sort(key
=lambda x
: x
[0])
31 def print_log(self
, message
):
32 now
= datetime
.datetime
.now()
33 message
= "{:%Y-%m-%d %H:%M:%S}: {}".format(now
, str(message
))
34 self
.print_logs
.append([now
, message
])
35 if self
.verbose_print
:
38 def add_log(self
, hash_
):
39 hash_
["date"] = datetime
.datetime
.now()
40 if self
.market
is not None:
41 hash_
["user_id"] = self
.market
.user_id
42 hash_
["market_id"] = self
.market
.market_id
44 hash_
["user_id"] = None
45 hash_
["market_id"] = None
46 self
.logs
.append(hash_
)
50 def default_json_serial(obj
):
51 if isinstance(obj
, (datetime
.datetime
, datetime
.date
)):
52 return obj
.isoformat()
56 return json
.dumps(self
.logs
, default
=self
.default_json_serial
, indent
=" ")
58 def to_json_array(self
):
59 for log
in (x
.copy() for x
in self
.logs
):
63 json
.dumps(log
, default
=self
.default_json_serial
, indent
=" ")
66 def set_verbose(self
, verbose_print
):
67 self
.verbose_print
= verbose_print
69 def log_stage(self
, stage
, **kwargs
):
72 return inspect
.getsource(element
).strip()
73 elif hasattr(element
, "as_json"):
74 return element
.as_json()
78 args
= { k: as_json(v) for k, v in kwargs.items() }
79 args_str
= ["{}={}".format(k
, v
) for k
, v
in args
.items()]
80 self
.print_log("-" * (len(stage
) + 8))
81 self
.print_log("[Stage] {} {}".format(stage
, ", ".join(args_str
)))
89 def log_balances(self
, tag
=None):
90 self
.print_log("[Balance]")
91 for currency
, balance
in self
.market
.balances
.all
.items():
92 self
.print_log("\t{}".format(balance
))
97 "balances": self
.market
.balances
.as_json()
100 def log_tickers(self
, amounts
, other_currency
,
101 compute_value
, type):
104 if callable(compute_value
):
105 compute_value
= inspect
.getsource(compute_value
).strip()
107 for currency
, amount
in amounts
.items():
108 values
[currency
] = amount
.as_json()["value"]
109 rates
[currency
] = amount
.rate
112 "compute_value": compute_value
,
113 "balance_type": type,
114 "currency": other_currency
,
117 "total": sum(amounts
.values()).as_json()["value"]
120 def log_dispatch(self
, amount
, amounts
, liquidity
, repartition
):
123 "liquidity": liquidity
,
124 "repartition_ratio": repartition
,
125 "total_amount": amount
.as_json(),
126 "repartition": { k: v.as_json()["value"] for k, v in amounts.items() }
129 def log_trades(self
, matching_and_trades
, only
):
131 for matching
, trade
in matching_and_trades
:
132 trade_json
= trade
.as_json()
133 trade_json
["skipped"] = not matching
134 trades
.append(trade_json
)
139 "debug": self
.market
.debug
,
143 def log_orders(self
, orders
, tick
=None, only
=None, compute_value
=None):
144 if callable(compute_value
):
145 compute_value
= inspect
.getsource(compute_value
).strip()
146 self
.print_log("[Orders]")
147 self
.market
.trades
.print_all_with_order(ind
="\t")
151 "compute_value": compute_value
,
153 "orders": [order
.as_json() for order
in orders
if order
is not None]
156 def log_order(self
, order
, tick
, finished
=False, update
=None,
157 new_order
=None, compute_value
=None):
158 if callable(compute_value
):
159 compute_value
= inspect
.getsource(compute_value
).strip()
161 self
.print_log("[Order] Finished {}".format(order
))
162 elif update
== "waiting":
163 self
.print_log("[Order] {}, tick {}, waiting".format(order
, tick
))
164 elif update
== "adjusting":
165 self
.print_log("[Order] {}, tick {}, cancelling and adjusting to {}".format(order
, tick
, new_order
))
166 elif update
== "market_fallback":
167 self
.print_log("[Order] {}, tick {}, fallbacking to market value".format(order
, tick
))
168 elif update
== "market_adjust":
169 self
.print_log("[Order] {}, tick {}, market value, cancelling and adjusting to {}".format(order
, tick
, new_order
))
175 "order": order
.as_json(),
176 "compute_value": compute_value
,
177 "new_order": new_order
.as_json() if new_order
is not None else None
180 def log_move_balances(self
, needed
, moving
):
182 "type": "move_balances",
183 "debug": self
.market
.debug
,
184 "needed": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in needed.items() }
,
185 "moving": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in moving.items() }
,
188 def log_http_request(self
, method
, url
, body
, headers
, response
):
189 if isinstance(response
, Exception):
191 "type": "http_request",
198 "error": response
.__class
__.__name
__,
199 "error_message": str(response
),
201 self
.last_http
= None
202 elif self
.no_http_dup
and \
203 self
.last_http
is not None and \
204 self
.last_http
["url"] == url
and \
205 self
.last_http
["method"] == method
and \
206 self
.last_http
["response"] == response
.text
:
208 "type": "http_request",
213 "status": response
.status_code
,
215 "response_same_as": self
.last_http
["date"]
218 self
.last_http
= self
.add_log({
219 "type": "http_request",
224 "status": response
.status_code
,
225 "response": response
.text
,
226 "response_same_as": None,
229 def log_error(self
, action
, message
=None, exception
=None):
230 self
.print_log("[Error] {}".format(action
))
231 if exception
is not None:
232 self
.print_log(str("\t{}: {}".format(exception
.__class
__.__name
__, exception
)))
233 if message
is not None:
234 self
.print_log("\t{}".format(message
))
239 "exception_class": exception
.__class
__.__name
__ if exception
is not None else None,
240 "exception_message": str(exception
) if exception
is not None else None,
244 def log_debug_action(self
, action
):
245 self
.print_log("[Debug] {}".format(action
))
248 "type": "debug_action",
252 def log_market(self
, args
):
255 "commit": "$Format:%H$",
260 def __init__(self
, market
):
264 def currencies(self
):
265 return self
.all
.keys()
267 def in_currency(self
, other_currency
, compute_value
="average", type="total"):
269 for currency
, balance
in self
.all
.items():
270 other_currency_amount
= getattr(balance
, type)\
271 .in_currency(other_currency
, self
.market
, compute_value
=compute_value
)
272 amounts
[currency
] = other_currency_amount
273 self
.market
.report
.log_tickers(amounts
, other_currency
,
277 def fetch_balances(self
, tag
=None):
278 all_balances
= self
.market
.ccxt
.fetch_all_balances()
279 for currency
, balance
in all_balances
.items():
280 if balance
["exchange_total"] != 0 or balance
["margin_total"] != 0 or \
281 currency
in self
.all
:
282 self
.all
[currency
] = portfolio
.Balance(currency
, balance
)
283 self
.market
.report
.log_balances(tag
=tag
)
285 def dispatch_assets(self
, amount
, liquidity
="medium", repartition
=None):
286 if repartition
is None:
287 repartition
= Portfolio
.repartition(liquidity
=liquidity
)
288 sum_ratio
= sum([v
[0] for k
, v
in repartition
.items()])
290 for currency
, (ptt
, trade_type
) in repartition
.items():
291 amounts
[currency
] = ptt
* amount
/ sum_ratio
292 if trade_type
== "short":
293 amounts
[currency
] = - amounts
[currency
]
294 self
.all
.setdefault(currency
, portfolio
.Balance(currency
, {}))
295 self
.market
.report
.log_dispatch(amount
, amounts
, liquidity
, repartition
)
299 return { k: v.as_json() for k, v in self.all.items() }
302 def __init__(self
, market
):
308 return list(filter(lambda t
: t
.pending
, self
.all
))
310 def compute_trades(self
, values_in_base
, new_repartition
, only
=None):
312 base_currency
= sum(values_in_base
.values()).currency
313 for currency
in self
.market
.balances
.currencies():
314 if currency
== base_currency
:
316 value_from
= values_in_base
.get(currency
, portfolio
.Amount(base_currency
, 0))
317 value_to
= new_repartition
.get(currency
, portfolio
.Amount(base_currency
, 0))
319 if value_from
.value
* value_to
.value
< 0:
320 computed_trades
.append(self
.trade_if_matching(
321 value_from
, portfolio
.Amount(base_currency
, 0),
322 currency
, only
=only
))
323 computed_trades
.append(self
.trade_if_matching(
324 portfolio
.Amount(base_currency
, 0), value_to
,
325 currency
, only
=only
))
327 computed_trades
.append(self
.trade_if_matching(
328 value_from
, value_to
,
329 currency
, only
=only
))
330 for matching
, trade
in computed_trades
:
332 self
.all
.append(trade
)
333 self
.market
.report
.log_trades(computed_trades
, only
)
335 def trade_if_matching(self
, value_from
, value_to
, currency
,
337 trade
= portfolio
.Trade(value_from
, value_to
, currency
,
339 matching
= only
is None or trade
.action
== only
340 return [matching
, trade
]
342 def prepare_orders(self
, only
=None, compute_value
="default"):
344 for trade
in self
.pending
:
345 if only
is None or trade
.action
== only
:
346 orders
.append(trade
.prepare_order(compute_value
=compute_value
))
347 self
.market
.report
.log_orders(orders
, only
, compute_value
)
349 def close_trades(self
):
350 for trade
in self
.all
:
353 def print_all_with_order(self
, ind
=""):
354 for trade
in self
.all
:
355 trade
.print_with_order(ind
=ind
)
357 def run_orders(self
):
358 orders
= self
.all_orders(state
="pending")
361 self
.market
.report
.log_stage("run_orders")
362 self
.market
.report
.log_orders(orders
)
364 def all_orders(self
, state
=None):
365 all_orders
= sum(map(lambda v
: v
.orders
, self
.all
), [])
369 return list(filter(lambda o
: o
.status
== state
, all_orders
))
371 def update_all_orders_status(self
):
372 for order
in self
.all_orders(state
="open"):
376 def __enter__(self
, *args
):
378 def __exit__(self
, *args
):
382 def __init__(self
, value
):
383 self
.lock
= NoopLock()
386 def start_lock(self
):
388 self
.lock
= threading
.Lock()
390 def set(self
, value
):
394 def get(self
, key
=None):
396 if key
is not None and isinstance(self
.val
, dict):
397 return self
.val
.get(key
)
401 def __getattr__(self
, key
):
403 return getattr(self
.val
, key
)
406 URL
= "https://cryptoportfolio.io/wp-content/uploads/portfolio/json/cryptoportfolio.json"
407 data
= LockedVar(None)
408 liquidities
= LockedVar({})
409 last_date
= LockedVar(None)
410 report
= LockedVar(ReportStore(None, no_http_dup
=True))
412 worker_started
= False
417 def start_worker(cls
, poll
=30):
420 cls
.worker
= threading
.Thread(name
="portfolio", daemon
=True,
421 target
=cls
.wait_for_notification
, kwargs
={"poll": poll}
)
422 cls
.worker_notify
= threading
.Event()
423 cls
.callback
= threading
.Event()
425 cls
.last_date
.start_lock()
426 cls
.liquidities
.start_lock()
427 cls
.report
.start_lock()
429 cls
.worker_started
= True
433 def is_worker_thread(cls
):
434 if cls
.worker
is None:
438 return cls
.worker
== threading
.current_thread()
441 def wait_for_notification(cls
, poll
=30):
442 if not cls
.is_worker_thread():
443 raise RuntimeError("This method needs to be ran with the worker")
444 while cls
.worker_started
:
445 cls
.worker_notify
.wait()
446 if cls
.worker_started
:
447 cls
.worker_notify
.clear()
448 cls
.report
.print_log("Fetching cryptoportfolio")
449 cls
.get_cryptoportfolio(refetch
=True)
454 def stop_worker(cls
):
455 cls
.worker_started
= False
456 cls
.worker_notify
.set()
459 def notify_and_wait(cls
):
461 cls
.worker_notify
.set()
465 def wait_for_recent(cls
, delta
=4, poll
=30):
466 cls
.get_cryptoportfolio()
467 while cls
.last_date
.get() is None or datetime
.datetime
.now() - cls
.last_date
.get() > datetime
.timedelta(delta
):
468 if cls
.worker
is None:
470 cls
.report
.print_log("Attempt to fetch up-to-date cryptoportfolio")
471 cls
.get_cryptoportfolio(refetch
=True)
474 def repartition(cls
, liquidity
="medium"):
475 cls
.get_cryptoportfolio()
476 liquidities
= cls
.liquidities
.get(liquidity
)
477 return liquidities
[cls
.last_date
.get()]
480 def get_cryptoportfolio(cls
, refetch
=False):
481 if cls
.data
.get() is not None and not refetch
:
483 if cls
.worker
is not None and not cls
.is_worker_thread():
484 cls
.notify_and_wait()
487 r
= requests
.get(cls
.URL
)
488 cls
.report
.log_http_request(r
.request
.method
,
489 r
.request
.url
, r
.request
.body
, r
.request
.headers
, r
)
490 except Exception as e
:
491 cls
.report
.log_error("get_cryptoportfolio", exception
=e
)
494 cls
.data
.set(r
.json(parse_int
=D
, parse_float
=D
))
495 cls
.parse_cryptoportfolio()
496 except (JSONDecodeError
, SimpleJSONDecodeError
):
498 cls
.last_date
.set(None)
499 cls
.liquidities
.set({})
502 def parse_cryptoportfolio(cls
):
503 def filter_weights(weight_hash
):
504 if weight_hash
[1][0] == 0:
506 if weight_hash
[0] == "_row":
510 def clean_weights(i
):
511 def clean_weights_(h
):
512 if h
[0].endswith("s"):
513 return [h
[0][0:-1], (h
[1][i
], "short")]
515 return [h
[0], (h
[1][i
], "long")]
516 return clean_weights_
518 def parse_weights(portfolio_hash
):
519 if "weights" not in portfolio_hash
:
521 weights_hash
= portfolio_hash
["weights"]
523 for i
in range(len(weights_hash
["_row"])):
524 date
= datetime
.datetime
.strptime(weights_hash
["_row"][i
], "%Y-%m-%d")
525 weights
[date
] = dict(filter(
527 map(clean_weights(i
), weights_hash
.items())))
530 high_liquidity
= parse_weights(cls
.data
.get("portfolio_1"))
531 medium_liquidity
= parse_weights(cls
.data
.get("portfolio_2"))
533 cls
.liquidities
.set({
534 "medium": medium_liquidity
,
535 "high": high_liquidity
,
537 cls
.last_date
.set(max(
538 max(medium_liquidity
.keys(), default
=datetime
.datetime(1, 1, 1)),
539 max(high_liquidity
.keys(), default
=datetime
.datetime(1, 1, 1))