mirror of
https://github.com/wassname/catalyst.git
synced 2026-08-11 11:16:15 +08:00
ENH: Added testing for versioning of serialized state.
This commit is contained in:
@@ -75,12 +75,9 @@ argument_list = [
|
||||
(PerDollar, ()),
|
||||
(PerformancePeriod, (10000,)),
|
||||
(Position, (8554,)),
|
||||
(PerformanceTracker, (sim_params_daily,)),
|
||||
(PerformanceTracker, (sim_params_minute,)),
|
||||
(RiskMetricsCumulative, (sim_params_daily,)),
|
||||
(RiskMetricsCumulative, (sim_params_minute,)),
|
||||
(RiskMetricsPeriod, (returns.index[0], returns.index[0], returns)),
|
||||
(RiskReport, (returns, sim_params_daily)),
|
||||
(RiskReport, (returns, sim_params_minute)),
|
||||
(FixedSlippage, ()),
|
||||
(Transaction, (8554, 10, datetime.datetime(2013, 6, 19), 100, "0000")),
|
||||
@@ -91,7 +88,7 @@ argument_list = [
|
||||
]
|
||||
|
||||
|
||||
def write_state_to_disk(cls, state):
|
||||
def write_state_to_disk(cls, state, emission_rate=None):
|
||||
state_dir = cls.__module__ + cls.__name__
|
||||
|
||||
full_dir = base_state_dir + '/' + state_dir
|
||||
@@ -99,7 +96,11 @@ def write_state_to_disk(cls, state):
|
||||
if not os.path.exists(full_dir):
|
||||
os.makedirs(full_dir)
|
||||
|
||||
name = 'State_Version_' + str(state['obj_state'][VERSION_LABEL])
|
||||
if emission_rate is not None:
|
||||
name = 'State_Version_' + emission_rate + \
|
||||
str(state['obj_state'][VERSION_LABEL])
|
||||
else:
|
||||
name = 'State_Version_' + str(state['obj_state'][VERSION_LABEL])
|
||||
|
||||
full_path = full_dir + '/' + name
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'_stateversion_'
|
||||
p3
|
||||
I1
|
||||
sS'new_orders'
|
||||
p4
|
||||
(lp5
|
||||
sS'orders'
|
||||
p6
|
||||
(dp7
|
||||
sS'open_orders'
|
||||
p8
|
||||
(dp9
|
||||
ssS'initargs'
|
||||
p10
|
||||
NsS'newargs'
|
||||
p11
|
||||
Ns.
|
||||
@@ -0,0 +1,60 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'direction'
|
||||
p3
|
||||
F1.0
|
||||
sS'_stateversion_'
|
||||
p4
|
||||
I1
|
||||
sS'_status'
|
||||
p5
|
||||
I0
|
||||
sS'created'
|
||||
p6
|
||||
cdatetime
|
||||
datetime
|
||||
p7
|
||||
(S'\x07\xdd\x06\x13\x00\x00\x00\x00\x00\x00'
|
||||
p8
|
||||
tp9
|
||||
Rp10
|
||||
sS'limit_reached'
|
||||
p11
|
||||
I00
|
||||
sS'stop'
|
||||
p12
|
||||
NsS'reason'
|
||||
p13
|
||||
NsS'stop_reached'
|
||||
p14
|
||||
I00
|
||||
sS'commission'
|
||||
p15
|
||||
NsS'amount'
|
||||
p16
|
||||
I100
|
||||
sS'limit'
|
||||
p17
|
||||
NsS'sid'
|
||||
p18
|
||||
I8554
|
||||
sS'dt'
|
||||
p19
|
||||
g10
|
||||
sS'type'
|
||||
p20
|
||||
I6
|
||||
sS'id'
|
||||
p21
|
||||
S'c09d3814a34c4ddeb445b34433121a03'
|
||||
p22
|
||||
sS'filled'
|
||||
p23
|
||||
I0
|
||||
ssS'initargs'
|
||||
p24
|
||||
NsS'newargs'
|
||||
p25
|
||||
Ns.
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'cost'
|
||||
p3
|
||||
F0.0015
|
||||
sS'_stateversion_'
|
||||
p4
|
||||
I1
|
||||
ssS'initargs'
|
||||
p5
|
||||
NsS'newargs'
|
||||
p6
|
||||
Ns.
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'min_trade_cost'
|
||||
p3
|
||||
NsS'cost'
|
||||
p4
|
||||
F0.03
|
||||
sS'_stateversion_'
|
||||
p5
|
||||
I1
|
||||
ssS'initargs'
|
||||
p6
|
||||
NsS'newargs'
|
||||
p7
|
||||
Ns.
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'cost'
|
||||
p3
|
||||
F5.0
|
||||
sS'_stateversion_'
|
||||
p4
|
||||
I1
|
||||
ssS'initargs'
|
||||
p5
|
||||
NsS'newargs'
|
||||
p6
|
||||
Ns.
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'_account_store'
|
||||
p3
|
||||
ccopy_reg
|
||||
_reconstructor
|
||||
p4
|
||||
(czipline.protocol
|
||||
Account
|
||||
p5
|
||||
c__builtin__
|
||||
object
|
||||
p6
|
||||
Ntp7
|
||||
Rp8
|
||||
(dp9
|
||||
S'regt_margin'
|
||||
p10
|
||||
Finf
|
||||
sS'maintenance_margin_requirement'
|
||||
p11
|
||||
F0.0
|
||||
sS'day_trades_remaining'
|
||||
p12
|
||||
Finf
|
||||
sS'buying_power'
|
||||
p13
|
||||
Finf
|
||||
sS'net_leverage'
|
||||
p14
|
||||
F0.0
|
||||
sS'settled_cash'
|
||||
p15
|
||||
F0.0
|
||||
sS'cushion'
|
||||
p16
|
||||
F0.0
|
||||
sS'_stateversion_'
|
||||
p17
|
||||
I1
|
||||
sS'leverage'
|
||||
p18
|
||||
F0.0
|
||||
sS'regt_equity'
|
||||
p19
|
||||
F0.0
|
||||
sS'excess_liquidity'
|
||||
p20
|
||||
F0.0
|
||||
sS'available_funds'
|
||||
p21
|
||||
F0.0
|
||||
sS'equity_with_loan'
|
||||
p22
|
||||
F0.0
|
||||
sS'initial_margin_requirement'
|
||||
p23
|
||||
F0.0
|
||||
sS'net_liquidation'
|
||||
p24
|
||||
F0.0
|
||||
sS'total_positions_value'
|
||||
p25
|
||||
F0.0
|
||||
sS'accrued_interest'
|
||||
p26
|
||||
F0.0
|
||||
sbsS'orders_by_modified'
|
||||
p27
|
||||
(dp28
|
||||
sS'keep_transactions'
|
||||
p29
|
||||
I01
|
||||
sS'ending_cash'
|
||||
p30
|
||||
F10000.0
|
||||
sS'_positions_store'
|
||||
p31
|
||||
(dp32
|
||||
sS'positions'
|
||||
p33
|
||||
(dp34
|
||||
sS'processed_transactions'
|
||||
p35
|
||||
(dp36
|
||||
sS'ending_value'
|
||||
p37
|
||||
cnumpy.core.multiarray
|
||||
scalar
|
||||
p38
|
||||
(cnumpy
|
||||
dtype
|
||||
p39
|
||||
(S'f8'
|
||||
p40
|
||||
I0
|
||||
I1
|
||||
tp41
|
||||
Rp42
|
||||
(I3
|
||||
S'<'
|
||||
p43
|
||||
NNNI-1
|
||||
I-1
|
||||
I0
|
||||
tp44
|
||||
bS'\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
p45
|
||||
tp46
|
||||
Rp47
|
||||
sS'loc_map'
|
||||
p48
|
||||
(dp49
|
||||
sS'starting_cash'
|
||||
p50
|
||||
I10000
|
||||
sS'returns'
|
||||
p51
|
||||
g38
|
||||
(g42
|
||||
S'\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
p52
|
||||
tp53
|
||||
Rp54
|
||||
sg17
|
||||
I1
|
||||
sS'pnl'
|
||||
p55
|
||||
g38
|
||||
(g42
|
||||
S'\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
p56
|
||||
tp57
|
||||
Rp58
|
||||
sS'period_cash_flow'
|
||||
p59
|
||||
F0.0
|
||||
sS'serialize_positions'
|
||||
p60
|
||||
I01
|
||||
sS'keep_orders'
|
||||
p61
|
||||
I00
|
||||
sS'_portfolio_store'
|
||||
p62
|
||||
g4
|
||||
(czipline.protocol
|
||||
Portfolio
|
||||
p63
|
||||
g6
|
||||
Ntp64
|
||||
Rp65
|
||||
(dp66
|
||||
g17
|
||||
I1
|
||||
sS'portfolio_value'
|
||||
p67
|
||||
F0.0
|
||||
sS'cash'
|
||||
p68
|
||||
F0.0
|
||||
sg50
|
||||
F0.0
|
||||
sg51
|
||||
F0.0
|
||||
sS'capital_used'
|
||||
p69
|
||||
F0.0
|
||||
sg55
|
||||
F0.0
|
||||
sg33
|
||||
g4
|
||||
(czipline.protocol
|
||||
Positions
|
||||
p70
|
||||
c__builtin__
|
||||
dict
|
||||
p71
|
||||
(dp72
|
||||
tp73
|
||||
Rp74
|
||||
sS'positions_value'
|
||||
p75
|
||||
F0.0
|
||||
sS'start_date'
|
||||
p76
|
||||
NsbsS'starting_value'
|
||||
p77
|
||||
F0.0
|
||||
sS'period_open'
|
||||
p78
|
||||
NsS'period_close'
|
||||
p79
|
||||
NsS'orders_by_id'
|
||||
p80
|
||||
ccyordereddict._cyordereddict
|
||||
OrderedDict
|
||||
p81
|
||||
((lp82
|
||||
tp83
|
||||
Rp84
|
||||
ssS'initargs'
|
||||
p85
|
||||
NsS'newargs'
|
||||
p86
|
||||
Ns.
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'_stateversion_'
|
||||
p3
|
||||
I1
|
||||
sS'cost_basis'
|
||||
p4
|
||||
F0.0
|
||||
sS'amount'
|
||||
p5
|
||||
I0
|
||||
sS'last_sale_price'
|
||||
p6
|
||||
F0.0
|
||||
sS'sid'
|
||||
p7
|
||||
I8554
|
||||
sS'last_sale_date'
|
||||
p8
|
||||
NssS'initargs'
|
||||
p9
|
||||
NsS'newargs'
|
||||
p10
|
||||
Ns.
|
||||
+78750
File diff suppressed because one or more lines are too long
+77769
File diff suppressed because one or more lines are too long
+77433
File diff suppressed because it is too large
Load Diff
+77573
File diff suppressed because it is too large
Load Diff
+15
@@ -0,0 +1,15 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'spread'
|
||||
p3
|
||||
F0.0
|
||||
sS'_stateversion_'
|
||||
p4
|
||||
I1
|
||||
ssS'initargs'
|
||||
p5
|
||||
NsS'newargs'
|
||||
p6
|
||||
Ns.
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'commission'
|
||||
p3
|
||||
NsS'amount'
|
||||
p4
|
||||
I10
|
||||
sS'_stateversion_'
|
||||
p5
|
||||
I1
|
||||
sS'sid'
|
||||
p6
|
||||
I8554
|
||||
sS'order_id'
|
||||
p7
|
||||
S'0000'
|
||||
p8
|
||||
sS'price'
|
||||
p9
|
||||
I100
|
||||
sS'type'
|
||||
p10
|
||||
I5
|
||||
sS'dt'
|
||||
p11
|
||||
cdatetime
|
||||
datetime
|
||||
p12
|
||||
(S'\x07\xdd\x06\x13\x00\x00\x00\x00\x00\x00'
|
||||
p13
|
||||
tp14
|
||||
Rp15
|
||||
ssS'initargs'
|
||||
p16
|
||||
NsS'newargs'
|
||||
p17
|
||||
Ns.
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'price_impact'
|
||||
p3
|
||||
F0.1
|
||||
sS'volume_limit'
|
||||
p4
|
||||
F0.25
|
||||
sS'_stateversion_'
|
||||
p5
|
||||
I1
|
||||
ssS'initargs'
|
||||
p6
|
||||
NsS'newargs'
|
||||
p7
|
||||
Ns.
|
||||
@@ -0,0 +1,60 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'regt_margin'
|
||||
p3
|
||||
Finf
|
||||
sS'maintenance_margin_requirement'
|
||||
p4
|
||||
F0.0
|
||||
sS'day_trades_remaining'
|
||||
p5
|
||||
Finf
|
||||
sS'buying_power'
|
||||
p6
|
||||
Finf
|
||||
sS'net_leverage'
|
||||
p7
|
||||
F0.0
|
||||
sS'settled_cash'
|
||||
p8
|
||||
F0.0
|
||||
sS'cushion'
|
||||
p9
|
||||
F0.0
|
||||
sS'_stateversion_'
|
||||
p10
|
||||
I1
|
||||
sS'leverage'
|
||||
p11
|
||||
F0.0
|
||||
sS'regt_equity'
|
||||
p12
|
||||
F0.0
|
||||
sS'excess_liquidity'
|
||||
p13
|
||||
F0.0
|
||||
sS'available_funds'
|
||||
p14
|
||||
F0.0
|
||||
sS'equity_with_loan'
|
||||
p15
|
||||
F0.0
|
||||
sS'initial_margin_requirement'
|
||||
p16
|
||||
F0.0
|
||||
sS'net_liquidation'
|
||||
p17
|
||||
F0.0
|
||||
sS'total_positions_value'
|
||||
p18
|
||||
F0.0
|
||||
sS'accrued_interest'
|
||||
p19
|
||||
F0.0
|
||||
ssS'initargs'
|
||||
p20
|
||||
NsS'newargs'
|
||||
p21
|
||||
Ns.
|
||||
@@ -0,0 +1,49 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'_stateversion_'
|
||||
p3
|
||||
I1
|
||||
sS'portfolio_value'
|
||||
p4
|
||||
F0.0
|
||||
sS'cash'
|
||||
p5
|
||||
F0.0
|
||||
sS'starting_cash'
|
||||
p6
|
||||
F0.0
|
||||
sS'returns'
|
||||
p7
|
||||
F0.0
|
||||
sS'capital_used'
|
||||
p8
|
||||
F0.0
|
||||
sS'pnl'
|
||||
p9
|
||||
F0.0
|
||||
sS'positions'
|
||||
p10
|
||||
ccopy_reg
|
||||
_reconstructor
|
||||
p11
|
||||
(czipline.protocol
|
||||
Positions
|
||||
p12
|
||||
c__builtin__
|
||||
dict
|
||||
p13
|
||||
(dp14
|
||||
tp15
|
||||
Rp16
|
||||
sS'positions_value'
|
||||
p17
|
||||
F0.0
|
||||
sS'start_date'
|
||||
p18
|
||||
NssS'initargs'
|
||||
p19
|
||||
NsS'newargs'
|
||||
p20
|
||||
Ns.
|
||||
@@ -0,0 +1,24 @@
|
||||
(dp0
|
||||
S'obj_state'
|
||||
p1
|
||||
(dp2
|
||||
S'_stateversion_'
|
||||
p3
|
||||
I1
|
||||
sS'amount'
|
||||
p4
|
||||
I0
|
||||
sS'last_sale_price'
|
||||
p5
|
||||
F0.0
|
||||
sS'cost_basis'
|
||||
p6
|
||||
F0.0
|
||||
sS'sid'
|
||||
p7
|
||||
I8554
|
||||
ssS'initargs'
|
||||
p8
|
||||
NsS'newargs'
|
||||
p9
|
||||
Ns.
|
||||
@@ -0,0 +1,136 @@
|
||||
#
|
||||
# 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 os
|
||||
import pickle
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
base_state_dir = 'tests/resources/saved_state_archive'
|
||||
|
||||
|
||||
class VersioningTestCase(TestCase):
|
||||
|
||||
def load_state_from_disk(self, cls):
|
||||
state_dir = cls.__module__ + cls.__name__
|
||||
|
||||
full_dir = base_state_dir + '/' + state_dir
|
||||
|
||||
state_files = \
|
||||
[f for f in os.listdir(full_dir) if 'State_Version_' in f]
|
||||
|
||||
for f_name in state_files:
|
||||
f = open(full_dir + '/' + f_name, 'r')
|
||||
yield pickle.load(f)
|
||||
|
||||
# Only test versioning in minutely mode right now
|
||||
@parameterized.expand([
|
||||
(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_minute,), 'to_dict'),
|
||||
(RiskMetricsCumulative, (sim_params_minute,), 'to_dict'),
|
||||
(RiskMetricsPeriod,
|
||||
(returns.index[0], returns.index[0], returns), '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')
|
||||
])
|
||||
def test_object_serialization(self,
|
||||
cls,
|
||||
initargs,
|
||||
comparison_method='dict'):
|
||||
|
||||
# Make reference object
|
||||
obj = cls(*initargs)
|
||||
|
||||
# Fetch state
|
||||
state_versions = self.load_state_from_disk(cls)
|
||||
|
||||
for version in state_versions:
|
||||
|
||||
# For each version inflate a new object and ensure that it
|
||||
# matches the original.
|
||||
|
||||
newargs = version['newargs']
|
||||
initargs = version['initargs']
|
||||
state = version['obj_state']
|
||||
|
||||
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)
|
||||
|
||||
# The ObjectId generated on instantiation of Order will
|
||||
# not be the same as the one loaded from saved state.
|
||||
if cls == Order:
|
||||
obj.__dict__['id'] = obj2.__dict__['id']
|
||||
|
||||
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__)
|
||||
Reference in New Issue
Block a user