from datetime import datetime import argparse import configparser import psycopg2 import os import sys import portfolio def main_parse_args(argv): parser = argparse.ArgumentParser( description="Run the trade bot") 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") parser.add_argument("--user", default=None, required=False, help="Only run for that user") parser.add_argument("--action", default=None, required=False, help="Do a different action than trading") args = parser.parse_args(argv) if not os.path.exists(args.config): print("no config file found, exiting") sys.exit(1) return args def main_parse_config(config_file): config = configparser.ConfigParser() config.read(config_file) if "postgresql" not in config: print("no configuration for postgresql in config file") sys.exit(1) if "app" in config and "report_path" in config["app"]: report_path = config["app"]["report_path"] if not os.path.exists(report_path): os.makedirs(report_path) else: report_path = None return [config["postgresql"], report_path] def main_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") else: cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user) for row in cursor: yield row def main_process_market(user_market, action, before=False, after=False): if action is None: if before: process_sell_all__1_all_sell(user_market) if after: portfolio.Portfolio.wait_for_recent(user_market) process_sell_all__2_all_buy(user_market) elif action == "print_balances": print_balances(user_market) elif action == "print_orders": print_orders(user_market) else: raise NotImplementedError("Unknown action {}".format(action)) def main_store_report(report_path, user_id, user_market): try: if report_path is not None: report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id) with open(report_file, "w") as f: f.write(user_market.report.to_json()) except Exception as e: print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) def print_orders(market, base_currency="BTC"): market.report.log_stage("print_orders") market.balances.fetch_balances(tag="print_orders") market.prepare_trades(base_currency=base_currency, compute_value="average") market.trades.prepare_orders(compute_value="average") def print_balances(market, base_currency="BTC"): market.report.log_stage("print_balances") market.balances.fetch_balances() if base_currency is not None: market.report.print_log("total:") market.report.print_log(sum(market.balances.in_currency(base_currency).values())) def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC"): market.report.log_stage("process_sell_needed__1_sell_begin") market.balances.fetch_balances(tag="process_sell_needed__1_sell_begin") market.prepare_trades(liquidity=liquidity, base_currency=base_currency) market.trades.prepare_orders(compute_value="average", only="dispose") market.trades.run_orders() market.follow_orders() market.balances.fetch_balances(tag="process_sell_needed__1_sell_end") market.report.log_stage("process_sell_needed__1_sell_end") def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC"): market.report.log_stage("process_sell_needed__2_buy_begin") market.balances.fetch_balances(tag="process_sell_needed__2_buy_begin") market.update_trades(base_currency=base_currency, liquidity=liquidity, only="acquire") market.trades.prepare_orders(compute_value="average", only="acquire") market.move_balances() market.trades.run_orders() market.follow_orders() market.balances.fetch_balances(tag="process_sell_needed__2_buy_end") market.report.log_stage("process_sell_needed__2_buy_end") def process_sell_all__1_all_sell(market, base_currency="BTC", liquidity="medium"): market.report.log_stage("process_sell_all__1_all_sell_begin") market.balances.fetch_balances(tag="process_sell_all__1_all_sell_begin") market.prepare_trades_to_sell_all(base_currency=base_currency) market.trades.prepare_orders(compute_value="average") market.trades.run_orders() market.follow_orders() market.balances.fetch_balances(tag="process_sell_all__1_all_sell_end") market.report.log_stage("process_sell_all__1_all_sell_end") def process_sell_all__2_all_buy(market, base_currency="BTC", liquidity="medium"): market.report.log_stage("process_sell_all__2_all_buy_begin") market.balances.fetch_balances(tag="process_sell_all__2_all_buy_begin") market.prepare_trades(liquidity=liquidity, base_currency=base_currency) market.trades.prepare_orders(compute_value="average") market.move_balances() market.trades.run_orders() market.follow_orders() market.balances.fetch_balances(tag="process_sell_all__2_all_buy_end") market.report.log_stage("process_sell_all__2_all_buy_end")