diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-24 10:41:28 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-24 10:41:28 +0100 |
commit | 0c6eb1640c0d0c0e7b679d1702415a35319863f1 (patch) | |
tree | 758fa5203849f284bd8379d8928b22dfbbbc699e /market.py | |
parent | b53f483d54367875bed3769d2e4817866fbde224 (diff) | |
parent | 35667b31ddf1ce47a56ccbf4db9896dbc165ad0a (diff) | |
download | Trader-0c6eb1640c0d0c0e7b679d1702415a35319863f1.tar.gz Trader-0c6eb1640c0d0c0e7b679d1702415a35319863f1.tar.zst Trader-0c6eb1640c0d0c0e7b679d1702415a35319863f1.zip |
Merge branch 'store_reports' into dev
Add reports storing in database.
See https://git.immae.eu/mantisbt/view.php?id=56 for the expected schema
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 51 |
1 files changed, 38 insertions, 13 deletions
@@ -1,6 +1,7 @@ | |||
1 | from ccxt import ExchangeError, NotSupported | 1 | from ccxt import ExchangeError, NotSupported |
2 | import ccxt_wrapper as ccxt | 2 | import ccxt_wrapper as ccxt |
3 | import time | 3 | import time |
4 | import psycopg2 | ||
4 | from store import * | 5 | from store import * |
5 | from cachetools.func import ttl_cache | 6 | from cachetools.func import ttl_cache |
6 | from datetime import datetime | 7 | from datetime import datetime |
@@ -13,20 +14,21 @@ class Market: | |||
13 | trades = None | 14 | trades = None |
14 | balances = None | 15 | balances = None |
15 | 16 | ||
16 | def __init__(self, ccxt_instance, debug=False, user_id=None, report_path=None): | 17 | def __init__(self, ccxt_instance, args, **kwargs): |
17 | self.debug = debug | 18 | self.args = args |
19 | self.debug = args.debug | ||
18 | self.ccxt = ccxt_instance | 20 | self.ccxt = ccxt_instance |
19 | self.ccxt._market = self | 21 | self.ccxt._market = self |
20 | self.report = ReportStore(self) | 22 | self.report = ReportStore(self, verbose_print=(not args.quiet)) |
21 | self.trades = TradeStore(self) | 23 | self.trades = TradeStore(self) |
22 | self.balances = BalanceStore(self) | 24 | self.balances = BalanceStore(self) |
23 | self.processor = Processor(self) | 25 | self.processor = Processor(self) |
24 | 26 | ||
25 | self.user_id = user_id | 27 | for key in ["user_id", "market_id", "report_path", "pg_config"]: |
26 | self.report_path = report_path | 28 | setattr(self, key, kwargs.get(key, None)) |
27 | 29 | ||
28 | @classmethod | 30 | @classmethod |
29 | def from_config(cls, config, debug=False, user_id=None, report_path=None): | 31 | def from_config(cls, config, args, **kwargs): |
30 | config["apiKey"] = config.pop("key", None) | 32 | config["apiKey"] = config.pop("key", None) |
31 | 33 | ||
32 | ccxt_instance = ccxt.poloniexE(config) | 34 | ccxt_instance = ccxt.poloniexE(config) |
@@ -43,20 +45,43 @@ class Market: | |||
43 | ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, | 45 | ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, |
44 | ccxt_instance.session.__class__) | 46 | ccxt_instance.session.__class__) |
45 | 47 | ||
46 | return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path) | 48 | return cls(ccxt_instance, args, **kwargs) |
47 | 49 | ||
48 | def store_report(self): | 50 | def store_report(self): |
49 | self.report.merge(Portfolio.report) | 51 | self.report.merge(Portfolio.report) |
52 | date = datetime.now() | ||
53 | if self.report_path is not None: | ||
54 | self.store_file_report(date) | ||
55 | if self.pg_config is not None: | ||
56 | self.store_database_report(date) | ||
57 | |||
58 | def store_file_report(self, date): | ||
50 | try: | 59 | try: |
51 | if self.report_path is not None: | 60 | report_file = "{}/{}_{}".format(self.report_path, date.isoformat(), self.user_id) |
52 | report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id) | 61 | with open(report_file + ".json", "w") as f: |
53 | with open(report_file + ".json", "w") as f: | 62 | f.write(self.report.to_json()) |
54 | f.write(self.report.to_json()) | 63 | with open(report_file + ".log", "w") as f: |
55 | with open(report_file + ".log", "w") as f: | 64 | f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) |
56 | f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) | ||
57 | except Exception as e: | 65 | except Exception as e: |
58 | print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) | 66 | print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) |
59 | 67 | ||
68 | def store_database_report(self, date): | ||
69 | try: | ||
70 | report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;' | ||
71 | line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);' | ||
72 | connection = psycopg2.connect(**self.pg_config) | ||
73 | cursor = connection.cursor() | ||
74 | cursor.execute(report_query, (date, self.market_id, self.debug)) | ||
75 | report_id = cursor.fetchone()[0] | ||
76 | for date, type_, payload in self.report.to_json_array(): | ||
77 | cursor.execute(line_query, (date, report_id, type_, payload)) | ||
78 | |||
79 | connection.commit() | ||
80 | cursor.close() | ||
81 | connection.close() | ||
82 | except Exception as e: | ||
83 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) | ||
84 | |||
60 | def process(self, actions, before=False, after=False): | 85 | def process(self, actions, before=False, after=False): |
61 | try: | 86 | try: |
62 | if len(actions or []) == 0: | 87 | if len(actions or []) == 0: |