mirror of
https://github.com/wassname/catalyst.git
synced 2026-06-28 17:49:09 +08:00
51677ea700
Thanks, Dale.
151 lines
5.0 KiB
Python
151 lines
5.0 KiB
Python
#
|
|
# Copyright 2015 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 datetime
|
|
import pytz
|
|
|
|
from nose_parameterized import parameterized
|
|
from unittest import TestCase
|
|
|
|
from zipline.finance.blotter import Blotter, Order
|
|
from zipline.finance.commission import PerShare, PerTrade, PerDollar
|
|
from zipline.finance.performance.period import PerformancePeriod
|
|
from zipline.finance.performance.position import Position
|
|
from zipline.finance.performance.tracker import PerformanceTracker
|
|
from zipline.finance.risk.cumulative import RiskMetricsCumulative
|
|
from zipline.finance.risk.period import RiskMetricsPeriod
|
|
from zipline.finance.risk.report import RiskReport
|
|
from zipline.finance.slippage import (
|
|
FixedSlippage,
|
|
Transaction,
|
|
VolumeShareSlippage
|
|
)
|
|
from zipline.protocol import Account
|
|
from zipline.protocol import Portfolio
|
|
from zipline.protocol import Position as ProtocolPosition
|
|
|
|
|
|
from zipline.finance.trading import SimulationParameters
|
|
|
|
from zipline.utils import factory
|
|
|
|
from six import iteritems
|
|
|
|
sim_params_daily = SimulationParameters(
|
|
datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC),
|
|
datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC),
|
|
10000,
|
|
emission_rate='daily')
|
|
sim_params_minute = SimulationParameters(
|
|
datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC),
|
|
datetime.datetime(2013, 6, 19, tzinfo=pytz.UTC),
|
|
10000,
|
|
emission_rate='minute')
|
|
returns = factory.create_returns_from_list(
|
|
[1.0], sim_params_daily)
|
|
|
|
|
|
def gather_bad_dicts(state):
|
|
bad = []
|
|
for k, v in iteritems(state):
|
|
if not isinstance(v, dict):
|
|
continue
|
|
if type(v) != dict:
|
|
bad.append((k, v))
|
|
bad.extend(gather_bad_dicts(v))
|
|
return bad
|
|
|
|
|
|
def stringify_cases(cases, func=None):
|
|
# get better test case names
|
|
results = []
|
|
if func is None:
|
|
func = lambda case: case[0].__name__
|
|
for case in cases:
|
|
new_case = list(case)
|
|
key = func(case)
|
|
new_case.insert(0, key)
|
|
results.append(new_case)
|
|
return results
|
|
|
|
|
|
class SerializationTestCase(TestCase):
|
|
|
|
object_serialization_cases = stringify_cases([
|
|
(Blotter, (), 'repr'),
|
|
(Order, (datetime.datetime(2013, 6, 19), 8554, 100), 'dict'),
|
|
(PerShare, (), 'dict'),
|
|
(PerTrade, (), 'dict'),
|
|
(PerDollar, (), 'dict'),
|
|
(PerformancePeriod, (10000,), 'to_dict'),
|
|
(Position, (8554,), 'dict'),
|
|
(PerformanceTracker, (sim_params_daily,), 'to_dict'),
|
|
(PerformanceTracker, (sim_params_minute,), 'to_dict'),
|
|
(RiskMetricsCumulative, (sim_params_daily,), 'to_dict'),
|
|
(RiskMetricsCumulative, (sim_params_minute,), 'to_dict'),
|
|
(RiskMetricsPeriod,
|
|
(returns.index[0], returns.index[0], returns), 'to_dict'),
|
|
(RiskReport, (returns, sim_params_daily), 'to_dict'),
|
|
(RiskReport, (returns, sim_params_minute), 'to_dict'),
|
|
(FixedSlippage, (), 'dict'),
|
|
(Transaction,
|
|
(8554, 10, datetime.datetime(2013, 6, 19), 100, "0000"), 'dict'),
|
|
(VolumeShareSlippage, (), 'dict'),
|
|
(Account, (), 'dict'),
|
|
(Portfolio, (), 'dict'),
|
|
(ProtocolPosition, (8554,), 'dict')
|
|
])
|
|
|
|
@parameterized.expand(object_serialization_cases)
|
|
def test_object_serialization(self,
|
|
_,
|
|
cls,
|
|
initargs,
|
|
comparison_method='dict'):
|
|
|
|
obj = cls(*initargs)
|
|
state = obj.__getstate__()
|
|
|
|
bad_dicts = gather_bad_dicts(state)
|
|
bad_template = "type({0}) == {1}".format
|
|
bad_msgs = [bad_template(k, type(v)) for k, v in bad_dicts]
|
|
msg = "Only support bare dicts. " + ', '.join(bad_msgs)
|
|
self.assertEqual(len(bad_dicts), 0, msg)
|
|
# no state should have a dict subclass. Only regular PyDict
|
|
|
|
if hasattr(obj, '__getinitargs__'):
|
|
initargs = obj.__getinitargs__()
|
|
else:
|
|
initargs = None
|
|
if hasattr(obj, '__getnewargs__'):
|
|
newargs = obj.__getnewargs__()
|
|
else:
|
|
newargs = None
|
|
|
|
if newargs is not None:
|
|
obj2 = cls.__new__(cls, *newargs)
|
|
else:
|
|
obj2 = cls.__new__(cls)
|
|
if initargs is not None:
|
|
obj2.__init__(*initargs)
|
|
obj2.__setstate__(state)
|
|
|
|
if comparison_method == 'repr':
|
|
self.assertEqual(obj.__repr__(), obj2.__repr__())
|
|
elif comparison_method == 'to_dict':
|
|
self.assertEqual(obj.to_dict(), obj2.to_dict())
|
|
else:
|
|
self.assertEqual(obj.__dict__, obj2.__dict__)
|