aboutsummaryrefslogtreecommitdiff
path: root/market.py
diff options
context:
space:
mode:
Diffstat (limited to 'market.py')
-rw-r--r--market.py49
1 files changed, 40 insertions, 9 deletions
diff --git a/market.py b/market.py
index fc5832c..78ced1a 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,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: