]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - main.py
Fix console helper
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / main.py
diff --git a/main.py b/main.py
index 41abe9e740187362c6e8baa5f8e2d6179c19add2..6383ed17775f185ad3973358a9bbcbc3a83b3a0c 100644 (file)
--- a/main.py
+++ b/main.py
+from datetime import datetime
+import configargparse
+import psycopg2
 import os
 import sys
-import configparser
-import psycopg2
-import argparse
-from datetime import datetime
 
-import portfolio, market
+import market
+import portfolio
+
+__all__ = ["make_order", "get_user_market"]
+
+def make_order(market, value, currency, action="acquire",
+        close_if_possible=False, base_currency="BTC", follow=True,
+        compute_value="average"):
+    """
+    Make an order on market
+    "market": The market on which to place the order
+    "value": The value in *base_currency* to acquire,
+             or in *currency* to dispose.
+             use negative for margin trade.
+    "action": "acquire" or "dispose".
+                "acquire" will buy long or sell short,
+                "dispose" will sell long or buy short.
+    "currency": The currency to acquire or dispose
+    "base_currency": The base currency. The value is expressed in that
+                     currency (default: BTC)
+    "follow": Whether to follow the order once run (default: True)
+    "close_if_possible": Whether to try to close the position at the end
+                         of the trade, i.e. reach exactly 0 at the end
+                         (only meaningful in "dispose"). May have
+                         unwanted effects if the end value of the
+                         currency is not 0.
+    "compute_value": Compute value to place the order
+    """
+    market.report.log_stage("make_order_begin")
+    market.balances.fetch_balances(tag="make_order_begin")
+    if action == "acquire":
+        trade = portfolio.Trade(
+                portfolio.Amount(base_currency, 0),
+                portfolio.Amount(base_currency, value),
+                currency, market)
+    else:
+        amount = portfolio.Amount(currency, value)
+        trade = portfolio.Trade(
+                amount.in_currency(base_currency, market, compute_value=compute_value),
+                portfolio.Amount(base_currency, 0),
+                currency, market)
+    market.trades.all.append(trade)
+    order = trade.prepare_order(
+            close_if_possible=close_if_possible,
+            compute_value=compute_value)
+    market.report.log_orders([order], None, compute_value)
+    market.trades.run_orders()
+    if follow:
+        market.follow_orders()
+        market.balances.fetch_balances(tag="make_order_end")
+    else:
+        market.report.log_stage("make_order_end_not_followed")
+        return order
+    market.report.log_stage("make_order_end")
+
+def get_user_market(config_path, user_id, debug=False):
+    args = ["--config", config_path]
+    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)
 
-parser = argparse.ArgumentParser(
-        description="Run the trade bot")
+def fetch_markets(pg_config, user):
+    connection = psycopg2.connect(**pg_config)
+    cursor = connection.cursor()
 
-parser.add_argument("-c", "--config",
-        default="config.ini",
-        required=False,
-        help="Config file to load (default: config.ini)")
-parser.add_argument("--before",
-        default=False, action='store_const', const=True,
-        help="Run the steps before the cryptoportfolio update")
-parser.add_argument("--after",
-        default=False, action='store_const', const=True,
-        help="Run the steps after the cryptoportfolio update")
-parser.add_argument("--debug",
-        default=False, action='store_const', const=True,
-        help="Run in debug mode")
+    if user is None:
+        cursor.execute("SELECT id,config,user_id FROM market_configs")
+    else:
+        cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user)
 
-args = parser.parse_args()
+    for row in cursor:
+        yield row
 
-if not os.path.exists(args.config):
-    print("no config file found, exiting")
-    sys.exit(1)
+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)
 
-config = configparser.ConfigParser()
-config.read(args.config)
+    report_path = args.report_path
 
-pg_config = config["postgresql"]
+    if report_path is not None and not \
+            os.path.exists(report_path):
+        os.makedirs(report_path)
 
-connection = psycopg2.connect(**pg_config)
-cursor = connection.cursor()
+    return pg_config
 
-cursor.execute("SELECT config,user_id FROM market_configs")
+def parse_args(argv):
+    parser = configargparse.ArgumentParser(
+            description="Run the trade bot.")
 
-report_path = config["app"]["report_path"]
-if not os.path.exists(report_path):
-    os.makedirs(report_path)
+    parser.add_argument("-c", "--config",
+            default="config.ini",
+            required=False, is_config_file=True,
+            help="Config file to load (default: config.ini)")
+    parser.add_argument("--before",
+            default=False, action='store_const', const=True,
+            help="Run the steps before the cryptoportfolio update")
+    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")
+    parser.add_argument("--user",
+            default=None, required=False, help="Only run for that user")
+    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")
+    parser.add_argument("--report-db", action='store_true', default=True, dest="report_db",
+            help="Store report to database (default)")
+    parser.add_argument("--no-report-db", action='store_false', dest="report_db",
+            help="Don't store report to database")
+    parser.add_argument("--report-path", required=False,
+            help="Where to store the reports (default: absent, don't store)")
+    parser.add_argument("--no-report-path", action='store_const', dest='report_path', const=None,
+            help="Don't store the report to file (default)")
+    parser.add_argument("--db-host", default="localhost",
+            help="Host access to database (default: localhost)")
+    parser.add_argument("--db-port", default=5432,
+            help="Port access to database (default: 5432)")
+    parser.add_argument("--db-user", default="cryptoportfolio",
+            help="User access to database (default: cryptoportfolio)")
+    parser.add_argument("--db-password", default="cryptoportfolio",
+            help="Password access to database (default: cryptoportfolio)")
+    parser.add_argument("--db-database", default="cryptoportfolio",
+            help="Database access to database (default: cryptoportfolio)")
 
-for row in cursor:
-    market_config, user_id = row
+    return parser.parse_args(argv)
+
+def process(market_config, market_id, user_id, args, pg_config):
     try:
-        user_market = market.get_market(market_config)
-        if args.before:
-            portfolio.h.process_sell_all__1_all_sell(user_market, debug=args.debug)
-        if args.after:
-            portfolio.Portfolio.wait_for_recent()
-            portfolio.h.process_sell_all__2_all_buy(user_market, debug=args.debug)
+        market.Market\
+                .from_config(market_config, args, market_id=market_id,
+                        pg_config=pg_config, user_id=user_id)\
+                .process(args.action, before=args.before, after=args.after)
     except Exception as e:
-        print(e)
-        pass
-    finally:
-        report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id)
-        with open(report_file, "w") as f:
-            f.write(portfolio.ReportStore.to_json())
-        portfolio.h.reset_all()
+        print("{}: {}".format(e.__class__.__name__, e))
+
+def main(argv):
+    args = parse_args(argv)
+
+    pg_config = parse_config(args)
+
+    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, pg_config)
 
+if __name__ == '__main__': # pragma: no cover
+    main(sys.argv[1:])