aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-02-24 23:35:40 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-02-25 01:35:28 +0100
commiteb9c92e155941b51042ba57e23f651454bd8e55a (patch)
tree8de8ed33394777aec4b538275ffa319368086bed
parent17ff995eb623dcaea579e33e507091d6169c52e5 (diff)
downloadTrader-eb9c92e155941b51042ba57e23f651454bd8e55a.tar.gz
Trader-eb9c92e155941b51042ba57e23f651454bd8e55a.tar.zst
Trader-eb9c92e155941b51042ba57e23f651454bd8e55a.zip
Add main running file and fetch information from database
-rw-r--r--config.ini9
-rw-r--r--helper.py6
-rw-r--r--main.py64
-rw-r--r--market.py31
-rw-r--r--requirements.txt1
-rw-r--r--test.py10
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]
2host = localhost
3port = 5432
4user = cryptoportfolio
5password = cryptoportfolio
6database = cryptoportfolio
7
8[app]
9report_path = reports
diff --git a/helper.py b/helper.py
index d9c69cc..fa92ac7 100644
--- a/helper.py
+++ b/helper.py
@@ -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
130def reset_all():
131 # use them as regular classes, sub-object of market
132 ReportStore.logs = []
133 BalanceStore.all = {}
134 TradeStore.all = []
135
130def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False): 136def 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")
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..41abe9e
--- /dev/null
+++ b/main.py
@@ -0,0 +1,64 @@
1import os
2import sys
3import configparser
4import psycopg2
5import argparse
6from datetime import datetime
7
8import portfolio, market
9
10parser = argparse.ArgumentParser(
11 description="Run the trade bot")
12
13parser.add_argument("-c", "--config",
14 default="config.ini",
15 required=False,
16 help="Config file to load (default: config.ini)")
17parser.add_argument("--before",
18 default=False, action='store_const', const=True,
19 help="Run the steps before the cryptoportfolio update")
20parser.add_argument("--after",
21 default=False, action='store_const', const=True,
22 help="Run the steps after the cryptoportfolio update")
23parser.add_argument("--debug",
24 default=False, action='store_const', const=True,
25 help="Run in debug mode")
26
27args = parser.parse_args()
28
29if not os.path.exists(args.config):
30 print("no config file found, exiting")
31 sys.exit(1)
32
33config = configparser.ConfigParser()
34config.read(args.config)
35
36pg_config = config["postgresql"]
37
38connection = psycopg2.connect(**pg_config)
39cursor = connection.cursor()
40
41cursor.execute("SELECT config,user_id FROM market_configs")
42
43report_path = config["app"]["report_path"]
44if not os.path.exists(report_path):
45 os.makedirs(report_path)
46
47for 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
diff --git a/market.py b/market.py
index 08838a7..224cc32 100644
--- a/market.py
+++ b/market.py
@@ -1,24 +1,17 @@
1import ccxt_wrapper as ccxt 1import ccxt_wrapper as ccxt
2from store import ReportStore
2 3
3market = ccxt.poloniexE({ 4def 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):
10requests = [] 11 r = self.origin_request(*args, **kwargs)
11market.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
13def 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
24market.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
2simplejson>=3.13.2 2simplejson>=3.13.2
3requests>=2.11.1 3requests>=2.11.1
4requests_mock>=1.4.0 4requests_mock>=1.4.0
5psycopg2>=2.7
diff --git a/test.py b/test.py
index 34b2fe3..fc331c3 100644
--- a/test.py
+++ b/test.py
@@ -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")
1080class TradeStoreTest(WebMockTestCase): 1090class TradeStoreTest(WebMockTestCase):