X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git;a=blobdiff_plain;f=main.py;h=a7ad8f73db8e3983171b9727a9e0e898915c7b5b;hp=13c22409188a34bd10138b308d766287ada0671b;hb=a4bb44d8bc27af7c57f439ffd9edca51c2f0afb4;hpb=c5ca26b83ca9f120fb39f1e61265216342f8a4db diff --git a/main.py b/main.py index 13c2240..a7ad8f7 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ import configargparse -import psycopg2 +import dbs import os import sys @@ -63,49 +63,38 @@ def get_user_market(config_path, user_id, debug=False): if debug: args.append("--debug") args = parse_args(args) - pg_config = parse_config(args) - market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0] - return market.Market.from_config(market_config, args, - pg_config=pg_config, market_id=market_id, - user_id=user_id) + parse_config(args) + market_id, market_config, user_id, options = list(fetch_markets(str(user_id)))[0] + return market.Market.from_config(market_config, args, user_id=user_id, options=options) -def fetch_markets(pg_config, user): - connection = psycopg2.connect(**pg_config) - cursor = connection.cursor() +def fetch_markets(user): + cursor = dbs.psql.cursor() if user is None: - cursor.execute("SELECT id,config,user_id FROM market_configs") + cursor.execute("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled'") else: - cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user) + cursor.execute("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled' AND user_id = %s", [user]) for row in cursor: - yield row + options = { + "liquidity": parse_liquidity(row[3]) + } + yield row[0:3] + (options,) + +def parse_liquidity(value): + if value == "high-liquidity": + return "high" + elif value == "medium-liquidity": + return "medium" + else: + return None def parse_config(args): - pg_config = { - "host": args.db_host, - "port": args.db_port, - "user": args.db_user, - "password": args.db_password, - "database": args.db_database, - } - del(args.db_host) - del(args.db_port) - del(args.db_user) - del(args.db_password) - del(args.db_database) - - redis_config = { - "host": args.redis_host, - "port": args.redis_port, - "db": args.redis_database, - } - if redis_config["host"].startswith("/"): - redis_config["unix_socket_path"] = redis_config.pop("host") - del(redis_config["port"]) - del(args.redis_host) - del(args.redis_port) - del(args.redis_database) + if args.db_host is not None: + dbs.connect_psql(args) + + if args.redis_host is not None: + dbs.connect_redis(args) report_path = args.report_path @@ -113,8 +102,6 @@ def parse_config(args): os.path.exists(report_path): os.makedirs(report_path) - return pg_config, redis_config - def parse_args(argv): parser = configargparse.ArgumentParser( description="Run the trade bot.") @@ -176,12 +163,11 @@ def parse_args(argv): parsed.action = ["sell_all"] return parsed -def process(market_config, market_id, user_id, args, pg_config, redis_config): +def process(market_config, market_id, user_id, args, options): try: market.Market\ .from_config(market_config, args, market_id=market_id, - pg_config=pg_config, redis_config=redis_config, - user_id=user_id)\ + user_id=user_id, options=options)\ .process(args.action, before=args.before, after=args.after) except Exception as e: print("{}: {}".format(e.__class__.__name__, e)) @@ -189,7 +175,7 @@ def process(market_config, market_id, user_id, args, pg_config, redis_config): def main(argv): args = parse_args(argv) - pg_config, redis_config = parse_config(args) + parse_config(args) market.Portfolio.report.set_verbose(not args.quiet) @@ -205,8 +191,8 @@ def main(argv): else: process_ = process - for market_id, market_config, user_id in fetch_markets(pg_config, args.user): - process_(market_config, market_id, user_id, args, pg_config, redis_config) + for market_id, market_config, user_id, options in fetch_markets(args.user): + process_(market_config, market_id, user_id, args, options) if args.parallel: for thread in threads: