diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-24 23:35:40 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-25 01:35:28 +0100 |
commit | eb9c92e155941b51042ba57e23f651454bd8e55a (patch) | |
tree | 8de8ed33394777aec4b538275ffa319368086bed | |
parent | 17ff995eb623dcaea579e33e507091d6169c52e5 (diff) | |
download | Trader-eb9c92e155941b51042ba57e23f651454bd8e55a.tar.gz Trader-eb9c92e155941b51042ba57e23f651454bd8e55a.tar.zst Trader-eb9c92e155941b51042ba57e23f651454bd8e55a.zip |
Add main running file and fetch information from database
-rw-r--r-- | config.ini | 9 | ||||
-rw-r--r-- | helper.py | 6 | ||||
-rw-r--r-- | main.py | 64 | ||||
-rw-r--r-- | market.py | 31 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | test.py | 10 |
6 files changed, 102 insertions, 19 deletions
diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..50cbd1b --- /dev/null +++ b/config.ini | |||
@@ -0,0 +1,9 @@ | |||
1 | [postgresql] | ||
2 | host = localhost | ||
3 | port = 5432 | ||
4 | user = cryptoportfolio | ||
5 | password = cryptoportfolio | ||
6 | database = cryptoportfolio | ||
7 | |||
8 | [app] | ||
9 | report_path = reports | ||
@@ -127,6 +127,12 @@ def print_balances(market, base_currency="BTC"): | |||
127 | ReportStore.print_log("total:") | 127 | ReportStore.print_log("total:") |
128 | ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values())) | 128 | ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values())) |
129 | 129 | ||
130 | def reset_all(): | ||
131 | # use them as regular classes, sub-object of market | ||
132 | ReportStore.logs = [] | ||
133 | BalanceStore.all = {} | ||
134 | TradeStore.all = [] | ||
135 | |||
130 | def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False): | 136 | def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False): |
131 | ReportStore.log_stage("process_sell_needed__1_sell_begin") | 137 | ReportStore.log_stage("process_sell_needed__1_sell_begin") |
132 | BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_begin") | 138 | BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_begin") |
@@ -0,0 +1,64 @@ | |||
1 | import os | ||
2 | import sys | ||
3 | import configparser | ||
4 | import psycopg2 | ||
5 | import argparse | ||
6 | from datetime import datetime | ||
7 | |||
8 | import portfolio, market | ||
9 | |||
10 | parser = argparse.ArgumentParser( | ||
11 | description="Run the trade bot") | ||
12 | |||
13 | parser.add_argument("-c", "--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: | ||
50 | user_market = market.get_market(market_config) | ||
51 | if args.before: | ||
52 | portfolio.h.process_sell_all__1_all_sell(user_market, debug=args.debug) | ||
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: | ||
57 | print(e) | ||
58 | pass | ||
59 | finally: | ||
60 | report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id) | ||
61 | with open(report_file, "w") as f: | ||
62 | f.write(portfolio.ReportStore.to_json()) | ||
63 | portfolio.h.reset_all() | ||
64 | |||
@@ -1,24 +1,17 @@ | |||
1 | import ccxt_wrapper as ccxt | 1 | import ccxt_wrapper as ccxt |
2 | from store import ReportStore | ||
2 | 3 | ||
3 | market = ccxt.poloniexE({ | 4 | def get_market(config): |
4 | "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX", | 5 | market = ccxt.poloniexE(config) |
5 | "secret": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", | ||
6 | }) | ||
7 | 6 | ||
7 | # For requests logging | ||
8 | market.session.origin_request = market.session.request | ||
8 | 9 | ||
9 | # For requests logging | 10 | def request_wrap(self, *args, **kwargs): |
10 | requests = [] | 11 | r = self.origin_request(*args, **kwargs) |
11 | market.session.origin_request = market.session.request | 12 | ReportStore.log_http_request(args[0], args[1], kwargs["data"], |
13 | kwargs["headers"], r) | ||
14 | return r | ||
15 | market.session.request = request_wrap.__get__(market.session, market.session.__class__) | ||
12 | 16 | ||
13 | def request_wrap(self, *args, **kwargs): | 17 | return market |
14 | r = self.origin_request(*args, **kwargs) | ||
15 | requests.append({ | ||
16 | "method": args[0], | ||
17 | "url": args[1], | ||
18 | "body": kwargs["data"], | ||
19 | "headers": kwargs["headers"], | ||
20 | "status": r.status_code, | ||
21 | "response": r.text, | ||
22 | }) | ||
23 | return r | ||
24 | market.session.request = request_wrap.__get__(market.session, market.session.__class__) | ||
diff --git a/requirements.txt b/requirements.txt index 0eea7a4..8e78c05 100644 --- a/requirements.txt +++ b/requirements.txt | |||
@@ -2,3 +2,4 @@ ccxt>=1.10.593 | |||
2 | simplejson>=3.13.2 | 2 | simplejson>=3.13.2 |
3 | requests>=2.11.1 | 3 | requests>=2.11.1 |
4 | requests_mock>=1.4.0 | 4 | requests_mock>=1.4.0 |
5 | psycopg2>=2.7 | ||
@@ -1075,6 +1075,16 @@ class HelperTest(WebMockTestCase): | |||
1075 | follow_orders.assert_called() | 1075 | follow_orders.assert_called() |
1076 | log_stage.assert_called_with("process_sell_all__2_all_buy_end") | 1076 | log_stage.assert_called_with("process_sell_all__2_all_buy_end") |
1077 | 1077 | ||
1078 | def test_reset_all(self): | ||
1079 | portfolio.BalanceStore.all = { "foo": "bar" } | ||
1080 | portfolio.ReportStore.logs.append("hey") | ||
1081 | portfolio.TradeStore.all.append("bouh") | ||
1082 | |||
1083 | helper.reset_all() | ||
1084 | |||
1085 | self.assertEqual(0, len(portfolio.BalanceStore.all)) | ||
1086 | self.assertEqual(0, len(portfolio.ReportStore.logs)) | ||
1087 | self.assertEqual(0, len(portfolio.TradeStore.all)) | ||
1078 | 1088 | ||
1079 | @unittest.skipUnless("unit" in limits, "Unit skipped") | 1089 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
1080 | class TradeStoreTest(WebMockTestCase): | 1090 | class TradeStoreTest(WebMockTestCase): |