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