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