diff --git a/.travis.yml b/.travis.yml index 00d07600..f65cd46c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,6 @@ install: - cat etc/requirements_dev.txt | grep -v "^#" | grep -v "^$" | grep -v ipython | grep -v nose | xargs pip install - etc/ordered_pip.sh etc/requirements.txt before_script: - - "flake8 zipline tests" + - "flake8 --ignore=E124,E125,E126 zipline tests" script: - nosetests diff --git a/README.md b/README.md index d69cd20b..4e585621 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ To ensure that changes and patches are focused on behavior changes, the zipline The maintainers check the code using the flake8 script, , which is included in the requirements_dev.txt. -Before submitting patches or pull requests, please ensure that your changes pass ```flake8 zipline tests``` +Before submitting patches or pull requests, please ensure that your changes pass ```flake8 --ignore=E124,E125,E126 zipline tests``` Discussion and Help =================== diff --git a/etc/requirements_dev.txt b/etc/requirements_dev.txt index dedc64d1..84f6b033 100644 --- a/etc/requirements_dev.txt +++ b/etc/requirements_dev.txt @@ -9,4 +9,4 @@ requests==0.14.1 # Linting -flake8==1.4 +flake8==1.5 diff --git a/tests/finance/test_slippage.py b/tests/finance/test_slippage.py index 2d12329f..b5ad0380 100644 --- a/tests/finance/test_slippage.py +++ b/tests/finance/test_slippage.py @@ -55,8 +55,7 @@ class SlippageTestCase(TestCase): {'dt': datetime.datetime(2006, 1, 5, 14, 30, tzinfo=pytz.utc), 'amount': 100, 'filled': 0, 'sid': 133}) - ] - } + ]} txn = slippage_model.simulate( event, @@ -69,7 +68,7 @@ class SlippageTestCase(TestCase): 2006, 1, 5, 14, 31, tzinfo=pytz.utc), 'amount': int(50), 'sid': int(133) - } + } self.assertIsNotNone(txn) diff --git a/tests/test_exception_handling.py b/tests/test_exception_handling.py index 9df9e3ee..1399bed0 100644 --- a/tests/test_exception_handling.py +++ b/tests/test_exception_handling.py @@ -83,10 +83,10 @@ class ExceptionTestCase(TestCase): # Simulation # ---------- self.zipline_test_config['algorithm'] = \ - ExceptionAlgorithm( - 'handle_data', - self.zipline_test_config['sid'] - ) + ExceptionAlgorithm( + 'handle_data', + self.zipline_test_config['sid'] + ) zipline = simfactory.create_test_zipline( **self.zipline_test_config @@ -103,9 +103,9 @@ class ExceptionTestCase(TestCase): # Simulation # ---------- self.zipline_test_config['algorithm'] = \ - DivByZeroAlgorithm( - self.zipline_test_config['sid'] - ) + DivByZeroAlgorithm( + self.zipline_test_config['sid'] + ) zipline = simfactory.create_test_zipline( **self.zipline_test_config diff --git a/tests/test_finance.py b/tests/test_finance.py index 28718a6a..4bb14447 100644 --- a/tests/test_finance.py +++ b/tests/test_finance.py @@ -72,7 +72,7 @@ class FinanceTestCase(TestCase): @timed(DEFAULT_TIMEOUT) def test_trading_environment(self): benchmark_returns, treasury_curves = \ - factory.load_market_data() + factory.load_market_data() env = TradingEnvironment( benchmark_returns, @@ -270,8 +270,7 @@ class FinanceTestCase(TestCase): order_date = start_date for i in xrange(order_count): - order = ndict( - { + order = ndict({ 'sid': sid, 'amount': order_amount * alternator ** i, 'dt': order_date diff --git a/tests/test_perf_tracking.py b/tests/test_perf_tracking.py index ed407233..3d2cb7c8 100644 --- a/tests/test_perf_tracking.py +++ b/tests/test_perf_tracking.py @@ -30,7 +30,7 @@ class PerformanceTestCase(unittest.TestCase): def setUp(self): self.benchmark_returns, self.treasury_curves = \ - factory.load_market_data() + factory.load_market_data() for n in range(100): @@ -127,8 +127,7 @@ check treasury and benchmark data in findb, and re-run the test.""" "last sale should be same as last trade. \ expected {exp} actual {act}".format( exp=trades[-1]['price'], - act=pp.positions[1].last_sale_price - ) + act=pp.positions[1].last_sale_price) ) self.assertEqual( @@ -446,8 +445,7 @@ shares in position" pp.positions[1].last_sale_price, trades[-1].price, "should have a last sale of 12, got {val}".format( - val=pp.positions[1].last_sale_price - ) + val=pp.positions[1].last_sale_price) ) self.assertEqual( diff --git a/tests/test_risk.py b/tests/test_risk.py index c3c2066d..2c43410f 100644 --- a/tests/test_risk.py +++ b/tests/test_risk.py @@ -38,7 +38,7 @@ class Risk(unittest.TestCase): year=2006, month=12, day=31, tzinfo=pytz.utc) self.benchmark_returns, self.treasury_curves = \ - factory.load_market_data() + factory.load_market_data() self.trading_env = TradingEnvironment( self.benchmark_returns, @@ -768,46 +768,260 @@ class Risk(unittest.TestCase): period_length=period_length, start_date=start_date, end=col[-1].end_date, - actual=len(col) - )) + actual=len(col)) + ) self.assert_month(start_date.month, col[-1].end_date.month) self.assert_last_day(col[-1].end_date) RETURNS = [ - 0.0093, -0.0193, 0.0351, 0.0396, 0.0338, -0.0211, 0.0389, - 0.0326, -0.0137, -0.0411, -0.0032, 0.0149, 0.0133, 0.0348, - 0.042, -0.0455, 0.0262, -0.0461, 0.0021, -0.0273, -0.0429, - 0.0427, -0.0104, 0.0346, -0.0311, 0.0003, 0.0211, 0.0248, - -0.0215, 0.004, 0.0267, 0.0029, -0.0369, 0.0057, 0.0298, - -0.0179, -0.0361, -0.0401, -0.0123, -0.005, 0.0203, -0.041, - 0.0011, 0.0118, 0.0103, -0.0184, -0.0437, 0.0411, -0.0242, - -0.0054, -0.0039, -0.0273, -0.0075, 0.0064, -0.0376, 0.0424, - 0.0399, 0.019, 0.0236, -0.0284, -0.0341, 0.0266, 0.05, - 0.0069, -0.0442, -0.016, 0.0173, 0.0348, -0.0404, -0.0068, - -0.0376, 0.0356, 0.0043, -0.0481, -0.0134, 0.0257, 0.0442, - 0.0234, 0.0394, 0.0376, -0.0147, -0.0098, 0.0474, -0.0102, - 0.0138, 0.0286, 0.0347, 0.0279, -0.0067, 0.0462, -0.0432, - 0.0247, 0.0174, -0.0305, -0.0317, -0.0068, 0.0264, -0.0257, - -0.0328, 0.0092, 0.0288, -0.002, 0.0288, 0.028, -0.0093, - 0.0178, -0.0365, -0.0086, -0.0133, -0.0309, 0.0473, -0.0149, - 0.0378, -0.0316, -0.0292, -0.0453, -0.0451, 0.0093, 0.0397, - -0.0361, -0.0168, -0.0494, -0.0143, -0.0405, -0.0349, 0.0069, - 0.0378, -0.0233, -0.0492, 0.018, -0.0386, 0.0339, 0.0119, - 0.0454, 0.0118, -0.011, -0.0254, 0.0266, -0.0366, -0.0211, - 0.0399, 0.0307, 0.035, -0.0402, 0.0304, -0.0031, 0.0256, - 0.0134, -0.0019, -0.0235, -0.0058, -0.0117, 0.0051, -0.0451, - -0.0466, -0.0124, 0.0283, -0.0499, 0.0318, -0.0028, 0.0203, - 0.005, 0.0085, 0.0048, 0.0277, 0.0159, -0.0149, 0.035, - 0.0404, -0.01, 0.0377, 0.0302, 0.0046, -0.0328, -0.0469, - 0.0071, -0.0382, -0.0214, 0.0429, 0.0145, -0.0279, -0.0172, - 0.0423, 0.041, -0.0183, 0.0137, -0.0412, -0.0348, 0.0302, - 0.0248, 0.0051, -0.0298, -0.0103, -0.0333, -0.0399, 0.0485, - -0.0166, 0.0384, 0.0259, -0.0163, 0.0357, 0.0308, -0.0386, - 0.0481, -0.0446, -0.0282, -0.0037, 0.0202, 0.0216, 0.0113, - 0.0194, 0.0392, 0.0016, 0.0268, -0.0155, -0.027, 0.02, - 0.0216, -0.0009, 0.022, 0., 0.041, 0.0133, -0.0382, - 0.0495, -0.0221, -0.0329, -0.0033, -0.0089, -0.0129, -0.0252, - 0.048, -0.0307, -0.0357, 0.0033, -0.0412, -0.0407, 0.0455, - 0.0159, -0.0051, -0.0274, -0.0213, 0.0361, 0.0051, -0.0378, - 0.0084, 0.0066, -0.0103, -0.0037, 0.0478, -0.0278 -] + 0.0093, + -0.0193, + 0.0351, + 0.0396, + 0.0338, + -0.0211, + 0.0389, + 0.0326, + -0.0137, + -0.0411, + -0.0032, + 0.0149, + 0.0133, + 0.0348, + 0.042, + -0.0455, + 0.0262, + -0.0461, + 0.0021, + -0.0273, + -0.0429, + 0.0427, + -0.0104, + 0.0346, + -0.0311, + 0.0003, + 0.0211, + 0.0248, + -0.0215, + 0.004, + 0.0267, + 0.0029, + -0.0369, + 0.0057, + 0.0298, + -0.0179, + -0.0361, + -0.0401, + -0.0123, + -0.005, + 0.0203, + -0.041, + 0.0011, + 0.0118, + 0.0103, + -0.0184, + -0.0437, + 0.0411, + -0.0242, + -0.0054, + -0.0039, + -0.0273, + -0.0075, + 0.0064, + -0.0376, + 0.0424, + 0.0399, + 0.019, + 0.0236, + -0.0284, + -0.0341, + 0.0266, + 0.05, + 0.0069, + -0.0442, + -0.016, + 0.0173, + 0.0348, + -0.0404, + -0.0068, + -0.0376, + 0.0356, + 0.0043, + -0.0481, + -0.0134, + 0.0257, + 0.0442, + 0.0234, + 0.0394, + 0.0376, + -0.0147, + -0.0098, + 0.0474, + -0.0102, + 0.0138, + 0.0286, + 0.0347, + 0.0279, + -0.0067, + 0.0462, + -0.0432, + 0.0247, + 0.0174, + -0.0305, + -0.0317, + -0.0068, + 0.0264, + -0.0257, + -0.0328, + 0.0092, + 0.0288, + -0.002, + 0.0288, + 0.028, + -0.0093, + 0.0178, + -0.0365, + -0.0086, + -0.0133, + -0.0309, + 0.0473, + -0.0149, + 0.0378, + -0.0316, + -0.0292, + -0.0453, + -0.0451, + 0.0093, + 0.0397, + -0.0361, + -0.0168, + -0.0494, + -0.0143, + -0.0405, + -0.0349, + 0.0069, + 0.0378, + -0.0233, + -0.0492, + 0.018, + -0.0386, + 0.0339, + 0.0119, + 0.0454, + 0.0118, + -0.011, + -0.0254, + 0.0266, + -0.0366, + -0.0211, + 0.0399, + 0.0307, + 0.035, + -0.0402, + 0.0304, + -0.0031, + 0.0256, + 0.0134, + -0.0019, + -0.0235, + -0.0058, + -0.0117, + 0.0051, + -0.0451, + -0.0466, + -0.0124, + 0.0283, + -0.0499, + 0.0318, + -0.0028, + 0.0203, + 0.005, + 0.0085, + 0.0048, + 0.0277, + 0.0159, + -0.0149, + 0.035, + 0.0404, + -0.01, + 0.0377, + 0.0302, + 0.0046, + -0.0328, + -0.0469, + 0.0071, + -0.0382, + -0.0214, + 0.0429, + 0.0145, + -0.0279, + -0.0172, + 0.0423, + 0.041, + -0.0183, + 0.0137, + -0.0412, + -0.0348, + 0.0302, + 0.0248, + 0.0051, + -0.0298, + -0.0103, + -0.0333, + -0.0399, + 0.0485, + -0.0166, + 0.0384, + 0.0259, + -0.0163, + 0.0357, + 0.0308, + -0.0386, + 0.0481, + -0.0446, + -0.0282, + -0.0037, + 0.0202, + 0.0216, + 0.0113, + 0.0194, + 0.0392, + 0.0016, + 0.0268, + -0.0155, + -0.027, + 0.02, + 0.0216, + -0.0009, + 0.022, + 0.0, + 0.041, + 0.0133, + -0.0382, + 0.0495, + -0.0221, + -0.0329, + -0.0033, + -0.0089, + -0.0129, + -0.0252, + 0.048, + -0.0307, + -0.0357, + 0.0033, + -0.0412, + -0.0407, + 0.0455, + 0.0159, + -0.0051, + -0.0274, + -0.0213, + 0.0361, + 0.0051, + -0.0378, + 0.0084, + 0.0066, + -0.0103, + -0.0037, + 0.0478, + -0.0278] diff --git a/tests/test_risk_compare_batch_iterative.py b/tests/test_risk_compare_batch_iterative.py index c3a232f0..ed25761a 100644 --- a/tests/test_risk_compare_batch_iterative.py +++ b/tests/test_risk_compare_batch_iterative.py @@ -44,7 +44,7 @@ class RiskCompareIterativeToBatch(unittest.TestCase): self.end_date = datetime.datetime( year=2006, month=12, day=31, tzinfo=pytz.utc) self.benchmark_returns, self.treasury_curves = \ - factory.load_market_data() + factory.load_market_data() self.trading_env = TradingEnvironment( self.benchmark_returns, @@ -115,8 +115,8 @@ class RiskCompareIterativeToBatch(unittest.TestCase): self.assertEqual(set(risk_original_dict.keys()), set(risk_refactor_dict.keys())) - err_msg_format = \ -"In update step {iter}: {measure} should be {truth} but is {returned}!" + err_msg_format = """\ +"In update step {iter}: {measure} should be {truth} but is {returned}!""" for measure in risk_original_dict.iterkeys(): if measure == 'max_drawdown': diff --git a/tests/test_sources.py b/tests/test_sources.py index 52a03a50..39decc41 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -35,4 +35,4 @@ class TestDataFrameSource(TestCase): _, df = factory.create_test_df_source() source = DataFrameSource(df, sids=[0]) assert 1 not in [event.sid for event in source], \ - "DataFrameSource should only stream selected sid 0, not sid 1." + "DataFrameSource should only stream selected sid 0, not sid 1." diff --git a/tests/test_transforms.py b/tests/test_transforms.py index e0997865..14db877c 100644 --- a/tests/test_transforms.py +++ b/tests/test_transforms.py @@ -294,7 +294,7 @@ class FinanceTransformsTestCase(TestCase): np.std([10.0, 15.0], ddof=1), np.std([10.0, 15.0, 13.0], ddof=1), np.std([15.0, 13.0, 12.0], ddof=1), - ] + ] # np has odd rounding behavior, cf. # http://docs.scipy.org/doc/np/reference/generated/np.std.html diff --git a/zipline/MESSAGES.py b/zipline/MESSAGES.py index 058a5707..1a189cfc 100644 --- a/zipline/MESSAGES.py +++ b/zipline/MESSAGES.py @@ -10,29 +10,28 @@ ERRORS = ndict({ # with a slipage object that isn't a VolumeShareSlippage or # FixedSlipapge 'UNSUPPORTED_SLIPPAGE_MODEL': -"You attempted to override slippage with an unsupported class. \ -Please use VolumeShareSlippage or FixedSlippage.", + "You attempted to override slippage with an unsupported class. \ + Please use VolumeShareSlippage or FixedSlippage.", # Raised if a users script calls override_slippage magic # after the initialize method has returned. 'OVERRIDE_SLIPPAGE_POST_INIT': -"You attempted to override slippage after the simulation has \ -started. You may only call override_slippage in your initialize \ -method.", + "You attempted to override slippage after the simulation has \ + started. You may only call override_slippage in your initialize \ + method.", # Raised if a user script calls the override_commission magic # with a commission object that isn't a PerShare or # PerTrade commission 'UNSUPPORTED_COMMISSION_MODEL': -"You attempted to override commission with an unsupported class. \ -Please use PerShare or PerTrade.", + "You attempted to override commission with an unsupported class. \ + Please use PerShare or PerTrade.", # Raised if a users script calls override_commission magic # after the initialize method has returned. 'OVERRIDE_COMMISSION_POST_INIT': -"You attempted to override commission after the simulation has \ -started. You may only call override_commission in your initialize \ -method.", - + "You attempted to override commission after the simulation has \ + started. You may only call override_commission in your initialize \ + method.", }) diff --git a/zipline/algorithm.py b/zipline/algorithm.py index dd5cd2ad..33fca5ac 100644 --- a/zipline/algorithm.py +++ b/zipline/algorithm.py @@ -134,8 +134,8 @@ class TradingAlgorithm(object): """ if isinstance(source, (list, tuple)): assert start is not None and end is not None, \ - """When providing a list of sources, \ -start and end date have to be specified.""" + """When providing a list of sources, \ + start and end date have to be specified.""" elif isinstance(source, pd.DataFrame): assert isinstance(source.index, pd.tseries.index.DatetimeIndex) # if DataFrame provided, wrap in DataFrameSource @@ -234,14 +234,14 @@ start and end date have to be specified.""" def set_slippage(self, slippage): assert isinstance(slippage, (VolumeShareSlippage, FixedSlippage)), \ - MESSAGES.ERRORS.UNSUPPORTED_SLIPPAGE_MODEL + MESSAGES.ERRORS.UNSUPPORTED_SLIPPAGE_MODEL if self.initialized: raise Exception(MESSAGES.ERRORS.OVERRIDE_SLIPPAGE_POST_INIT) self.slippage = slippage def set_commission(self, commission): assert isinstance(commission, (PerShare, PerTrade)), \ - MESSAGES.ERRORS.UNSUPPORTED_COMMISSION_MODEL + MESSAGES.ERRORS.UNSUPPORTED_COMMISSION_MODEL if self.initialized: raise Exception(MESSAGES.ERRORS.OVERRIDE_COMMISSION_POST_INIT) diff --git a/zipline/data/benchmarks.py b/zipline/data/benchmarks.py index a024dbf0..e8be5db4 100644 --- a/zipline/data/benchmarks.py +++ b/zipline/data/benchmarks.py @@ -101,7 +101,7 @@ def get_benchmark_returns(): for data_point in get_benchmark_data(): returns = (data_point['close'] - data_point['open']) / \ - data_point['open'] + data_point['open'] daily_return = DailyReturn(date=data_point['date'], returns=returns) benchmark_returns.append(daily_return) diff --git a/zipline/data/loader.py b/zipline/data/loader.py index 8a5e9825..83597521 100644 --- a/zipline/data/loader.py +++ b/zipline/data/loader.py @@ -38,7 +38,7 @@ def dump_treasury_curves(): for curve in get_treasury_data(): print curve date_as_tuple = curve['date'].timetuple()[0:6] + \ - (curve['date'].microsecond,) + (curve['date'].microsecond,) # Not ideal but massaging data into expected format del curve['date'] tr = (date_as_tuple, curve) @@ -57,13 +57,13 @@ def dump_benchmarks(): Puts source treasury and data into zipline. """ benchmark_path = os.path.join(os.path.dirname(__file__), - "benchmark.msgpack") + "benchmark.msgpack") benchmark_fp = open(benchmark_path, "wb") benchmark_data = [] for daily_return in get_benchmark_returns(): print daily_return date_as_tuple = daily_return.date.timetuple()[0:6] + \ - (daily_return.date.microsecond,) + (daily_return.date.microsecond,) # Not ideal but massaging data into expected format benchmark = (date_as_tuple, daily_return.returns) benchmark_data.append(benchmark) diff --git a/zipline/data/loader_utils.py b/zipline/data/loader_utils.py index 700db778..7ff58406 100644 --- a/zipline/data/loader_utils.py +++ b/zipline/data/loader_utils.py @@ -127,7 +127,7 @@ def _row_cb(mapping, row): target: apply_mapping(mapping, row) for target, mapping in mapping.iteritems() - } + } def make_row_cb(mapping): diff --git a/zipline/finance/risk.py b/zipline/finance/risk.py index 759777ad..a32ef16b 100644 --- a/zipline/finance/risk.py +++ b/zipline/finance/risk.py @@ -132,10 +132,10 @@ class RiskMetricsBase(object): self.treasury_period_return = self.choose_treasury() self.sharpe = self.calculate_sharpe() self.beta, self.algorithm_covariance, self.benchmark_variance, \ - self.condition_number, self.eigen_values = self.calculate_beta() + self.condition_number, self.eigen_values = self.calculate_beta() self.alpha = self.calculate_alpha() self.excess_return = self.algorithm_period_returns - \ - self.treasury_period_return + self.treasury_period_return self.max_drawdown = self.calculate_max_drawdown() def to_dict(self): @@ -203,8 +203,8 @@ class RiskMetricsBase(object): returns = [ x.returns for x in daily_returns if x.date >= self.start_date and - x.date <= self.end_date and - self.trading_environment.is_trading_day(x.date) + x.date <= self.end_date and + self.trading_environment.is_trading_day(x.date) ] period_returns = 1.0 @@ -226,7 +226,7 @@ class RiskMetricsBase(object): return 0.0 return ((self.algorithm_period_returns - self.treasury_period_return) / - self.algorithm_volatility) + self.algorithm_volatility) def calculate_beta(self): """ @@ -564,7 +564,7 @@ class RiskReport(object): self, algorithm_returns, trading_environment, - ): + ): """ algorithm_returns needs to be a list of daily_return objects sorted in date ascending order diff --git a/zipline/finance/slippage.py b/zipline/finance/slippage.py index d4ed64a6..ef312ee8 100644 --- a/zipline/finance/slippage.py +++ b/zipline/finance/slippage.py @@ -101,7 +101,7 @@ class VolumeShareSlippage(object): self.volume_limit) simulated_amount = int(volume_share * event.volume * direction) simulated_impact = (volume_share) ** 2 \ - * self.price_impact * direction * event.price + * self.price_impact * direction * event.price order.filled += (simulated_amount - total_order) total_order = simulated_amount diff --git a/zipline/sources.py b/zipline/sources.py index 330d8dd9..eafb01e3 100644 --- a/zipline/sources.py +++ b/zipline/sources.py @@ -168,11 +168,11 @@ class SpecificEquityTrades(object): # in this context the count is the number of # trades per sid, not the total. dates = date_gen( - count=self.count, - start=self.start, - delta=self.delta, - repeats=len(self.sids), - ) + count=self.count, + start=self.start, + delta=self.delta, + repeats=len(self.sids), + ) else: dates = date_gen( count=self.count, diff --git a/zipline/transforms/stddev.py b/zipline/transforms/stddev.py index 3b15f865..b1f1790a 100644 --- a/zipline/transforms/stddev.py +++ b/zipline/transforms/stddev.py @@ -110,6 +110,6 @@ class MovingStandardDevWindow(EventWindow): else: average = self.sum / len(self) s_squared = (self.sum_sqr - self.sum * average) \ - / (len(self) - 1) + / (len(self) - 1) stddev = sqrt(s_squared) return stddev diff --git a/zipline/transforms/utils.py b/zipline/transforms/utils.py index 9bc29288..1c614530 100644 --- a/zipline/transforms/utils.py +++ b/zipline/transforms/utils.py @@ -72,9 +72,9 @@ class StatefulTransform(object): """ def __init__(self, tnfm_class, *args, **kwargs): assert isinstance(tnfm_class, (types.ObjectType, types.ClassType)), \ - "Stateful transform requires a class." + "Stateful transform requires a class." assert hasattr(tnfm_class, 'update'), \ - "Stateful transform requires the class to have an update method" + "Stateful transform requires the class to have an update method" # Flag set inside the Passthrough transform class to signify special # behavior if we are being fed to merged_transforms.