mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-21 12:30:16 +08:00
Python 3 requires submodules to have more explicit pathing, so use the dot syntax to declare submodules which are in the same directory as another module.
119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
#
|
|
# Copyright 2013 Quantopian, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import unittest
|
|
|
|
import datetime
|
|
import numpy as np
|
|
import pytz
|
|
import zipline.finance.risk as risk
|
|
from zipline.utils import factory
|
|
|
|
from zipline.finance.trading import SimulationParameters
|
|
|
|
from . import answer_key
|
|
ANSWER_KEY = answer_key.ANSWER_KEY
|
|
|
|
|
|
class TestRisk(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
start_date = datetime.datetime(
|
|
year=2006,
|
|
month=1,
|
|
day=1,
|
|
hour=0,
|
|
minute=0,
|
|
tzinfo=pytz.utc)
|
|
end_date = datetime.datetime(
|
|
year=2006, month=12, day=29, tzinfo=pytz.utc)
|
|
|
|
self.sim_params = SimulationParameters(
|
|
period_start=start_date,
|
|
period_end=end_date
|
|
)
|
|
|
|
self.algo_returns_06 = factory.create_returns_from_list(
|
|
answer_key.ALGORITHM_RETURNS.values,
|
|
self.sim_params
|
|
)
|
|
|
|
self.cumulative_metrics_06 = risk.RiskMetricsCumulative(
|
|
self.sim_params)
|
|
|
|
for dt, returns in answer_key.RETURNS_DATA.iterrows():
|
|
self.cumulative_metrics_06.update(dt,
|
|
returns['Algorithm Returns'],
|
|
returns['Benchmark Returns'])
|
|
|
|
def test_algorithm_volatility_06(self):
|
|
np.testing.assert_almost_equal(
|
|
ANSWER_KEY.ALGORITHM_CUMULATIVE_VOLATILITY,
|
|
self.cumulative_metrics_06.metrics.algorithm_volatility.values)
|
|
|
|
def test_sharpe_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.sharpe.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
value,
|
|
self.cumulative_metrics_06.metrics.sharpe[dt],
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_downside_risk_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.downside_risk.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
self.cumulative_metrics_06.metrics.downside_risk[dt],
|
|
value,
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_sortino_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.sortino.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
self.cumulative_metrics_06.metrics.sortino[dt],
|
|
value,
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_information_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.information.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
self.cumulative_metrics_06.metrics.information[dt],
|
|
value,
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_alpha_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.alpha.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
self.cumulative_metrics_06.metrics.alpha[dt],
|
|
value,
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_beta_06(self):
|
|
for dt, value in answer_key.RISK_CUMULATIVE.beta.iterkv():
|
|
np.testing.assert_almost_equal(
|
|
self.cumulative_metrics_06.metrics.beta[dt],
|
|
value,
|
|
decimal=2,
|
|
err_msg="Mismatch at %s" % (dt,))
|
|
|
|
def test_max_drawdown_calculated(self):
|
|
# We don't track max_drawdown by day, so it doesn't make sense to
|
|
# generate a full answer key for it. For now, ensure it's just
|
|
# "not zero"
|
|
self.assertNotEqual(self.cumulative_metrics_06.max_drawdown, 0.0)
|