]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Add main running file and fetch information from database
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 24 Feb 2018 22:35:40 +0000 (23:35 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 25 Feb 2018 00:35:28 +0000 (01:35 +0100)
config.ini [new file with mode: 0644]
helper.py
main.py [new file with mode: 0644]
market.py
requirements.txt
test.py

diff --git a/config.ini b/config.ini
new file mode 100644 (file)
index 0000000..50cbd1b
--- /dev/null
@@ -0,0 +1,9 @@
+[postgresql]
+host = localhost
+port = 5432
+user = cryptoportfolio
+password = cryptoportfolio
+database = cryptoportfolio
+
+[app]
+report_path = reports
index d9c69cc45ee80e2a658db164cf03ea95c762189a..fa92ac71ab631d78f36ffeacc4ad1d94bfecb63f 100644 (file)
--- a/helper.py
+++ b/helper.py
@@ -127,6 +127,12 @@ def print_balances(market, base_currency="BTC"):
         ReportStore.print_log("total:")
         ReportStore.print_log(sum(BalanceStore.in_currency(base_currency, market).values()))
 
+def reset_all():
+    # use them as regular classes, sub-object of market
+    ReportStore.logs = []
+    BalanceStore.all = {}
+    TradeStore.all = []
+
 def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC", debug=False):
     ReportStore.log_stage("process_sell_needed__1_sell_begin")
     BalanceStore.fetch_balances(market, tag="process_sell_needed__1_sell_begin")
diff --git a/main.py b/main.py
new file mode 100644 (file)
index 0000000..41abe9e
--- /dev/null
+++ b/main.py
@@ -0,0 +1,64 @@
+import os
+import sys
+import configparser
+import psycopg2
+import argparse
+from datetime import datetime
+
+import portfolio, market
+
+parser = argparse.ArgumentParser(
+        description="Run the trade bot")
+
+parser.add_argument("-c", "--config",
+        default="config.ini",
+        required=False,
+        help="Config file to load (default: config.ini)")
+parser.add_argument("--before",
+        default=False, action='store_const', const=True,
+        help="Run the steps before the cryptoportfolio update")
+parser.add_argument("--after",
+        default=False, action='store_const', const=True,
+        help="Run the steps after the cryptoportfolio update")
+parser.add_argument("--debug",
+        default=False, action='store_const', const=True,
+        help="Run in debug mode")
+
+args = parser.parse_args()
+
+if not os.path.exists(args.config):
+    print("no config file found, exiting")
+    sys.exit(1)
+
+config = configparser.ConfigParser()
+config.read(args.config)
+
+pg_config = config["postgresql"]
+
+connection = psycopg2.connect(**pg_config)
+cursor = connection.cursor()
+
+cursor.execute("SELECT config,user_id FROM market_configs")
+
+report_path = config["app"]["report_path"]
+if not os.path.exists(report_path):
+    os.makedirs(report_path)
+
+for row in cursor:
+    market_config, user_id = row
+    try:
+        user_market = market.get_market(market_config)
+        if args.before:
+            portfolio.h.process_sell_all__1_all_sell(user_market, debug=args.debug)
+        if args.after:
+            portfolio.Portfolio.wait_for_recent()
+            portfolio.h.process_sell_all__2_all_buy(user_market, debug=args.debug)
+    except Exception as e:
+        print(e)
+        pass
+    finally:
+        report_file = "{}/{}_{}.json".format(report_path, datetime.now().isoformat(), user_id)
+        with open(report_file, "w") as f:
+            f.write(portfolio.ReportStore.to_json())
+        portfolio.h.reset_all()
+
index 08838a7f971537975037790a63ef665cd0a82c05..224cc32bae3b19b51f20cc2ee2158062664f86ad 100644 (file)
--- a/market.py
+++ b/market.py
@@ -1,24 +1,17 @@
 import ccxt_wrapper as ccxt
+from store import ReportStore
 
-market = ccxt.poloniexE({
-    "apiKey": "XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX",
-    "secret": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
-    })
+def get_market(config):
+    market = ccxt.poloniexE(config)
 
+    # For requests logging
+    market.session.origin_request = market.session.request
 
-# For requests logging
-requests = []
-market.session.origin_request = market.session.request
+    def request_wrap(self, *args, **kwargs):
+        r = self.origin_request(*args, **kwargs)
+        ReportStore.log_http_request(args[0], args[1], kwargs["data"],
+                kwargs["headers"], r)
+        return r
+    market.session.request = request_wrap.__get__(market.session, market.session.__class__)
 
-def request_wrap(self, *args, **kwargs):
-    r = self.origin_request(*args, **kwargs)
-    requests.append({
-        "method": args[0],
-        "url": args[1],
-        "body": kwargs["data"],
-        "headers": kwargs["headers"],
-        "status": r.status_code,
-        "response": r.text,
-        })
-    return r
-market.session.request = request_wrap.__get__(market.session, market.session.__class__)
+    return market
index 0eea7a491cb2dc2a86f931b2c4d5ffc84046335a..8e78c05f4d7cdf669345c025ea4c17db894cb6d7 100644 (file)
@@ -2,3 +2,4 @@ ccxt>=1.10.593
 simplejson>=3.13.2
 requests>=2.11.1
 requests_mock>=1.4.0
+psycopg2>=2.7
diff --git a/test.py b/test.py
index 34b2fe3c228cf46b70878ea998965270eb273109..fc331c314825261dc30c8f4947e040064083f502 100644 (file)
--- a/test.py
+++ b/test.py
@@ -1075,6 +1075,16 @@ class HelperTest(WebMockTestCase):
         follow_orders.assert_called()
         log_stage.assert_called_with("process_sell_all__2_all_buy_end")
 
+    def test_reset_all(self):
+        portfolio.BalanceStore.all = { "foo": "bar" }
+        portfolio.ReportStore.logs.append("hey")
+        portfolio.TradeStore.all.append("bouh")
+
+        helper.reset_all()
+
+        self.assertEqual(0, len(portfolio.BalanceStore.all))
+        self.assertEqual(0, len(portfolio.ReportStore.logs))
+        self.assertEqual(0, len(portfolio.TradeStore.all))
 
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class TradeStoreTest(WebMockTestCase):