| 1 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # Copyright 2014 Quantopian, Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | #     http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | # Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | # distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import datetime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | import pytz | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | import zipline.finance.risk as risk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | from zipline.utils import factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | from zipline.finance.trading import SimulationParameters, TradingEnvironment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | from . import answer_key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | ANSWER_KEY = answer_key.ANSWER_KEY | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 30 |  |  | class TestRisk(unittest.TestCase): | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |     @classmethod | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |     def setUpClass(cls): | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |         cls.env = TradingEnvironment() | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |     @classmethod | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |     def tearDownClass(cls): | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |         del cls.env | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |     def setUp(self): | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |         start_date = datetime.datetime( | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |             year=2006, | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |             month=1, | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |             day=1, | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |             hour=0, | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |             minute=0, | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |             tzinfo=pytz.utc) | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         end_date = datetime.datetime( | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |             year=2006, month=12, day=29, tzinfo=pytz.utc) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         self.sim_params = SimulationParameters( | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |             period_start=start_date, | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             period_end=end_date, | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |             env=self.env, | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         self.algo_returns_06 = factory.create_returns_from_list( | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             answer_key.ALGORITHM_RETURNS.values, | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             self.sim_params | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         self.cumulative_metrics_06 = risk.RiskMetricsCumulative( | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             self.sim_params, env=self.env | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         for dt, returns in answer_key.RETURNS_DATA.iterrows(): | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             self.cumulative_metrics_06.update(dt, | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |                                               returns['Algorithm Returns'], | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |                                               returns['Benchmark Returns'], | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |                                               {'leverage': 0.0}) | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     def test_algorithm_volatility_06(self): | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         algo_vol_answers = answer_key.RISK_CUMULATIVE.volatility | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         for dt, value in algo_vol_answers.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |                 self.cumulative_metrics_06.algorithm_volatility[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     def test_sharpe_06(self): | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.sharpe.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |                 self.cumulative_metrics_06.sharpe[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     def test_downside_risk_06(self): | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.downside_risk.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |                 self.cumulative_metrics_06.downside_risk[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     def test_sortino_06(self): | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.sortino.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |                 self.cumulative_metrics_06.sortino[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |                 decimal=4, | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     def test_information_06(self): | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.information.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |                 self.cumulative_metrics_06.information[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     def test_alpha_06(self): | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.alpha.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |                 self.cumulative_metrics_06.alpha[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |     def test_beta_06(self): | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.beta.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |                 self.cumulative_metrics_06.beta[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |     def test_max_drawdown_06(self): | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |         for dt, value in answer_key.RISK_CUMULATIVE.max_drawdown.iteritems(): | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |             dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt) | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |             np.testing.assert_almost_equal( | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |                 self.cumulative_metrics_06.max_drawdowns[dt_loc], | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |                 value, | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |                 err_msg="Mismatch at %s" % (dt,)) | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  |