コード例 #1
0
 def load_exodata(self):
     if 'exo_storage' in self.context['strategy']:
         storage = self.context['strategy']['exo_storage']
         self.data, self.exo_dict = storage.load_series(self.exo_name)
         if self.data is None:
             raise KeyError(
                 "EXO ('{0}') not found in EXOStorage engine".format(
                     self.exo_name))
         del self.context['strategy']['exo_storage']
     else:
         if os.path.exists(self.exo_name):
             self.data, self.exo_dict = matlab.loaddata(self.exo_name)
         else:
             TMQRPATH = os.getenv("TMQRPATH", '')
             self.data, self.exo_dict = matlab.loaddata(
                 os.path.join(TMQRPATH, 'mat',
                              self.exo_name.replace('.mat', '') + '.mat'))
            exit_rule = (CrossDown(px, trailing_stop))  # Cross down for longs

        elif direction == -1:
            exit_rule = (CrossUp(px, trailing_stop)
                         )  # Cross up for shorts, Cross down for longs

        # Swarm_member_name must be *unique* for every swarm member
        # We use params values for uniqueness
        swarm_member_name = self.get_member_name(params)

        #
        # Calculation info
        #
        calc_info = None
        if save_info:
            calc_info = {'trailing_stop': trailing_stop, 'sp_df': sp_df}

        return swarm_member_name, entry_rule, exit_rule, calc_info


if __name__ == "__main__":
    #
    #   Run this code only from direct shell execution
    #
    #strategy = StrategyMACrossTrail()
    #equity, stats = strategy.calculate()

    # Do some work
    data, info = matlab.loaddata('../mat/strategy_270225.mat')
    data.plot()
コード例 #3
0
 def load_exodata(self):
     if os.path.exists(self.exo_name):
         self.data, self.exo_dict = matlab.loaddata(self.exo_name)
     else:
         self.data, self.exo_dict = matlab.loaddata('../mat/' +
                                                    self.exo_name + '.mat')
コード例 #4
0
 def from_matfile(cls, filename):
     data, exo_info = matlab.loaddata(filename)
     return cls(data, exo_info)
コード例 #5
0
    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])
コード例 #6
0
    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])