X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=market.py;h=496ec45843319f2145f955e811ea49e1842e4761;hb=472787b6360221588423d03fe3e73d92c09a7c9d;hp=388dea0cadb1265f966c1aa4afe2c66b85f93c26;hpb=ada1b5f109ebaa6f3adb7cd87b007c6db891811c;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/market.py b/market.py index 388dea0..496ec45 100644 --- a/market.py +++ b/market.py @@ -1,6 +1,7 @@ from ccxt import ExchangeError, NotSupported import ccxt_wrapper as ccxt import time +import psycopg2 from store import * from cachetools.func import ttl_cache from datetime import datetime @@ -13,20 +14,21 @@ class Market: trades = None balances = None - def __init__(self, ccxt_instance, debug=False, user_id=None, report_path=None): - self.debug = debug + 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) + self.report = ReportStore(self, verbose_print=(not args.quiet)) self.trades = TradeStore(self) self.balances = BalanceStore(self) self.processor = Processor(self) - self.user_id = user_id - self.report_path = report_path + for key in ["user_id", "market_id", "report_path", "pg_config"]: + setattr(self, key, kwargs.get(key, None)) @classmethod - def from_config(cls, config, debug=False, user_id=None, report_path=None): + def from_config(cls, config, args, **kwargs): config["apiKey"] = config.pop("key", None) ccxt_instance = ccxt.poloniexE(config) @@ -43,17 +45,43 @@ class Market: ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, ccxt_instance.session.__class__) - return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path) + 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: - if self.report_path is not None: - report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id) - with open(report_file, "w") as f: - f.write(self.report.to_json()) + 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: @@ -192,6 +220,33 @@ class Market: 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",