]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blame - store.py
Add processors
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / store.py
CommitLineData
6ca5a1ec 1import portfolio
3d0247f9
IB
2import simplejson as json
3from decimal import Decimal as D, ROUND_DOWN
4from datetime import date, datetime
aca4d437 5import inspect
6ca5a1ec 6
3d0247f9
IB
7__all__ = ["BalanceStore", "ReportStore", "TradeStore"]
8
9class ReportStore:
f86ee140
IB
10 def __init__(self, market, verbose_print=True):
11 self.market = market
12 self.verbose_print = verbose_print
3d0247f9 13
f86ee140
IB
14 self.logs = []
15
16 def print_log(self, message):
3d0247f9 17 message = str(message)
f86ee140 18 if self.verbose_print:
3d0247f9
IB
19 print(message)
20
f86ee140 21 def add_log(self, hash_):
3d0247f9 22 hash_["date"] = datetime.now()
f86ee140 23 self.logs.append(hash_)
3d0247f9 24
f86ee140 25 def to_json(self):
3d0247f9
IB
26 def default_json_serial(obj):
27 if isinstance(obj, (datetime, date)):
28 return obj.isoformat()
be54a201 29 return str(obj)
f86ee140 30 return json.dumps(self.logs, default=default_json_serial)
3d0247f9 31
f86ee140
IB
32 def set_verbose(self, verbose_print):
33 self.verbose_print = verbose_print
3d0247f9 34
7bd830a8
IB
35 def log_stage(self, stage, **kwargs):
36 def as_json(element):
37 if callable(element):
38 return inspect.getsource(element).strip()
39 elif hasattr(element, "as_json"):
40 return element.as_json()
41 else:
42 return element
43
44 args = { k: as_json(v) for k, v in kwargs.items() }
45 args_str = ["{}={}".format(k, v) for k, v in args.items()]
f86ee140 46 self.print_log("-" * (len(stage) + 8))
7bd830a8 47 self.print_log("[Stage] {} {}".format(stage, ", ".join(args_str)))
3d0247f9 48
f86ee140 49 self.add_log({
3d0247f9
IB
50 "type": "stage",
51 "stage": stage,
7bd830a8 52 "args": args,
3d0247f9
IB
53 })
54
f86ee140
IB
55 def log_balances(self, tag=None):
56 self.print_log("[Balance]")
57 for currency, balance in self.market.balances.all.items():
58 self.print_log("\t{}".format(balance))
3d0247f9 59
f86ee140 60 self.add_log({
3d0247f9 61 "type": "balance",
18167a3c 62 "tag": tag,
f86ee140 63 "balances": self.market.balances.as_json()
3d0247f9
IB
64 })
65
f86ee140 66 def log_tickers(self, amounts, other_currency,
3d0247f9
IB
67 compute_value, type):
68 values = {}
69 rates = {}
aca4d437
IB
70 if callable(compute_value):
71 compute_value = inspect.getsource(compute_value).strip()
72
3d0247f9
IB
73 for currency, amount in amounts.items():
74 values[currency] = amount.as_json()["value"]
75 rates[currency] = amount.rate
f86ee140 76 self.add_log({
3d0247f9
IB
77 "type": "tickers",
78 "compute_value": compute_value,
79 "balance_type": type,
80 "currency": other_currency,
81 "balances": values,
82 "rates": rates,
83 "total": sum(amounts.values()).as_json()["value"]
84 })
85
f86ee140
IB
86 def log_dispatch(self, amount, amounts, liquidity, repartition):
87 self.add_log({
3d0247f9
IB
88 "type": "dispatch",
89 "liquidity": liquidity,
90 "repartition_ratio": repartition,
91 "total_amount": amount.as_json(),
92 "repartition": { k: v.as_json()["value"] for k, v in amounts.items() }
93 })
94
f86ee140 95 def log_trades(self, matching_and_trades, only):
3d0247f9
IB
96 trades = []
97 for matching, trade in matching_and_trades:
98 trade_json = trade.as_json()
99 trade_json["skipped"] = not matching
100 trades.append(trade_json)
101
f86ee140 102 self.add_log({
3d0247f9
IB
103 "type": "trades",
104 "only": only,
f86ee140 105 "debug": self.market.debug,
3d0247f9
IB
106 "trades": trades
107 })
108
f86ee140 109 def log_orders(self, orders, tick=None, only=None, compute_value=None):
aca4d437
IB
110 if callable(compute_value):
111 compute_value = inspect.getsource(compute_value).strip()
f86ee140
IB
112 self.print_log("[Orders]")
113 self.market.trades.print_all_with_order(ind="\t")
114 self.add_log({
3d0247f9
IB
115 "type": "orders",
116 "only": only,
117 "compute_value": compute_value,
118 "tick": tick,
119 "orders": [order.as_json() for order in orders if order is not None]
120 })
121
f86ee140 122 def log_order(self, order, tick, finished=False, update=None,
3d0247f9 123 new_order=None, compute_value=None):
aca4d437
IB
124 if callable(compute_value):
125 compute_value = inspect.getsource(compute_value).strip()
3d0247f9 126 if finished:
f86ee140 127 self.print_log("[Order] Finished {}".format(order))
3d0247f9 128 elif update == "waiting":
f86ee140 129 self.print_log("[Order] {}, tick {}, waiting".format(order, tick))
3d0247f9 130 elif update == "adjusting":
f86ee140 131 self.print_log("[Order] {}, tick {}, cancelling and adjusting to {}".format(order, tick, new_order))
3d0247f9 132 elif update == "market_fallback":
f86ee140 133 self.print_log("[Order] {}, tick {}, fallbacking to market value".format(order, tick))
3d0247f9 134 elif update == "market_adjust":
f86ee140 135 self.print_log("[Order] {}, tick {}, market value, cancelling and adjusting to {}".format(order, tick, new_order))
3d0247f9 136
f86ee140 137 self.add_log({
3d0247f9
IB
138 "type": "order",
139 "tick": tick,
140 "update": update,
141 "order": order.as_json(),
142 "compute_value": compute_value,
143 "new_order": new_order.as_json() if new_order is not None else None
144 })
145
f86ee140
IB
146 def log_move_balances(self, needed, moving):
147 self.add_log({
3d0247f9 148 "type": "move_balances",
f86ee140 149 "debug": self.market.debug,
3d0247f9
IB
150 "needed": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in needed.items() },
151 "moving": { k: v.as_json()["value"] if isinstance(v, portfolio.Amount) else v for k, v in moving.items() },
152 })
153
f86ee140
IB
154 def log_http_request(self, method, url, body, headers, response):
155 self.add_log({
3d0247f9
IB
156 "type": "http_request",
157 "method": method,
158 "url": url,
159 "body": body,
160 "headers": headers,
161 "status": response.status_code,
162 "response": response.text
163 })
164
f86ee140
IB
165 def log_error(self, action, message=None, exception=None):
166 self.print_log("[Error] {}".format(action))
3d0247f9 167 if exception is not None:
f86ee140 168 self.print_log(str("\t{}: {}".format(exception.__class__.__name__, exception)))
3d0247f9 169 if message is not None:
f86ee140 170 self.print_log("\t{}".format(message))
3d0247f9 171
f86ee140 172 self.add_log({
3d0247f9
IB
173 "type": "error",
174 "action": action,
175 "exception_class": exception.__class__.__name__ if exception is not None else None,
176 "exception_message": str(exception) if exception is not None else None,
177 "message": message,
178 })
179
f86ee140
IB
180 def log_debug_action(self, action):
181 self.print_log("[Debug] {}".format(action))
3d0247f9 182
f86ee140 183 self.add_log({
3d0247f9
IB
184 "type": "debug_action",
185 "action": action,
186 })
6ca5a1ec
IB
187
188class BalanceStore:
f86ee140
IB
189 def __init__(self, market):
190 self.market = market
191 self.all = {}
6ca5a1ec 192
f86ee140
IB
193 def currencies(self):
194 return self.all.keys()
6ca5a1ec 195
f86ee140 196 def in_currency(self, other_currency, compute_value="average", type="total"):
6ca5a1ec 197 amounts = {}
f86ee140 198 for currency, balance in self.all.items():
6ca5a1ec 199 other_currency_amount = getattr(balance, type)\
f86ee140 200 .in_currency(other_currency, self.market, compute_value=compute_value)
6ca5a1ec 201 amounts[currency] = other_currency_amount
f86ee140 202 self.market.report.log_tickers(amounts, other_currency,
3d0247f9 203 compute_value, type)
6ca5a1ec
IB
204 return amounts
205
f86ee140
IB
206 def fetch_balances(self, tag=None):
207 all_balances = self.market.ccxt.fetch_all_balances()
6ca5a1ec
IB
208 for currency, balance in all_balances.items():
209 if balance["exchange_total"] != 0 or balance["margin_total"] != 0 or \
f86ee140
IB
210 currency in self.all:
211 self.all[currency] = portfolio.Balance(currency, balance)
212 self.market.report.log_balances(tag=tag)
6ca5a1ec 213
f86ee140 214 def dispatch_assets(self, amount, liquidity="medium", repartition=None):
6ca5a1ec 215 if repartition is None:
f86ee140 216 repartition = portfolio.Portfolio.repartition(self.market, liquidity=liquidity)
6ca5a1ec
IB
217 sum_ratio = sum([v[0] for k, v in repartition.items()])
218 amounts = {}
219 for currency, (ptt, trade_type) in repartition.items():
220 amounts[currency] = ptt * amount / sum_ratio
221 if trade_type == "short":
222 amounts[currency] = - amounts[currency]
aca4d437 223 self.all.setdefault(currency, portfolio.Balance(currency, {}))
f86ee140 224 self.market.report.log_dispatch(amount, amounts, liquidity, repartition)
6ca5a1ec
IB
225 return amounts
226
f86ee140
IB
227 def as_json(self):
228 return { k: v.as_json() for k, v in self.all.items() }
3d0247f9 229
6ca5a1ec 230class TradeStore:
f86ee140
IB
231 def __init__(self, market):
232 self.market = market
233 self.all = []
6ca5a1ec 234
aca4d437
IB
235 @property
236 def pending(self):
237 return list(filter(lambda t: not t.is_fullfiled, self.all))
238
f86ee140 239 def compute_trades(self, values_in_base, new_repartition, only=None):
3d0247f9 240 computed_trades = []
6ca5a1ec 241 base_currency = sum(values_in_base.values()).currency
f86ee140 242 for currency in self.market.balances.currencies():
6ca5a1ec
IB
243 if currency == base_currency:
244 continue
245 value_from = values_in_base.get(currency, portfolio.Amount(base_currency, 0))
246 value_to = new_repartition.get(currency, portfolio.Amount(base_currency, 0))
1aa7d4fa 247
6ca5a1ec 248 if value_from.value * value_to.value < 0:
f86ee140 249 computed_trades.append(self.trade_if_matching(
1aa7d4fa 250 value_from, portfolio.Amount(base_currency, 0),
f86ee140
IB
251 currency, only=only))
252 computed_trades.append(self.trade_if_matching(
1aa7d4fa 253 portfolio.Amount(base_currency, 0), value_to,
f86ee140 254 currency, only=only))
6ca5a1ec 255 else:
f86ee140 256 computed_trades.append(self.trade_if_matching(
3d0247f9 257 value_from, value_to,
f86ee140 258 currency, only=only))
3d0247f9
IB
259 for matching, trade in computed_trades:
260 if matching:
f86ee140
IB
261 self.all.append(trade)
262 self.market.report.log_trades(computed_trades, only)
1aa7d4fa 263
f86ee140
IB
264 def trade_if_matching(self, value_from, value_to, currency,
265 only=None):
1aa7d4fa 266 trade = portfolio.Trade(value_from, value_to, currency,
f86ee140 267 self.market)
3d0247f9
IB
268 matching = only is None or trade.action == only
269 return [matching, trade]
6ca5a1ec 270
f86ee140 271 def prepare_orders(self, only=None, compute_value="default"):
3d0247f9 272 orders = []
aca4d437 273 for trade in self.pending:
6ca5a1ec 274 if only is None or trade.action == only:
3d0247f9 275 orders.append(trade.prepare_order(compute_value=compute_value))
f86ee140 276 self.market.report.log_orders(orders, only, compute_value)
6ca5a1ec 277
f86ee140
IB
278 def print_all_with_order(self, ind=""):
279 for trade in self.all:
3d0247f9 280 trade.print_with_order(ind=ind)
6ca5a1ec 281
f86ee140
IB
282 def run_orders(self):
283 orders = self.all_orders(state="pending")
3d0247f9 284 for order in orders:
6ca5a1ec 285 order.run()
f86ee140
IB
286 self.market.report.log_stage("run_orders")
287 self.market.report.log_orders(orders)
6ca5a1ec 288
f86ee140
IB
289 def all_orders(self, state=None):
290 all_orders = sum(map(lambda v: v.orders, self.all), [])
6ca5a1ec
IB
291 if state is None:
292 return all_orders
293 else:
294 return list(filter(lambda o: o.status == state, all_orders))
295
f86ee140
IB
296 def update_all_orders_status(self):
297 for order in self.all_orders(state="open"):
6ca5a1ec
IB
298 order.get_status()
299
300