]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - main.py
Add logging at market instance creation
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / main.py
diff --git a/main.py b/main.py
index dcbc2a34c197e6540d8420cfb219d8152bdfa8c6..446219247cc2f8c9211032d1c03a9f1d96986a40 100644 (file)
--- a/main.py
+++ b/main.py
@@ -62,17 +62,20 @@ def make_order(market, value, currency, action="acquire",
 
 def get_user_market(config_path, user_id, debug=False):
     pg_config, report_path = parse_config(config_path)
-    market_config = list(fetch_markets(pg_config, str(user_id)))[0][0]
-    return market.Market.from_config(market_config, debug=debug)
+    market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0]
+    args = type('Args', (object,), { "debug": debug, "quiet": False })()
+    return market.Market.from_config(market_config, args,
+            pg_config=pg_config, market_id=market_id,
+            user_id=user_id, report_path=report_path)
 
 def fetch_markets(pg_config, user):
     connection = psycopg2.connect(**pg_config)
     cursor = connection.cursor()
 
     if user is None:
-        cursor.execute("SELECT config,user_id FROM market_configs")
+        cursor.execute("SELECT id,config,user_id FROM market_configs")
     else:
-        cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user)
+        cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user)
 
     for row in cursor:
         yield row
@@ -109,6 +112,9 @@ def parse_args(argv):
     parser.add_argument("--after",
             default=False, action='store_const', const=True,
             help="Run the steps after the cryptoportfolio update")
+    parser.add_argument("--quiet",
+            default=False, action='store_const', const=True,
+            help="Don't print messages")
     parser.add_argument("--debug",
             default=False, action='store_const', const=True,
             help="Run in debug mode")
@@ -117,6 +123,8 @@ def parse_args(argv):
     parser.add_argument("--action",
             action='append',
             help="Do a different action than trading (add several times to chain)")
+    parser.add_argument("--parallel", action='store_true', default=True, dest="parallel")
+    parser.add_argument("--no-parallel", action='store_false', dest="parallel")
 
     args = parser.parse_args(argv)
 
@@ -126,18 +134,32 @@ def parse_args(argv):
 
     return args
 
+def process(market_config, market_id, user_id, args, report_path, pg_config):
+    try:
+        market.Market\
+                .from_config(market_config, args,
+                        pg_config=pg_config, market_id=market_id,
+                        user_id=user_id, report_path=report_path)\
+                .process(args.action, before=args.before, after=args.after)
+    except Exception as e:
+        print("{}: {}".format(e.__class__.__name__, e))
+
 def main(argv):
     args = parse_args(argv)
 
     pg_config, report_path = parse_config(args.config)
 
-    for market_config, user_id in fetch_markets(pg_config, args.user):
-        try:
-            market.Market\
-                    .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\
-                    .process(args.action, before=args.before, after=args.after)
-        except Exception as e:
-            print("{}: {}".format(e.__class__.__name__, e))
+    if args.parallel:
+        import threading
+        market.Portfolio.start_worker()
+
+        def process_(*args):
+            threading.Thread(target=process, args=args).start()
+    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, report_path, pg_config)
 
 if __name__ == '__main__': # pragma: no cover
     main(sys.argv[1:])