From c1d140a8319d8732c8a5fdf22ca11b6025c37a29 Mon Sep 17 00:00:00 2001 From: fredfortier Date: Tue, 21 Nov 2017 15:55:56 -0500 Subject: [PATCH] BUG: fixed issue #77, a sortino warning prevents analyze() from completing --- catalyst/finance/risk/cumulative.py | 8 +++++--- catalyst/finance/risk/period.py | 21 ++++++++++++++------- catalyst/utils/run_algo.py | 5 +++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/catalyst/finance/risk/cumulative.py b/catalyst/finance/risk/cumulative.py index 82dd5c0b..20798f30 100644 --- a/catalyst/finance/risk/cumulative.py +++ b/catalyst/finance/risk/cumulative.py @@ -274,12 +274,14 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}" ) try: + risk = self.downside_risk[dt_loc] self.sortino[dt_loc] = sortino_ratio( self.algorithm_returns, - _downside_risk=self.downside_risk[dt_loc] + _downside_risk=risk ) - except Exception as e: - log.debug('sortino ratio error: {}'.format(e)) + except Exception: + # TODO: what causes it to error out? + self.sortino[dt_loc] = 0 self.information[dt_loc] = information_ratio( self.algorithm_returns, diff --git a/catalyst/finance/risk/period.py b/catalyst/finance/risk/period.py index ddef2def..83d34ba1 100644 --- a/catalyst/finance/risk/period.py +++ b/catalyst/finance/risk/period.py @@ -23,7 +23,7 @@ import numpy as np import pandas as pd from . import risk -from . risk import check_entry +from .risk import check_entry from empyrical import ( alpha_beta_aligned, @@ -85,7 +85,7 @@ class RiskMetricsPeriod(object): cum_returns(self.algorithm_returns).iloc[-1] if not self.algorithm_returns.index.equals( - self.benchmark_returns.index + self.benchmark_returns.index ): message = "Mismatch between benchmark_returns ({bm_count}) and \ algorithm_returns ({algo_count}) in range {start} : {end}" @@ -128,10 +128,17 @@ class RiskMetricsPeriod(object): self.downside_risk = downside_risk( self.algorithm_returns.values ) - self.sortino = sortino_ratio( - self.algorithm_returns.values, - _downside_risk=self.downside_risk, - ) + + try: + risk = self.downside_risk + self.sortino = sortino_ratio( + self.algorithm_returns.values, + _downside_risk=risk, + ) + except Exception: + # TODO: what causes it to error out? + self.sortino = 0 + self.information = information_ratio( self.algorithm_returns.values, self.benchmark_returns.values, @@ -141,7 +148,7 @@ class RiskMetricsPeriod(object): self.benchmark_returns.values, ) self.excess_return = self.algorithm_period_returns - \ - self.treasury_period_return + self.treasury_period_return self.max_drawdown = max_drawdown(self.algorithm_returns.values) self.max_leverage = self.calculate_max_leverage() diff --git a/catalyst/utils/run_algo.py b/catalyst/utils/run_algo.py index b4335311..dfb97c3e 100644 --- a/catalyst/utils/run_algo.py +++ b/catalyst/utils/run_algo.py @@ -451,7 +451,8 @@ def run_algorithm(initialize, exchange_name=None, base_currency=None, algo_namespace=None, - live_graph=False): + live_graph=False, + output=os.devnull): """Run a trading algorithm. Parameters @@ -564,7 +565,7 @@ def run_algorithm(initialize, bundle_timestamp=bundle_timestamp, start=start, end=end, - output=os.devnull, + output=output, print_algo=False, local_namespace=False, environ=environ,