aboutsummaryrefslogtreecommitdiff
path: root/market.py
diff options
context:
space:
mode:
Diffstat (limited to 'market.py')
-rw-r--r--market.py51
1 files changed, 38 insertions, 13 deletions
diff --git a/market.py b/market.py
index 2ddebfa..496ec45 100644
--- a/market.py
+++ b/market.py
@@ -1,6 +1,7 @@
1from ccxt import ExchangeError, NotSupported 1from ccxt import ExchangeError, NotSupported
2import ccxt_wrapper as ccxt 2import ccxt_wrapper as ccxt
3import time 3import time
4import psycopg2
4from store import * 5from store import *
5from cachetools.func import ttl_cache 6from cachetools.func import ttl_cache
6from datetime import datetime 7from 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: