aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: 41abe9e740187362c6e8baa5f8e2d6179c19add2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import sys
import configparser
import psycopg2
import argparse
from datetime import datetime

import portfolio, market

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")

args = parser.parse_args()

if not os.path.exists(args.config):
    print("no config file found, exiting")
    sys.exit(1)

config = configparser.ConfigParser()
config.read(args.config)

pg_config = config["postgresql"]

connection = psycopg2.connect(**pg_config)
cursor = connection.cursor()

cursor.execute("SELECT config,user_id FROM market_configs")

report_path = config["app"]["report_path"]
if not os.path.exists(report_path):
    os.makedirs(report_path)

for row in cursor:
    market_config, user_id = row
    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)
    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()