diff options
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 66 |
1 files changed, 9 insertions, 57 deletions
@@ -1,64 +1,16 @@ | |||
1 | import os | ||
2 | import sys | 1 | import sys |
3 | import configparser | 2 | import helper, market |
4 | import psycopg2 | ||
5 | import argparse | ||
6 | from datetime import datetime | ||
7 | 3 | ||
8 | import portfolio, market | 4 | args = helper.main_parse_args(sys.argv[1:]) |
9 | 5 | ||
10 | parser = argparse.ArgumentParser( | 6 | pg_config, report_path = helper.main_parse_config(args.config) |
11 | description="Run the trade bot") | ||
12 | 7 | ||
13 | parser.add_argument("-c", "--config", | 8 | for market_config, user_id in helper.main_fetch_markets(pg_config): |
14 | default="config.ini", | ||
15 | required=False, | ||
16 | help="Config file to load (default: config.ini)") | ||
17 | parser.add_argument("--before", | ||
18 | default=False, action='store_const', const=True, | ||
19 | help="Run the steps before the cryptoportfolio update") | ||
20 | parser.add_argument("--after", | ||
21 | default=False, action='store_const', const=True, | ||
22 | help="Run the steps after the cryptoportfolio update") | ||
23 | parser.add_argument("--debug", | ||
24 | default=False, action='store_const', const=True, | ||
25 | help="Run in debug mode") | ||
26 | |||
27 | args = parser.parse_args() | ||
28 | |||
29 | if not os.path.exists(args.config): | ||
30 | print("no config file found, exiting") | ||
31 | sys.exit(1) | ||
32 | |||
33 | config = configparser.ConfigParser() | ||
34 | config.read(args.config) | ||
35 | |||
36 | pg_config = config["postgresql"] | ||
37 | |||
38 | connection = psycopg2.connect(**pg_config) | ||
39 | cursor = connection.cursor() | ||
40 | |||
41 | cursor.execute("SELECT config,user_id FROM market_configs") | ||
42 | |||
43 | report_path = config["app"]["report_path"] | ||
44 | if not os.path.exists(report_path): | ||
45 | os.makedirs(report_path) | ||
46 | |||
47 | for row in cursor: | ||
48 | market_config, user_id = row | ||
49 | try: | 9 | try: |
50 | user_market = market.get_market(market_config) | 10 | market_config["apiKey"] = market_config.pop("key") |
51 | if args.before: | 11 | user_market = market.Market.from_config(market_config, debug=args.debug) |
52 | portfolio.h.process_sell_all__1_all_sell(user_market, debug=args.debug) | 12 | helper.main_process_market(user_market, before=args.before, after=args.after) |
53 | if args.after: | ||
54 | portfolio.Portfolio.wait_for_recent() | ||
55 | portfolio.h.process_sell_all__2_all_buy(user_market, debug=args.debug) | ||
56 | except Exception as e: | 13 | except Exception as e: |
57 | print(e) | 14 | print("{}: {}".format(e.__class__.__name__, e)) |
58 | pass | ||
59 | finally: | 15 | finally: |
60 | report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id) | 16 | helper.main_store_report(report_path, user_id, user_market) |
61 | with open(report_file, "w") as f: | ||
62 | f.write(portfolio.ReportStore.to_json()) | ||
63 | portfolio.h.reset_all() | ||
64 | |||