1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
from ccxt import ExchangeError, NotSupported, RequestTimeout
import ccxt_wrapper as ccxt
import time
import psycopg2
from store import *
from cachetools.func import ttl_cache
from datetime import datetime
from retry import retry
import portfolio
class Market:
debug = False
ccxt = None
report = None
trades = None
balances = None
def __init__(self, ccxt_instance, args, **kwargs):
self.args = args
self.debug = args.debug
self.ccxt = ccxt_instance
self.ccxt._market = self
self.report = ReportStore(self, verbose_print=(not args.quiet))
self.trades = TradeStore(self)
self.balances = BalanceStore(self)
self.processor = Processor(self)
for key in ["user_id", "market_id", "report_path", "pg_config"]:
setattr(self, key, kwargs.get(key, None))
@classmethod
def from_config(cls, config, args, **kwargs):
config["apiKey"] = config.pop("key", None)
ccxt_instance = ccxt.poloniexE(config)
return cls(ccxt_instance, args, **kwargs)
def store_report(self):
self.report.merge(Portfolio.report)
date = datetime.now()
if self.report_path is not None:
self.store_file_report(date)
if self.pg_config is not None:
self.store_database_report(date)
def store_file_report(self, date):
try:
report_file = "{}/{}_{}".format(self.report_path, date.isoformat(), self.user_id)
with open(report_file + ".json", "w") as f:
f.write(self.report.to_json())
with open(report_file + ".log", "w") as f:
f.write("\n".join(map(lambda x: x[1], self.report.print_logs)))
except Exception as e:
print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
def store_database_report(self, date):
try:
report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;'
line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);'
connection = psycopg2.connect(**self.pg_config)
cursor = connection.cursor()
cursor.execute(report_query, (date, self.market_id, self.debug))
report_id = cursor.fetchone()[0]
for date, type_, payload in self.report.to_json_array():
cursor.execute(line_query, (date, report_id, type_, payload))
connection.commit()
cursor.close()
connection.close()
except Exception as e:
print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e))
def process(self, actions, before=False, after=False):
try:
if len(actions or []) == 0:
if before:
self.processor.process("sell_all", steps="before")
if after:
self.processor.process("sell_all", steps="after")
else:
for action in actions:
if hasattr(self, action):
getattr(self, action)()
else:
self.report.log_error("market_process", message="Unknown action {}".format(action))
except Exception as e:
self.report.log_error("market_process", exception=e)
finally:
self.store_report()
@retry(RequestTimeout, tries=5)
def move_balances(self):
needed_in_margin = {}
moving_to_margin = {}
for currency, balance in self.balances.all.items():
needed_in_margin[currency] = balance.margin_in_position - balance.margin_pending_gain
for trade in self.trades.pending:
needed_in_margin.setdefault(trade.base_currency, 0)
if trade.trade_type == "short":
needed_in_margin[trade.base_currency] -= trade.delta
for currency, needed in needed_in_margin.items():
current_balance = self.balances.all[currency].margin_available
moving_to_margin[currency] = (needed - current_balance)
delta = moving_to_margin[currency].value
action = "Moving {} from exchange to margin".format(moving_to_margin[currency])
if self.debug and delta != 0:
self.report.log_debug_action(action)
continue
try:
if delta > 0:
self.ccxt.transfer_balance(currency, delta, "exchange", "margin")
elif delta < 0:
self.ccxt.transfer_balance(currency, -delta, "margin", "exchange")
except RequestTimeout as e:
self.report.log_error(action, message="Retrying", exception=e)
self.report.log_move_balances(needed_in_margin, moving_to_margin)
self.balances.fetch_balances()
raise e
self.report.log_move_balances(needed_in_margin, moving_to_margin)
self.balances.fetch_balances()
@ttl_cache(ttl=3600)
def fetch_fees(self):
return self.ccxt.fetch_fees()
@ttl_cache(maxsize=20, ttl=5)
def get_tickers(self, refresh=False):
try:
return self.ccxt.fetch_tickers()
except NotSupported:
return None
@ttl_cache(maxsize=20, ttl=5)
def get_ticker(self, c1, c2, refresh=False):
def invert(ticker):
return {
"inverted": True,
"average": (1/ticker["bid"] + 1/ticker["ask"]) / 2,
"original": ticker,
}
def augment_ticker(ticker):
ticker.update({
"inverted": False,
"average": (ticker["bid"] + ticker["ask"] ) / 2,
})
return ticker
tickers = self.get_tickers()
if tickers is None:
try:
ticker = augment_ticker(self.ccxt.fetch_ticker("{}/{}".format(c1, c2)))
except ExchangeError:
try:
ticker = invert(augment_ticker(self.ccxt.fetch_ticker("{}/{}".format(c2, c1))))
except ExchangeError:
ticker = None
else:
if "{}/{}".format(c1, c2) in tickers:
ticker = augment_ticker(tickers["{}/{}".format(c1, c2)])
elif "{}/{}".format(c2, c1) in tickers:
ticker = invert(augment_ticker(tickers["{}/{}".format(c2, c1)]))
else:
ticker = None
return ticker
def follow_orders(self, sleep=None):
if sleep is None:
sleep = 7 if self.debug else 30
if self.debug:
self.report.log_debug_action("Set follow_orders tick to {}s".format(sleep))
tick = 0
self.report.log_stage("follow_orders_begin")
while len(self.trades.all_orders(state="open")) > 0:
time.sleep(sleep)
tick += 1
open_orders = self.trades.all_orders(state="open")
self.report.log_stage("follow_orders_tick_{}".format(tick))
self.report.log_orders(open_orders, tick=tick)
for order in open_orders:
if order.get_status() != "open":
self.report.log_order(order, tick, finished=True)
else:
order.trade.update_order(order, tick)
self.report.log_stage("follow_orders_end")
def prepare_trades(self, base_currency="BTC", liquidity="medium",
compute_value="average", repartition=None, only=None):
self.report.log_stage("prepare_trades",
base_currency=base_currency, liquidity=liquidity,
compute_value=compute_value, only=only,
repartition=repartition)
values_in_base = self.balances.in_currency(base_currency,
compute_value=compute_value)
total_base_value = sum(values_in_base.values())
new_repartition = self.balances.dispatch_assets(total_base_value,
liquidity=liquidity, repartition=repartition)
self.trades.compute_trades(values_in_base, new_repartition, only=only)
# Helpers
def print_orders(self, base_currency="BTC"):
self.report.log_stage("print_orders")
self.balances.fetch_balances(tag="print_orders")
self.prepare_trades(base_currency=base_currency, compute_value="average")
self.trades.prepare_orders(compute_value="average")
def print_balances(self, base_currency="BTC"):
self.report.log_stage("print_balances")
self.balances.fetch_balances()
if base_currency is not None:
self.report.print_log("total:")
self.report.print_log(sum(self.balances.in_currency(base_currency).values()))
class Processor:
scenarios = {
"wait_for_cryptoportfolio": [
{
"name": "wait",
"number": 1,
"before": False,
"after": True,
"wait_for_recent": {},
},
],
"print_orders": [
{
"name": "wait",
"number": 1,
"before": False,
"after": True,
"wait_for_recent": {},
},
{
"name": "make_orders",
"number": 2,
"before": False,
"after": True,
"fetch_balances": ["begin"],
"prepare_trades": { "compute_value": "average" },
"prepare_orders": { "compute_value": "average" },
},
],
"sell_needed": [
{
"name": "wait",
"number": 0,
"before": False,
"after": True,
"wait_for_recent": {},
},
{
"name": "sell",
"number": 1,
"before": False,
"after": True,
"fetch_balances": ["begin", "end"],
"prepare_trades": {},
"prepare_orders": { "only": "dispose", "compute_value": "average" },
"run_orders": {},
"follow_orders": {},
"close_trades": {},
},
{
"name": "buy",
"number": 2,
"before": False,
"after": True,
"fetch_balances": ["begin", "end"],
"prepare_trades": { "only": "acquire" },
"prepare_orders": { "only": "acquire", "compute_value": "average" },
"move_balances": {},
"run_orders": {},
"follow_orders": {},
"close_trades": {},
},
],
"sell_all": [
{
"name": "all_sell",
"number": 1,
"before": True,
"after": False,
"fetch_balances": ["begin", "end"],
"prepare_trades": { "repartition": { "base_currency": (1, "long") } },
"prepare_orders": { "compute_value": "average" },
"run_orders": {},
"follow_orders": {},
"close_trades": {},
},
{
"name": "wait",
"number": 2,
"before": False,
"after": True,
"wait_for_recent": {},
},
{
"name": "all_buy",
"number": 3,
"before": False,
"after": True,
"fetch_balances": ["begin", "end"],
"prepare_trades": {},
"prepare_orders": { "compute_value": "average" },
"move_balances": {},
"run_orders": {},
"follow_orders": {},
"close_trades": {},
},
]
}
ordered_actions = [
"wait_for_recent", "prepare_trades", "prepare_orders",
"move_balances", "run_orders", "follow_orders",
"close_trades"]
def __init__(self, market):
self.market = market
def select_steps(self, scenario, step):
if step == "all":
return scenario
elif step == "before" or step == "after":
return list(filter(lambda x: step in x and x[step], scenario))
elif type(step) == int:
return [scenario[step-1]]
elif type(step) == str:
return list(filter(lambda x: x["name"] == step, scenario))
else:
raise TypeError("Unknown step {}".format(step))
def process(self, scenario_name, steps="all", **kwargs):
scenario = self.scenarios[scenario_name]
selected_steps = []
if type(steps) == str or type(steps) == int:
selected_steps += self.select_steps(scenario, steps)
else:
for step in steps:
selected_steps += self.select_steps(scenario, step)
for step in selected_steps:
self.process_step(scenario_name, step, kwargs)
def process_step(self, scenario_name, step, kwargs):
process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"])
self.market.report.log_stage("{}_begin".format(process_name))
if "begin" in step.get("fetch_balances", []):
self.market.balances.fetch_balances(tag="{}_begin".format(process_name))
for action in self.ordered_actions:
if action in step:
self.run_action(action, step[action], kwargs)
if "end" in step.get("fetch_balances", []):
self.market.balances.fetch_balances(tag="{}_end".format(process_name))
self.market.report.log_stage("{}_end".format(process_name))
def method_arguments(self, action):
import inspect
if action == "wait_for_recent":
method = Portfolio.wait_for_recent
elif action == "prepare_trades":
method = self.market.prepare_trades
elif action == "prepare_orders":
method = self.market.trades.prepare_orders
elif action == "move_balances":
method = self.market.move_balances
elif action == "run_orders":
method = self.market.trades.run_orders
elif action == "follow_orders":
method = self.market.follow_orders
elif action == "close_trades":
method = self.market.trades.close_trades
signature = inspect.getfullargspec(method)
defaults = signature.defaults or []
kwargs = signature.args[-len(defaults):]
return [method, kwargs]
def parse_args(self, action, default_args, kwargs):
method, allowed_arguments = self.method_arguments(action)
args = {k: v for k, v in {**default_args, **kwargs}.items() if k in allowed_arguments }
if "repartition" in args and "base_currency" in args["repartition"]:
r = args["repartition"]
r[args.get("base_currency", "BTC")] = r.pop("base_currency")
return method, args
def run_action(self, action, default_args, kwargs):
method, args = self.parse_args(action, default_args, kwargs)
method(**args)
|