]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - helper.py
Remove useless update_trades method
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / helper.py
index 4d73078dbb99aed90f819c0ac9947f04a8f9d080..be480e28fdbb76df7ae0d91c9130bd2a2e82d379 100644 (file)
--- a/helper.py
+++ b/helper.py
@@ -7,6 +7,62 @@ import sys
 
 import portfolio
 
+def make_order(market, value, currency, action="acquire",
+        close_if_possible=False, base_currency="BTC", follow=True,
+        compute_value="average"):
+    """
+    Make an order on market
+    "market": The market on which to place the order
+    "value": The value in *base_currency* to acquire,
+             or in *currency* to dispose.
+             use negative for margin trade.
+    "action": "acquire" or "dispose".
+                "acquire" will buy long or sell short,
+                "dispose" will sell long or buy short.
+    "currency": The currency to acquire or dispose
+    "base_currency": The base currency. The value is expressed in that
+                     currency (default: BTC)
+    "follow": Whether to follow the order once run (default: True)
+    "close_if_possible": Whether to try to close the position at the end
+                         of the trade, i.e. reach exactly 0 at the end
+                         (only meaningful in "dispose"). May have
+                         unwanted effects if the end value of the
+                         currency is not 0.
+    "compute_value": Compute value to place the order
+    """
+    market.report.log_stage("make_order_begin")
+    market.balances.fetch_balances(tag="make_order_begin")
+    if action == "acquire":
+        trade = portfolio.Trade(
+                portfolio.Amount(base_currency, 0),
+                portfolio.Amount(base_currency, value),
+                currency, market)
+    else:
+        amount = portfolio.Amount(currency, value)
+        trade = portfolio.Trade(
+                amount.in_currency(base_currency, market, compute_value=compute_value),
+                portfolio.Amount(base_currency, 0),
+                currency, market)
+    market.trades.all.append(trade)
+    order = trade.prepare_order(
+            close_if_possible=close_if_possible,
+            compute_value=compute_value)
+    market.report.log_orders([order], None, compute_value)
+    market.trades.run_orders()
+    if follow:
+        market.follow_orders()
+        market.balances.fetch_balances(tag="make_order_end")
+    else:
+        market.report.log_stage("make_order_end_not_followed")
+        return order
+    market.report.log_stage("make_order_end")
+
+def get_user_market(config_path, user_id, debug=False):
+    import market
+    pg_config, report_path = main_parse_config(config_path)
+    market_config = list(main_fetch_markets(pg_config, str(user_id)))[0][0]
+    return market.Market.from_config(market_config, debug=debug)
+
 def main_parse_args(argv):
     parser = argparse.ArgumentParser(
             description="Run the trade bot")
@@ -27,8 +83,8 @@ def main_parse_args(argv):
     parser.add_argument("--user",
             default=None, required=False, help="Only run for that user")
     parser.add_argument("--action",
-            default=None, required=False,
-            help="Do a different action than trading")
+            action='append',
+            help="Do a different action than trading (add several times to chain)")
 
     args = parser.parse_args(argv)
 
@@ -68,19 +124,19 @@ def main_fetch_markets(pg_config, user):
     for row in cursor:
         yield row
 
-def main_process_market(user_market, action, before=False, after=False):
-    if action is None:
+def main_process_market(user_market, actions, before=False, after=False):
+    if len(actions or []) == 0:
         if before:
             process_sell_all__1_all_sell(user_market)
         if after:
-            portfolio.Portfolio.wait_for_recent(user_market)
-            process_sell_all__2_all_buy(user_market)
-    elif action == "print_balances":
-        print_balances(user_market)
-    elif action == "print_orders":
-        print_orders(user_market)
+            process_sell_all__2_wait(user_market)
+            process_sell_all__3_all_buy(user_market)
     else:
-        raise NotImplementedError("Unknown action {}".format(action))
+        for action in actions:
+            if action in globals():
+                (globals()[action])(user_market)
+            else:
+                raise NotImplementedError("Unknown action {}".format(action))
 
 def main_store_report(report_path, user_id, user_market):
     try:
@@ -117,7 +173,7 @@ def process_sell_needed__1_sell(market, liquidity="medium", base_currency="BTC")
 def process_sell_needed__2_buy(market, liquidity="medium", base_currency="BTC"):
     market.report.log_stage("process_sell_needed__2_buy_begin")
     market.balances.fetch_balances(tag="process_sell_needed__2_buy_begin")
-    market.update_trades(base_currency=base_currency, liquidity=liquidity, only="acquire")
+    market.prepare_trades(base_currency=base_currency, liquidity=liquidity, only="acquire")
     market.trades.prepare_orders(compute_value="average", only="acquire")
     market.move_balances()
     market.trades.run_orders()
@@ -135,15 +191,20 @@ def process_sell_all__1_all_sell(market, base_currency="BTC", liquidity="medium"
     market.balances.fetch_balances(tag="process_sell_all__1_all_sell_end")
     market.report.log_stage("process_sell_all__1_all_sell_end")
 
-def process_sell_all__2_all_buy(market, base_currency="BTC", liquidity="medium"):
-    market.report.log_stage("process_sell_all__2_all_buy_begin")
-    market.balances.fetch_balances(tag="process_sell_all__2_all_buy_begin")
+def process_sell_all__2_wait(market, liquidity="medium", base_currency="BTC"):
+    market.report.log_stage("process_sell_all__2_wait_begin")
+    portfolio.Portfolio.wait_for_recent(market)
+    market.report.log_stage("process_sell_all__2_wait_end")
+
+def process_sell_all__3_all_buy(market, base_currency="BTC", liquidity="medium"):
+    market.report.log_stage("process_sell_all__3_all_buy_begin")
+    market.balances.fetch_balances(tag="process_sell_all__3_all_buy_begin")
     market.prepare_trades(liquidity=liquidity, base_currency=base_currency)
     market.trades.prepare_orders(compute_value="average")
     market.move_balances()
     market.trades.run_orders()
     market.follow_orders()
-    market.balances.fetch_balances(tag="process_sell_all__2_all_buy_end")
-    market.report.log_stage("process_sell_all__2_all_buy_end")
+    market.balances.fetch_balances(tag="process_sell_all__3_all_buy_end")
+    market.report.log_stage("process_sell_all__3_all_buy_end")