def test_laststate_update_recalculated_exo_price(self): STRATEGY_CONTEXT = { 'strategy': { 'class': StrategyMACrossTrail, 'exo_name': './mat/strategy_270225.mat', 'direction': -1, 'opt_params': [ # OptParam(name, default_value, min_value, max_value, step) OptParamArray('Direction', [-1]), OptParam('SlowMAPeriod', 20, 10, 40, 5), OptParam('FastMAPeriod', 2, 5, 20, 5), OptParam('MedianPeriod', 5, 2, 10, 1) ], }, 'swarm': { 'members_count': 3, 'ranking_class': RankerHighestReturns(return_period=1), 'rebalance_time_function': SwarmRebalance.every_friday }, } swm_full = Swarm(STRATEGY_CONTEXT) swm_full.run_swarm() swm_full.pick() swm_start = Swarm(STRATEGY_CONTEXT) swm_start.strategy.data.at[pd.Timestamp('2016-03-04'), 'exo'] += 10 swm_start.strategy.data = swm_start.strategy.data.ix[:'2016-03-04'] swm_start.run_swarm() swm_start.pick() ctx = deepcopy(STRATEGY_CONTEXT) ctx['strategy']['opt_preset'] = Swarm._parse_params(swm_start.last_members_list) swm_next = Swarm(ctx) swm_next.strategy.data = swm_next.strategy.data.ix[:'2016-03-11'] swm_next.run_swarm() # Make sure that old EXO price used self.assertEqual(-2, swm_start.last_exposure) self.assertEqual(swm_full.picked_equity.ix['2016-03-04'], swm_start.picked_equity.ix['2016-03-04'] - 10*swm_start.last_exposure) # After this run swm_start._laststate_update(swm_next.strategy.data, swm_next.raw_exposure.sum(axis=1)) self.assertEqual(swm_full.picked_equity.ix['2016-03-04'], swm_start.picked_equity.ix['2016-03-04']) self.assertAlmostEqual(swm_full.picked_equity.ix['2016-03-07'], swm_start.picked_equity.ix['2016-03-07']) self.assertAlmostEqual(swm_full.picked_equity.ix['2016-03-10'], swm_start.picked_equity.ix['2016-03-10']) self.assertAlmostEqual(swm_full.picked_equity.ix['2016-03-11'], swm_start.picked_equity.ix['2016-03-11']) self.assertEqual(-3, swm_start.last_exposure)
def test_laststate_update_handle_if_swarm_composition_is_empty_after_rebalance_with_costs(self, mock_get_costs): STRATEGY_CONTEXT = { 'strategy': { 'class': StrategyMACrossTrail, 'exo_name': './mat/strategy_270225.mat', 'direction': -1, 'opt_params': [ # OptParam(name, default_value, min_value, max_value, step) OptParamArray('Direction', [-1]), OptParam('SlowMAPeriod', 20, 10, 40, 5), OptParam('FastMAPeriod', 2, 5, 20, 5), OptParam('MedianPeriod', 5, 2, 10, 1) ], }, 'swarm': { 'members_count': 3, 'ranking_class': RankerHighestReturns(return_period=1), 'rebalance_time_function': SwarmRebalance.every_friday }, } # # Mocking the CostsManagerEXOFixed.get_costs # from backtester.matlab import loaddata exo_df, info = loaddata('./mat/strategy_270225.mat') mock_get_costs.return_value = pd.DataFrame({'rollover_costs': np.zeros(len(exo_df.index)), 'transaction_costs': np.ones(len(exo_df.index)) * 10}, index=exo_df.index) swm_full = Swarm(STRATEGY_CONTEXT) swm_full.strategy.data.at[pd.Timestamp('2016-03-04'), 'exo'] = swm_full.strategy.data.ix['2016-03-03']['exo'] swm_full.strategy.data.loc[:, 'delta'] = 1.0 swm_full.run_swarm() swm_full.pick() swm_start = Swarm(STRATEGY_CONTEXT) swm_start.strategy.data.at[pd.Timestamp('2016-03-04'), 'exo'] = swm_full.strategy.data.ix['2016-03-03']['exo'] swm_start.strategy.data = swm_start.strategy.data.ix[:'2016-03-04'] swm_start.strategy.data.loc[:, 'delta'] = 1.0 swm_start.run_swarm() swm_start.pick() ctx = deepcopy(STRATEGY_CONTEXT) ctx['strategy']['opt_preset'] = Swarm._parse_params(swm_start.last_members_list) swm_next = Swarm(ctx) swm_next.strategy.data.at[pd.Timestamp('2016-03-04'), 'exo'] = swm_full.strategy.data.ix['2016-03-03']['exo'] swm_next.strategy.data = swm_next.strategy.data.ix[:'2016-03-11'] swm_next.strategy.data.loc[:, 'delta'] = 1.0 swm_next.run_swarm() # Make sure that old EXO price used self.assertEqual(-2, swm_start.last_exposure) self.assertEqual(swm_full.picked_equity.ix['2016-03-03'], swm_start.picked_equity.ix['2016-03-04']) # After this run with patch('warnings.warn') as mock_warn: swm_start._laststate_update(swm_next.strategy.data, swm_next.raw_exposure.sum(axis=1), swm_next.strategy.costs) self.assertTrue(mock_warn.called) dt = '2016-03-04' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-07' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-08' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-09' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-10' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-11' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt])
def test_laststate_update_real_with_costs(self, mock_get_costs): from backtester.matlab import loaddata STRATEGY_CONTEXT = { 'strategy': { 'class': StrategyMACrossTrail, 'exo_name': './mat/strategy_270225.mat', 'direction': -1, 'opt_params': [ # OptParam(name, default_value, min_value, max_value, step) OptParamArray('Direction', [-1]), OptParam('SlowMAPeriod', 20, 10, 40, 5), OptParam('FastMAPeriod', 2, 5, 20, 5), OptParam('MedianPeriod', 5, 2, 10, 1) ], }, 'swarm': { 'members_count': 3, 'ranking_class': RankerHighestReturns(return_period=1), 'rebalance_time_function': SwarmRebalance.every_friday }, 'costs': { 'manager': CostsManagerEXOFixed, 'context': { 'costs_options': 3.0, 'costs_futures': 3.0, } } } # # Mocking the CostsManagerEXOFixed.get_costs # exo_df, info = loaddata('./mat/strategy_270225.mat') mock_get_costs.return_value = pd.DataFrame({'rollover_costs': np.zeros(len(exo_df.index)), 'transaction_costs': np.ones(len(exo_df.index)) * 10}, index=exo_df.index) swm_full = Swarm(STRATEGY_CONTEXT) swm_full.strategy.data.loc[:, 'delta'] = 1.0 swm_full.run_swarm() swm_full.pick() swm_start = Swarm(STRATEGY_CONTEXT) swm_start.strategy.data = swm_start.strategy.data.ix[:'2016-03-18'] swm_start.strategy.data.loc[:, 'delta'] = 1.0 swm_start.run_swarm() swm_start.pick() ctx = deepcopy(STRATEGY_CONTEXT) ctx['strategy']['opt_preset'] = Swarm._parse_params(swm_start.last_members_list) swm_next = Swarm(ctx) swm_next.strategy.data = swm_next.strategy.data.ix[:'2016-03-25'] swm_next.strategy.data.loc[:, 'delta'] = 1.0 swm_next.run_swarm() dt = '2016-03-18' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(np.isnan(swm_full.series['delta'].ix[dt]), np.isnan(swm_start.series['delta'].ix[dt])) self.assertEqual(swm_start.last_exposure, swm_full.picked_exposure.sum(axis=1).ix['2016-03-18']) # Updating swm_start (assuming that it was loaded from DB) swm_start._laststate_update(swm_next.strategy.data, swm_next.raw_exposure.sum(axis=1), swm_next.strategy.costs) dt = '2016-03-21' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-22' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-23' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-24' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt]) dt = '2016-03-25' self.assertEqual(swm_full.series['equity'].ix[dt], swm_start.series['equity'].ix[dt]) self.assertEqual(swm_full.series['exposure'].ix[dt], swm_start.series['exposure'].ix[dt]) self.assertEqual(swm_full.series['costs'].ix[dt], swm_start.series['costs'].ix[dt]) self.assertEqual(swm_full.series['delta'].ix[dt], swm_start.series['delta'].ix[dt])