aboutsummaryrefslogtreecommitdiff
path: root/market.py
diff options
context:
space:
mode:
Diffstat (limited to 'market.py')
-rw-r--r--market.py52
1 files changed, 27 insertions, 25 deletions
diff --git a/market.py b/market.py
index 7a37cf6..4593eb1 100644
--- a/market.py
+++ b/market.py
@@ -76,17 +76,13 @@ class Market:
76 76
77 def process(self, actions, before=False, after=False): 77 def process(self, actions, before=False, after=False):
78 try: 78 try:
79 if len(actions or []) == 0: 79 for action in actions:
80 if before: 80 if bool(before) is bool(after):
81 self.processor.process("sell_all", steps="before") 81 self.processor.process(action, steps="all")
82 if after: 82 elif before:
83 self.processor.process("sell_all", steps="after") 83 self.processor.process(action, steps="before")
84 else: 84 elif after:
85 for action in actions: 85 self.processor.process(action, steps="after")
86 if hasattr(self, action):
87 getattr(self, action)()
88 else:
89 self.report.log_error("market_process", message="Unknown action {}".format(action))
90 except Exception as e: 86 except Exception as e:
91 self.report.log_error("market_process", exception=e) 87 self.report.log_error("market_process", exception=e)
92 finally: 88 finally:
@@ -212,16 +208,7 @@ class Market:
212 liquidity=liquidity, repartition=repartition) 208 liquidity=liquidity, repartition=repartition)
213 self.trades.compute_trades(values_in_base, new_repartition, only=only) 209 self.trades.compute_trades(values_in_base, new_repartition, only=only)
214 210
215 # Helpers 211 def print_tickers(self, base_currency="BTC"):
216 def print_orders(self, base_currency="BTC"):
217 self.report.log_stage("print_orders")
218 self.balances.fetch_balances(tag="print_orders")
219 self.prepare_trades(base_currency=base_currency, compute_value="average")
220 self.trades.prepare_orders(compute_value="average")
221
222 def print_balances(self, base_currency="BTC"):
223 self.report.log_stage("print_balances")
224 self.balances.fetch_balances()
225 if base_currency is not None: 212 if base_currency is not None:
226 self.report.print_log("total:") 213 self.report.print_log("total:")
227 self.report.print_log(sum(self.balances.in_currency(base_currency).values())) 214 self.report.print_log(sum(self.balances.in_currency(base_currency).values()))
@@ -237,12 +224,20 @@ class Processor:
237 "wait_for_recent": {}, 224 "wait_for_recent": {},
238 }, 225 },
239 ], 226 ],
227 "print_balances": [
228 {
229 "name": "print_balances",
230 "number": 1,
231 "fetch_balances": ["begin"],
232 "print_tickers": { "base_currency": "BTC" },
233 }
234 ],
240 "print_orders": [ 235 "print_orders": [
241 { 236 {
242 "name": "wait", 237 "name": "wait",
243 "number": 1, 238 "number": 1,
244 "before": False, 239 "before": True,
245 "after": True, 240 "after": False,
246 "wait_for_recent": {}, 241 "wait_for_recent": {},
247 }, 242 },
248 { 243 {
@@ -328,7 +323,7 @@ class Processor:
328 ordered_actions = [ 323 ordered_actions = [
329 "wait_for_recent", "prepare_trades", "prepare_orders", 324 "wait_for_recent", "prepare_trades", "prepare_orders",
330 "move_balances", "run_orders", "follow_orders", 325 "move_balances", "run_orders", "follow_orders",
331 "close_trades"] 326 "close_trades", "print_tickers"]
332 327
333 def __init__(self, market): 328 def __init__(self, market):
334 self.market = market 329 self.market = market
@@ -337,7 +332,7 @@ class Processor:
337 if step == "all": 332 if step == "all":
338 return scenario 333 return scenario
339 elif step == "before" or step == "after": 334 elif step == "before" or step == "after":
340 return list(filter(lambda x: step in x and x[step], scenario)) 335 return list(filter(lambda x: x.get(step, False), scenario))
341 elif type(step) == int: 336 elif type(step) == int:
342 return [scenario[step-1]] 337 return [scenario[step-1]]
343 elif type(step) == str: 338 elif type(step) == str:
@@ -345,7 +340,12 @@ class Processor:
345 else: 340 else:
346 raise TypeError("Unknown step {}".format(step)) 341 raise TypeError("Unknown step {}".format(step))
347 342
343 def can_process(self, scenario_name):
344 return scenario_name in self.scenarios
345
348 def process(self, scenario_name, steps="all", **kwargs): 346 def process(self, scenario_name, steps="all", **kwargs):
347 if not self.can_process(scenario_name):
348 raise TypeError("Unknown scenario {}".format(scenario_name))
349 scenario = self.scenarios[scenario_name] 349 scenario = self.scenarios[scenario_name]
350 selected_steps = [] 350 selected_steps = []
351 351
@@ -388,6 +388,8 @@ class Processor:
388 method = self.market.follow_orders 388 method = self.market.follow_orders
389 elif action == "close_trades": 389 elif action == "close_trades":
390 method = self.market.trades.close_trades 390 method = self.market.trades.close_trades
391 elif action == "print_tickers":
392 method = self.market.print_tickers
391 393
392 signature = inspect.getfullargspec(method) 394 signature = inspect.getfullargspec(method)
393 defaults = signature.defaults or [] 395 defaults = signature.defaults or []