]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blob - helper.py
Add make_order and get_user_market helpers
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / helper.py
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 make_order(market, value, currency, action="acquire",
11 close_if_possible=False, base_currency="BTC", follow=True,
12 compute_value="average"):
13 """
14 Make an order on market
15 "market": The market on which to place the order
16 "value": The value in *base_currency* to acquire,
17 or in *currency* to dispose.
18 use negative for margin trade.
19 "action": "acquire" or "dispose".
20 "acquire" will buy long or sell short,
21 "dispose" will sell long or buy short.
22 "currency": The currency to acquire or dispose
23 "base_currency": The base currency. The value is expressed in that
24 currency (default: BTC)
25 "follow": Whether to follow the order once run (default: True)
26 "close_if_possible": Whether to try to close the position at the end
27 of the trade, i.e. reach exactly 0 at the end
28 (only meaningful in "dispose"). May have
29 unwanted effects if the end value of the
30 currency is not 0.
31 "compute_value": Compute value to place the order
32 """
33 market.report.log_stage("make_order_begin")
34 market.balances.fetch_balances(tag="make_order_begin")
35 if action == "acquire":
36 trade = portfolio.Trade(
37 portfolio.Amount(base_currency, 0),
38 portfolio.Amount(base_currency, value),
39 currency, market)
40 else:
41 amount = portfolio.Amount(currency, value)
42 trade = portfolio.Trade(
43 amount.in_currency(base_currency, market, compute_value=compute_value),
44 portfolio.Amount(base_currency, 0),
45 currency, market)
46 market.trades.all.append(trade)
47 order = trade.prepare_order(
48 close_if_possible=close_if_possible,
49 compute_value=compute_value)
50 market.report.log_orders([order], None, compute_value)
51 market.trades.run_orders()
52 if follow:
53 market.follow_orders()
54 market.balances.fetch_balances(tag="make_order_end")
55 else:
56 market.report.log_stage("make_order_end_not_followed")
57 return order
58 market.report.log_stage("make_order_end")
59
60 def get_user_market(config_path, user_id, debug=False):
61 import market
62 pg_config, report_path = main_parse_config(config_path)
63 market_config = list(main_fetch_markets(pg_config, str(user_id)))[0][0]
64 return market.Market.from_config(market_config, debug=debug)
65
66 def main_parse_args(argv):
67 parser = argparse.ArgumentParser(
68 description="Run the trade bot")
69
70 parser.add_argument("-c", "--config",
71 default="config.ini",
72 required=False,
73 help="Config file to load (default: config.ini)")
74 parser.add_argument("--before",
75 default=False, action='store_const', const=True,
76 help="Run the steps before the cryptoportfolio update")
77 parser.add_argument("--after",
78 default=False, action='store_const', const=True,
79 help="Run the steps after the cryptoportfolio update")
80 parser.add_argument("--debug",
81 default=False, action='store_const', const=True,
82 help="Run in debug mode")
83 parser.add_argument("--user",
84 default=None, required=False, help="Only run for that user")
85 parser.add_argument("--action",
86 default=None, required=False,
87 help="Do a different action than trading")
88
89 args = parser.parse_args(argv)
90
91 if not os.path.exists(args.config):
92 print("no config file found, exiting")
93 sys.exit(1)
94
95 return args
96
97 def main_parse_config(config_file):
98 config = configparser.ConfigParser()
99 config.read(config_file)
100
101 if "postgresql" not in config:
102 print("no configuration for postgresql in config file")
103 sys.exit(1)
104
105 if "app" in config and "report_path" in config["app"]:
106 report_path = config["app"]["report_path"]
107
108 if not os.path.exists(report_path):
109 os.makedirs(report_path)
110 else:
111 report_path = None
112
113 return [config["postgresql"], report_path]
114
115 def main_fetch_markets(pg_config, user):
116 connection = psycopg2.connect(**pg_config)
117 cursor = connection.cursor()
118
119 if user is None:
120 cursor.execute("SELECT config,user_id FROM market_configs")
121 else:
122 cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user)
123
124 for row in cursor:
125 yield row
126
127 def main_process_market(user_market, action, before=False, after=False):
128 if action is None:
129 if before:
130 process_sell_all__1_all_sell(user_market)
131 if after:
132 portfolio.Portfolio.wait_for_recent(user_market)
133 process_sell_all__2_all_buy(user_market)
134 elif action == "print_balances":
135 print_balances(user_market)
136 elif action == "print_orders":
137 print_orders(user_market)
138 else:
139 raise NotImplementedError("Unknown action {}".format(action))
140
141 def main_store_report(report_path, user_id, user_market):
142 try:
143 if report_path is not None:
144 report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id)
145 with open(report_file, "w") as f:
146 f.write(user_market.report.to_json())
147 except Exception as e:
148 print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
149
150 def print_orders(market, base_currency="BTC"):
151 market.report.log_stage("print_orders")
152 market.balances.fetch_balances(tag="print_orders")
153 market.prepare_trades(base_currency=base_currency, compute_value="average")
154 market.trades.prepare_orders(compute_value="average")
155
156 def print_balances(market, base_currency="BTC"):
157 market.report.log_stage("print_balances")
158 market.balances.fetch_balances()
159 if base_currency is not None:
160 market.report.print_log("total:")
161 market.report.print_log(sum(market.balances.in_currency(base_currency).values()))
162
163 def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC"):
164 market.report.log_stage("process_sell_needed__1_sell_begin")
165 market.balances.fetch_balances(tag="process_sell_needed__1_sell_begin")
166 market.prepare_trades(liquidity=liquidity, base_currency=base_currency)
167 market.trades.prepare_orders(compute_value="average", only="dispose")
168 market.trades.run_orders()
169 market.follow_orders()
170 market.balances.fetch_balances(tag="process_sell_needed__1_sell_end")
171 market.report.log_stage("process_sell_needed__1_sell_end")
172
173 def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC"):
174 market.report.log_stage("process_sell_needed__2_buy_begin")
175 market.balances.fetch_balances(tag="process_sell_needed__2_buy_begin")
176 market.update_trades(base_currency=base_currency, liquidity=liquidity, only="acquire")
177 market.trades.prepare_orders(compute_value="average", only="acquire")
178 market.move_balances()
179 market.trades.run_orders()
180 market.follow_orders()
181 market.balances.fetch_balances(tag="process_sell_needed__2_buy_end")
182 market.report.log_stage("process_sell_needed__2_buy_end")
183
184 def process_sell_all__1_all_sell(market, base_currency="BTC", liquidity="medium"):
185 market.report.log_stage("process_sell_all__1_all_sell_begin")
186 market.balances.fetch_balances(tag="process_sell_all__1_all_sell_begin")
187 market.prepare_trades_to_sell_all(base_currency=base_currency)
188 market.trades.prepare_orders(compute_value="average")
189 market.trades.run_orders()
190 market.follow_orders()
191 market.balances.fetch_balances(tag="process_sell_all__1_all_sell_end")
192 market.report.log_stage("process_sell_all__1_all_sell_end")
193
194 def process_sell_all__2_all_buy(market, base_currency="BTC", liquidity="medium"):
195 market.report.log_stage("process_sell_all__2_all_buy_begin")
196 market.balances.fetch_balances(tag="process_sell_all__2_all_buy_begin")
197 market.prepare_trades(liquidity=liquidity, base_currency=base_currency)
198 market.trades.prepare_orders(compute_value="average")
199 market.move_balances()
200 market.trades.run_orders()
201 market.follow_orders()
202 market.balances.fetch_balances(tag="process_sell_all__2_all_buy_end")
203 market.report.log_stage("process_sell_all__2_all_buy_end")
204
205