]>
Commit | Line | Data |
---|---|---|
f86ee140 IB |
1 | from datetime import datetime |
2 | import argparse | |
3 | import configparser | |
4 | import psycopg2 | |
5 | import os | |
6 | import sys | |
7 | ||
8 | import portfolio | |
9 | ||
10 | def main_parse_args(argv): | |
11 | parser = argparse.ArgumentParser( | |
12 | description="Run the trade bot") | |
13 | ||
14 | parser.add_argument("-c", "--config", | |
15 | default="config.ini", | |
16 | required=False, | |
17 | help="Config file to load (default: config.ini)") | |
18 | parser.add_argument("--before", | |
19 | default=False, action='store_const', const=True, | |
20 | help="Run the steps before the cryptoportfolio update") | |
21 | parser.add_argument("--after", | |
22 | default=False, action='store_const', const=True, | |
23 | help="Run the steps after the cryptoportfolio update") | |
24 | parser.add_argument("--debug", | |
25 | default=False, action='store_const', const=True, | |
26 | help="Run in debug mode") | |
27 | ||
28 | args = parser.parse_args(argv) | |
29 | ||
30 | if not os.path.exists(args.config): | |
31 | print("no config file found, exiting") | |
32 | sys.exit(1) | |
33 | ||
34 | return args | |
35 | ||
36 | def main_parse_config(config_file): | |
37 | config = configparser.ConfigParser() | |
38 | config.read(config_file) | |
39 | ||
40 | if "postgresql" not in config: | |
41 | print("no configuration for postgresql in config file") | |
42 | sys.exit(1) | |
43 | ||
44 | if "app" in config and "report_path" in config["app"]: | |
45 | report_path = config["app"]["report_path"] | |
46 | ||
47 | if not os.path.exists(report_path): | |
48 | os.makedirs(report_path) | |
49 | else: | |
50 | report_path = None | |
51 | ||
52 | return [config["postgresql"], report_path] | |
53 | ||
54 | def main_fetch_markets(pg_config): | |
55 | connection = psycopg2.connect(**pg_config) | |
56 | cursor = connection.cursor() | |
57 | ||
58 | cursor.execute("SELECT config,user_id FROM market_configs") | |
59 | ||
60 | for row in cursor: | |
61 | yield row | |
62 | ||
63 | def main_process_market(user_market, before=False, after=False): | |
64 | if before: | |
65 | process_sell_all__1_all_sell(user_market) | |
66 | if after: | |
67 | portfolio.Portfolio.wait_for_recent(user_market) | |
68 | process_sell_all__2_all_buy(user_market) | |
69 | ||
70 | def main_store_report(report_path, user_id, user_market): | |
6ca5a1ec | 71 | try: |
f86ee140 IB |
72 | if report_path is not None: |
73 | report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id) | |
74 | with open(report_file, "w") as f: | |
75 | f.write(user_market.report.to_json()) | |
76 | except Exception as e: | |
77 | print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) | |
6ca5a1ec IB |
78 | |
79 | def print_orders(market, base_currency="BTC"): | |
f86ee140 IB |
80 | market.report.log_stage("print_orders") |
81 | market.balances.fetch_balances(tag="print_orders") | |
82 | market.prepare_trades(base_currency=base_currency, compute_value="average") | |
83 | market.trades.prepare_orders(compute_value="average") | |
6ca5a1ec | 84 | |
97922ff1 | 85 | def print_balances(market, base_currency="BTC"): |
f86ee140 | 86 | market.balances.fetch_balances() |
97922ff1 | 87 | if base_currency is not None: |
f86ee140 IB |
88 | market.report.print_log("total:") |
89 | market.report.print_log(sum(market.balances.in_currency(base_currency).values())) | |
90 | ||
91 | def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC"): | |
92 | market.report.log_stage("process_sell_needed__1_sell_begin") | |
93 | market.balances.fetch_balances(tag="process_sell_needed__1_sell_begin") | |
94 | market.prepare_trades(liquidity=liquidity, base_currency=base_currency) | |
95 | market.trades.prepare_orders(compute_value="average", only="dispose") | |
96 | market.trades.run_orders() | |
97 | market.follow_orders() | |
98 | market.balances.fetch_balances(tag="process_sell_needed__1_sell_end") | |
99 | market.report.log_stage("process_sell_needed__1_sell_end") | |
100 | ||
101 | def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC"): | |
102 | market.report.log_stage("process_sell_needed__2_buy_begin") | |
103 | market.balances.fetch_balances(tag="process_sell_needed__2_buy_begin") | |
104 | market.update_trades(base_currency=base_currency, liquidity=liquidity, only="acquire") | |
105 | market.trades.prepare_orders(compute_value="average", only="acquire") | |
106 | market.move_balances() | |
107 | market.trades.run_orders() | |
108 | market.follow_orders() | |
109 | market.balances.fetch_balances(tag="process_sell_needed__2_buy_end") | |
110 | market.report.log_stage("process_sell_needed__2_buy_end") | |
111 | ||
112 | def process_sell_all__1_all_sell(market, base_currency="BTC", liquidity="medium"): | |
113 | market.report.log_stage("process_sell_all__1_all_sell_begin") | |
114 | market.balances.fetch_balances(tag="process_sell_all__1_all_sell_begin") | |
115 | market.prepare_trades_to_sell_all(base_currency=base_currency) | |
116 | market.trades.prepare_orders(compute_value="average") | |
117 | market.trades.run_orders() | |
118 | market.follow_orders() | |
119 | market.balances.fetch_balances(tag="process_sell_all__1_all_sell_end") | |
120 | market.report.log_stage("process_sell_all__1_all_sell_end") | |
121 | ||
122 | def process_sell_all__2_all_buy(market, base_currency="BTC", liquidity="medium"): | |
123 | market.report.log_stage("process_sell_all__2_all_buy_begin") | |
124 | market.balances.fetch_balances(tag="process_sell_all__2_all_buy_begin") | |
125 | market.prepare_trades(liquidity=liquidity, base_currency=base_currency) | |
126 | market.trades.prepare_orders(compute_value="average") | |
127 | market.move_balances() | |
128 | market.trades.run_orders() | |
129 | market.follow_orders() | |
130 | market.balances.fetch_balances(tag="process_sell_all__2_all_buy_end") | |
131 | market.report.log_stage("process_sell_all__2_all_buy_end") | |
6ca5a1ec IB |
132 | |
133 |