示例#1
0
文件: euler.py 项目: yizhang7210/MaLT
    def get_best(self):
        """ Produce a best instance of this strategy.

            Args:
                void.

            Returns:
                self: Euler instance. With the params and model having the
                    highest score among all combinations.
        """
        # Log enter.
        logger.info("Euler: Selecting best for %s.", self.instrument)

        # Initialize the scores array and strategy parameters.
        scores = []
        strategy_params = util.get_euler_params()

        # TODO: Do more than 1 run.
        # Run for all predictive models.
        counter = 0
        for model in self.all_models:
            scores_row = []
            model_params = util.get_model_params(model)

            # Try different model parameters.
            for model_param in model_params:
                scores_col = []
                model = self.learner.build_model(model, 0.9, **model_param)
                pred, _ = self.learner.test_model(model)

                # And different strategy parameters, e.g. threshold.
                for strategy_param in strategy_params:
                    self.set_params(**strategy_param)

                    # Do the dry run.
                    plot_name = '{0}_{1}.png'.format(self.instrument, counter)
                    balance = self.dry_run(pred, export_plot=plot_name)

                    # Determine the quality of the params via score.
                    scores_col.append(util.get_strategy_score(balance))
                    counter += 1

                scores_row.append(scores_col)

            scores.append(scores_row)

        # Get the best and set the parameters to the best.
        best = np.unravel_index(np.argmax(scores), np.array(scores).shape)

        model = self.all_models[best[0]]
        model_param = util.get_model_params(model)[best[1]]
        model = self.learner.build_model(model, 1, **model_param)

        logger.info("Best score is: %s.", str(np.array(scores)[best]))
        self.set_params(**strategy_params[best[2]])
        self.model = model

        return self
示例#2
0
    def test_strategy_score(self):
        """ Make sure the strategy score is calculated properly."""
        # Make up some arbitrary balance vector.
        balance = [0, 4.5, 8, -19, -8, 4, 5, -1, 4, 5, 33, 2, 0, 0, 3, 3, 3]
        balance = np.array(balance)
        score = util.get_strategy_score(balance)

        # Check the score.
        self.assertEqual(score, 11.0/17)

        return