]>
Commit | Line | Data |
---|---|---|
1 | import configargparse | |
2 | import psycopg2 | |
3 | import os | |
4 | import sys | |
5 | ||
6 | import market | |
7 | import portfolio | |
8 | ||
9 | __all__ = ["make_order", "get_user_market"] | |
10 | ||
11 | def make_order(market, value, currency, action="acquire", | |
12 | close_if_possible=False, base_currency="BTC", follow=True, | |
13 | compute_value="average"): | |
14 | """ | |
15 | Make an order on market | |
16 | "market": The market on which to place the order | |
17 | "value": The value in *base_currency* to acquire, | |
18 | or in *currency* to dispose. | |
19 | use negative for margin trade. | |
20 | "action": "acquire" or "dispose". | |
21 | "acquire" will buy long or sell short, | |
22 | "dispose" will sell long or buy short. | |
23 | "currency": The currency to acquire or dispose | |
24 | "base_currency": The base currency. The value is expressed in that | |
25 | currency (default: BTC) | |
26 | "follow": Whether to follow the order once run (default: True) | |
27 | "close_if_possible": Whether to try to close the position at the end | |
28 | of the trade, i.e. reach exactly 0 at the end | |
29 | (only meaningful in "dispose"). May have | |
30 | unwanted effects if the end value of the | |
31 | currency is not 0. | |
32 | "compute_value": Compute value to place the order | |
33 | """ | |
34 | market.report.log_stage("make_order_begin") | |
35 | market.balances.fetch_balances(tag="make_order_begin") | |
36 | if action == "acquire": | |
37 | trade = portfolio.Trade( | |
38 | portfolio.Amount(base_currency, 0), | |
39 | portfolio.Amount(base_currency, value), | |
40 | currency, market) | |
41 | else: | |
42 | amount = portfolio.Amount(currency, value) | |
43 | trade = portfolio.Trade( | |
44 | amount.in_currency(base_currency, market, compute_value=compute_value), | |
45 | portfolio.Amount(base_currency, 0), | |
46 | currency, market) | |
47 | market.trades.all.append(trade) | |
48 | order = trade.prepare_order( | |
49 | close_if_possible=close_if_possible, | |
50 | compute_value=compute_value) | |
51 | market.report.log_orders([order], None, compute_value) | |
52 | market.trades.run_orders() | |
53 | if follow: | |
54 | market.follow_orders() | |
55 | market.balances.fetch_balances(tag="make_order_end") | |
56 | else: | |
57 | market.report.log_stage("make_order_end_not_followed") | |
58 | return order | |
59 | market.report.log_stage("make_order_end") | |
60 | ||
61 | def get_user_market(config_path, user_id, debug=False): | |
62 | args = ["--config", config_path] | |
63 | if debug: | |
64 | args.append("--debug") | |
65 | args = parse_args(args) | |
66 | pg_config = parse_config(args) | |
67 | market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0] | |
68 | return market.Market.from_config(market_config, args, | |
69 | pg_config=pg_config, market_id=market_id, | |
70 | user_id=user_id) | |
71 | ||
72 | def fetch_markets(pg_config, user): | |
73 | connection = psycopg2.connect(**pg_config) | |
74 | cursor = connection.cursor() | |
75 | ||
76 | if user is None: | |
77 | cursor.execute("SELECT id,config,user_id FROM market_configs") | |
78 | else: | |
79 | cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user) | |
80 | ||
81 | for row in cursor: | |
82 | yield row | |
83 | ||
84 | def parse_config(args): | |
85 | pg_config = { | |
86 | "host": args.db_host, | |
87 | "port": args.db_port, | |
88 | "user": args.db_user, | |
89 | "password": args.db_password, | |
90 | "database": args.db_database, | |
91 | } | |
92 | del(args.db_host) | |
93 | del(args.db_port) | |
94 | del(args.db_user) | |
95 | del(args.db_password) | |
96 | del(args.db_database) | |
97 | ||
98 | redis_config = { | |
99 | "host": args.redis_host, | |
100 | "port": args.redis_port, | |
101 | "db": args.redis_database, | |
102 | } | |
103 | if redis_config["host"].startswith("/"): | |
104 | redis_config["unix_socket_path"] = redis_config.pop("host") | |
105 | del(redis_config["port"]) | |
106 | del(args.redis_host) | |
107 | del(args.redis_port) | |
108 | del(args.redis_database) | |
109 | ||
110 | report_path = args.report_path | |
111 | ||
112 | if report_path is not None and not \ | |
113 | os.path.exists(report_path): | |
114 | os.makedirs(report_path) | |
115 | ||
116 | return pg_config, redis_config | |
117 | ||
118 | def parse_args(argv): | |
119 | parser = configargparse.ArgumentParser( | |
120 | description="Run the trade bot.") | |
121 | ||
122 | parser.add_argument("-c", "--config", | |
123 | default="config.ini", | |
124 | required=False, is_config_file=True, | |
125 | help="Config file to load (default: config.ini)") | |
126 | parser.add_argument("--before", | |
127 | default=False, action='store_const', const=True, | |
128 | help="Run the steps before the cryptoportfolio update") | |
129 | parser.add_argument("--after", | |
130 | default=False, action='store_const', const=True, | |
131 | help="Run the steps after the cryptoportfolio update") | |
132 | parser.add_argument("--quiet", | |
133 | default=False, action='store_const', const=True, | |
134 | help="Don't print messages") | |
135 | parser.add_argument("--debug", | |
136 | default=False, action='store_const', const=True, | |
137 | help="Run in debug mode") | |
138 | parser.add_argument("--user", | |
139 | default=None, required=False, help="Only run for that user") | |
140 | parser.add_argument("--action", | |
141 | action='append', | |
142 | help="Do a different action than trading (add several times to chain)") | |
143 | parser.add_argument("--parallel", action='store_true', default=True, dest="parallel") | |
144 | parser.add_argument("--no-parallel", action='store_false', dest="parallel") | |
145 | parser.add_argument("--report-db", action='store_true', default=True, dest="report_db", | |
146 | help="Store report to database (default)") | |
147 | parser.add_argument("--no-report-db", action='store_false', dest="report_db", | |
148 | help="Don't store report to database") | |
149 | parser.add_argument("--report-redis", action='store_true', default=False, dest="report_redis", | |
150 | help="Store report to redis") | |
151 | parser.add_argument("--no-report-redis", action='store_false', dest="report_redis", | |
152 | help="Don't store report to redis (default)") | |
153 | parser.add_argument("--report-path", required=False, | |
154 | help="Where to store the reports (default: absent, don't store)") | |
155 | parser.add_argument("--no-report-path", action='store_const', dest='report_path', const=None, | |
156 | help="Don't store the report to file (default)") | |
157 | parser.add_argument("--db-host", default="localhost", | |
158 | help="Host access to database (default: localhost)") | |
159 | parser.add_argument("--db-port", default=5432, | |
160 | help="Port access to database (default: 5432)") | |
161 | parser.add_argument("--db-user", default="cryptoportfolio", | |
162 | help="User access to database (default: cryptoportfolio)") | |
163 | parser.add_argument("--db-password", default="cryptoportfolio", | |
164 | help="Password access to database (default: cryptoportfolio)") | |
165 | parser.add_argument("--db-database", default="cryptoportfolio", | |
166 | help="Database access to database (default: cryptoportfolio)") | |
167 | parser.add_argument("--redis-host", default="localhost", | |
168 | help="Host access to database (default: localhost). Use path for socket") | |
169 | parser.add_argument("--redis-port", default=6379, | |
170 | help="Port access to redis (default: 6379)") | |
171 | parser.add_argument("--redis-database", default=0, | |
172 | help="Redis database to use (default: 0)") | |
173 | ||
174 | parsed = parser.parse_args(argv) | |
175 | if parsed.action is None: | |
176 | parsed.action = ["sell_all"] | |
177 | return parsed | |
178 | ||
179 | def process(market_config, market_id, user_id, args, pg_config, redis_config): | |
180 | try: | |
181 | market.Market\ | |
182 | .from_config(market_config, args, market_id=market_id, | |
183 | pg_config=pg_config, redis_config=redis_config, | |
184 | user_id=user_id)\ | |
185 | .process(args.action, before=args.before, after=args.after) | |
186 | except Exception as e: | |
187 | print("{}: {}".format(e.__class__.__name__, e)) | |
188 | ||
189 | def main(argv): | |
190 | args = parse_args(argv) | |
191 | ||
192 | pg_config, redis_config = parse_config(args) | |
193 | ||
194 | market.Portfolio.report.set_verbose(not args.quiet) | |
195 | ||
196 | if args.parallel: | |
197 | import threading | |
198 | market.Portfolio.start_worker() | |
199 | ||
200 | threads = [] | |
201 | def process_(*args): | |
202 | thread = threading.Thread(target=process, args=args) | |
203 | thread.start() | |
204 | threads.append(thread) | |
205 | else: | |
206 | process_ = process | |
207 | ||
208 | for market_id, market_config, user_id in fetch_markets(pg_config, args.user): | |
209 | process_(market_config, market_id, user_id, args, pg_config, redis_config) | |
210 | ||
211 | if args.parallel: | |
212 | for thread in threads: | |
213 | thread.join() | |
214 | market.Portfolio.stop_worker() | |
215 | ||
216 | if __name__ == '__main__': # pragma: no cover | |
217 | main(sys.argv[1:]) |