From 52d4ced37c46d8f5976b1ad617df593048a548a4 Mon Sep 17 00:00:00 2001 From: Victor Grau Serrat Date: Tue, 16 Jan 2018 23:40:18 -0700 Subject: [PATCH] BLD: live mode accepts end parameter, when algo finishes --- catalyst/__main__.py | 9 ++++++++- catalyst/exchange/exchange_algorithm.py | 5 +++++ catalyst/utils/run_algo.py | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/catalyst/__main__.py b/catalyst/__main__.py index 5f8f9136..def7838e 100644 --- a/catalyst/__main__.py +++ b/catalyst/__main__.py @@ -394,6 +394,12 @@ def catalyst_magic(line, cell=None): help='The base currency used to calculate statistics ' '(e.g. usd, btc, eth).', ) +@click.option( + '-e', + '--end', + type=Date(tz='utc', as_timestamp=True), + help='An optional end date at which to stop the execution.', +) @click.option( '--live-graph/--no-live-graph', is_flag=True, @@ -419,6 +425,7 @@ def live(ctx, exchange_name, algo_namespace, base_currency, + end, live_graph, simulate_orders): """Trade live with the given algorithm. @@ -461,7 +468,7 @@ def live(ctx, bundle=None, bundle_timestamp=None, start=None, - end=None, + end=end, output=output, print_algo=print_algo, local_namespace=local_namespace, diff --git a/catalyst/exchange/exchange_algorithm.py b/catalyst/exchange/exchange_algorithm.py index 0befb11a..857d44ac 100644 --- a/catalyst/exchange/exchange_algorithm.py +++ b/catalyst/exchange/exchange_algorithm.py @@ -351,6 +351,7 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): self.live_graph = kwargs.pop('live_graph', None) self.stats_output = kwargs.pop('stats_output', None) self._analyze_live = kwargs.pop('analyze_live', None) + self.end = kwargs.pop('end', None) self._clock = None self.frame_stats = list() @@ -710,6 +711,10 @@ class ExchangeTradingAlgorithmLive(ExchangeTradingAlgorithmBase): if not self.is_running: return + if self.end is not None and self.end < data.current_dt: + log.info('Algorithm has reached specified end time. Finishing...') + self.interrupt_algorithm() + # Resetting the frame stats every day to minimize memory footprint today = data.current_dt.floor('1D') if self.current_day is not None and today > self.current_day: diff --git a/catalyst/utils/run_algo.py b/catalyst/utils/run_algo.py index d1945fc6..9285114a 100644 --- a/catalyst/utils/run_algo.py +++ b/catalyst/utils/run_algo.py @@ -206,7 +206,8 @@ def _run(handle_data, start = pd.Timestamp.utcnow() # TODO: fix the end data. - end = start + timedelta(hours=8760) + if end is None: + end = start + timedelta(hours=8760) data = DataPortalExchangeLive( exchanges=exchanges, @@ -234,6 +235,7 @@ def _run(handle_data, simulate_orders=simulate_orders, stats_output=stats_output, analyze_live=analyze_live, + end=end, ) elif exchanges: # Removed the existing Poloniex fork to keep things simple