diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-23 01:11:34 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-24 10:39:35 +0100 |
commit | b4e0ba0b0aa84550d0b06338b59557c3050798c9 (patch) | |
tree | ec3f6c1d6166e6d3598ecd630aa64853f8842828 /market.py | |
parent | 07fa7a4bf8f7a6f799120fb9a5965a09bea6c38e (diff) | |
download | Trader-b4e0ba0b0aa84550d0b06338b59557c3050798c9.tar.gz Trader-b4e0ba0b0aa84550d0b06338b59557c3050798c9.tar.zst Trader-b4e0ba0b0aa84550d0b06338b59557c3050798c9.zip |
Store reports to database
Fixes https://git.immae.eu/mantisbt/view.php?id=57
Diffstat (limited to 'market.py')
-rw-r--r-- | market.py | 49 |
1 files changed, 40 insertions, 9 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,7 +14,9 @@ class Market: | |||
13 | trades = None | 14 | trades = None |
14 | balances = None | 15 | balances = None |
15 | 16 | ||
16 | def __init__(self, ccxt_instance, args, user_id=None, report_path=None): | 17 | def __init__(self, ccxt_instance, args, |
18 | user_id=None, market_id=None, | ||
19 | report_path=None, pg_config=None): | ||
17 | self.args = args | 20 | self.args = args |
18 | self.debug = args.debug | 21 | self.debug = args.debug |
19 | self.ccxt = ccxt_instance | 22 | self.ccxt = ccxt_instance |
@@ -24,10 +27,13 @@ class Market: | |||
24 | self.processor = Processor(self) | 27 | self.processor = Processor(self) |
25 | 28 | ||
26 | self.user_id = user_id | 29 | self.user_id = user_id |
30 | self.market_id = market_id | ||
27 | self.report_path = report_path | 31 | self.report_path = report_path |
32 | self.pg_config = pg_config | ||
28 | 33 | ||
29 | @classmethod | 34 | @classmethod |
30 | def from_config(cls, config, args, user_id=None, report_path=None): | 35 | def from_config(cls, config, args, |
36 | user_id=None, market_id=None, report_path=None, pg_config=None): | ||
31 | config["apiKey"] = config.pop("key", None) | 37 | config["apiKey"] = config.pop("key", None) |
32 | 38 | ||
33 | ccxt_instance = ccxt.poloniexE(config) | 39 | ccxt_instance = ccxt.poloniexE(config) |
@@ -44,20 +50,45 @@ class Market: | |||
44 | ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, | 50 | ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, |
45 | ccxt_instance.session.__class__) | 51 | ccxt_instance.session.__class__) |
46 | 52 | ||
47 | return cls(ccxt_instance, args, user_id=user_id, report_path=report_path) | 53 | return cls(ccxt_instance, args, |
54 | user_id=user_id, market_id=market_id, | ||
55 | pg_config=pg_config, report_path=report_path) | ||
48 | 56 | ||
49 | def store_report(self): | 57 | def store_report(self): |
50 | self.report.merge(Portfolio.report) | 58 | self.report.merge(Portfolio.report) |
59 | date = datetime.now() | ||
60 | if self.report_path is not None: | ||
61 | self.store_file_report(date) | ||
62 | if self.pg_config is not None: | ||
63 | self.store_database_report(date) | ||
64 | |||
65 | def store_file_report(self, date): | ||
51 | try: | 66 | try: |
52 | if self.report_path is not None: | 67 | report_file = "{}/{}_{}".format(self.report_path, date.isoformat(), self.user_id) |
53 | report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id) | 68 | with open(report_file + ".json", "w") as f: |
54 | with open(report_file + ".json", "w") as f: | 69 | f.write(self.report.to_json()) |
55 | f.write(self.report.to_json()) | 70 | with open(report_file + ".log", "w") as f: |
56 | with open(report_file + ".log", "w") as f: | 71 | f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) |
57 | f.write("\n".join(map(lambda x: x[1], self.report.print_logs))) | ||
58 | except Exception as e: | 72 | except Exception as e: |
59 | print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) | 73 | print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) |
60 | 74 | ||
75 | def store_database_report(self, date): | ||
76 | try: | ||
77 | report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;' | ||
78 | line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);' | ||
79 | connection = psycopg2.connect(**self.pg_config) | ||
80 | cursor = connection.cursor() | ||
81 | cursor.execute(report_query, (date, self.market_id, self.debug)) | ||
82 | report_id = cursor.fetchone()[0] | ||
83 | for date, type_, payload in self.report.to_json_array(): | ||
84 | cursor.execute(line_query, (date, report_id, type_, payload)) | ||
85 | |||
86 | connection.commit() | ||
87 | cursor.close() | ||
88 | connection.close() | ||
89 | except Exception as e: | ||
90 | print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e)) | ||
91 | |||
61 | def process(self, actions, before=False, after=False): | 92 | def process(self, actions, before=False, after=False): |
62 | try: | 93 | try: |
63 | if len(actions or []) == 0: | 94 | if len(actions or []) == 0: |