def run(self, config):
        logger.init_logger(config.log_uri)

        # Load market data
        log.info('Loading data...')
        data = market_data.load_market_data(config.tickers, config.ticker_types, config.data_sources, \
            config.start_date, config.end_date, config.history_window, config.csv_data_uri)
        log.info('Data loaded!')
        print

        # Create the trading algorithm w/o parameters
        trading_algorithm = TradingAlgorithm.create_trading_algorithm(config.algorithm_uri, config.tickers, \
            config.history_window)

        # Setup and run the optimizer
        optimizer = of.create_optimizer(
            config.num_processors, config.optimizer_name, trading_algorithm,
            config.commission, config.ticker_spreads,
            config.optimization_metric, config.optimization_metric_ascending,
            config.optimization_parameters, config.time_resolution)
        log.info('Running the optimizer...')
        optimizer.run(data, config.start_date, config.end_date)
        log.info('Ran optimizer!')
        print

        return optimizer.results
Exemple #2
0
    def test_load_market_data_from_all_data_sources(self):
        # Setup input values for all data sources
        tickers = [
            'INDEX_SPY',
            'SPY',
        ]
        ticker_types = [
            'YAHOO',
            'dummy',
        ]
        data_sources = [
            'Quandl',
            'CSV',
        ]
        start_date = pd.to_datetime('2015-12-01')
        end_date = pd.to_datetime('2016-01-29')
        history_window = 0
        csv_data_uri = './support_files'

        # Load market data
        data = market_data.load_market_data(tickers, ticker_types,
                                            data_sources, start_date, end_date,
                                            history_window, csv_data_uri)

        # Check results
        self.assertEqual(len(data), 2)
        self.assertEqual(data['INDEX_SPY'].index[0], start_date)
        self.assertEqual(data['INDEX_SPY'].index[-1], end_date)
        self.assertEqual(data['SPY'].index[0], start_date)
        self.assertEqual(data['SPY'].index[-1], end_date)
    def test_grid_search_optimizer_as_expected(self):
        # Initialize market data loading values
        tickers = ['SPY']
        ticker_types = ['']
        data_sources = ['CSV']
        start_date = pd.to_datetime('2016-01-01')
        end_date = pd.to_datetime('2016-5-31')
        history_window = 20
        csv_data_uri = "support_files"

        # Load market data
        data = market_data.load_market_data(tickers, ticker_types,
                                            data_sources, start_date, end_date,
                                            history_window, csv_data_uri)

        # Initialize grid search optimizer values
        algorithm_uri = "support_files/MovingAverageDivergenceAlgorithm.py"
        num_processors = 4
        commission = 0.0
        ticker_spreads = [0.0001]
        optimizer_name = "GridSearchOptimizer"
        optimization_metric = "sharpe_ratio"
        optimization_metric_ascending = True
        optimization_parameters = {
            "ma_long_window": [10, 20, 2],
            "ma_short_window": [2, 5, 2],
            "open_long": [-0.25, -0.25, 1],
            "close_long": [0.4, 0.4, 1]
        }
        time_resolution = "daily"

        # Create trading algorithm
        trading_algorithm = TradingAlgorithm.create_trading_algorithm(
            algorithm_uri, tickers, history_window, None)

        # Setup and run the optimizer
        optimizer = of.create_optimizer(num_processors, optimizer_name,
                                        trading_algorithm, commission,
                                        ticker_spreads, optimization_metric,
                                        optimization_metric_ascending,
                                        optimization_parameters,
                                        time_resolution)
        results = optimizer.run(data, start_date, end_date)

        # Manually compute optimal parameters
        opt_params = Optimizer.get_optimal_parameters(
            results.backtest_results, optimization_metric,
            results.parameter_sets, optimization_metric_ascending,
            time_resolution)

        # Check results
        self.assertEqual(opt_params, results.optimal_parameters)
        self.assertTrue(
            len(results.backtest_results) == len(results.parameter_sets))
        self.assertEqual(4, len(results.backtest_results))
Exemple #4
0
    def setUp(self):
        # Initialize market data loading values
        tickers = ['SPY']
        ticker_types = ['']
        data_sources = ['CSV']
        start_date = pd.to_datetime('1993-01-29')
        end_date = pd.to_datetime('2016-5-31')
        history_window = 0
        csv_data_uri = "support_files"

        # Load market data and save for later use
        data = market_data.load_market_data(tickers, ticker_types,
                                            data_sources, start_date, end_date,
                                            history_window, csv_data_uri)
        self.test_series = data['SPY']['Adjusted Close']
Exemple #5
0
    def test_load_market_data_with_non_existent_data(self):
        # Setup input values for all data sources
        tickers = [
            'DAX',
        ]
        ticker_types = [
            'dummy',
        ]
        data_sources = [
            'CSV',
        ]
        start_date = pd.to_datetime('1987-08-27')
        end_date = pd.to_datetime('1989-02-17')
        history_window = 0
        csv_data_uri = './support_files'

        # Load market data
        with self.assertRaises(ValueError):
            data = market_data.load_market_data(tickers, ticker_types, data_sources, start_date, \
                end_date, history_window, csv_data_uri)
Exemple #6
0
    def run(self, config):
        logger.init_logger(config.log_uri)

        # Estimate data count needed prior to first out-of-sample period
        freq_factor = self._frequency_diff_factor(config.time_resolution,
                                                  config.sample_period)
        data_request_history_window = (config.in_sample_periods *
                                       freq_factor) + config.history_window

        # Load market data
        log.info('Loading data...')
        data = market_data.load_market_data(config.tickers, config.ticker_types, config.data_sources, \
            config.start_date, config.end_date, data_request_history_window, config.csv_data_uri)
        log.info('Data loaded!')
        print

        # Create the trading algorithm w/o parameters
        trading_algorithm = TradingAlgorithm.create_trading_algorithm(config.algorithm_uri, config.tickers, \
            config.history_window)

        # Create the optimizer
        optimizer = of.create_optimizer(config.num_processors, config.optimizer_name, trading_algorithm, config.commission, \
            config.ticker_spreads, config.optimization_metric, config.optimization_metric_ascending, config.optimization_parameters,
            config.time_resolution)

        # Create the backtester
        backtester = b.Backtester(-1, trading_algorithm, config.cash,
                                  config.commission, config.ticker_spreads)

        # Setup and run the walk forward analyzer
        walk_forward_analyzer = WalkForwardAnalyzer(config.in_sample_periods, config.out_of_sample_periods, config.sample_period, \
            optimizer, backtester)

        log.info('Running the walk forward analyzer...')
        walk_forward_analyzer.run(data, config.start_date, config.end_date,
                                  config.cash)
        log.info('Ran the walk forward analyzer!')
        print

        return walk_forward_analyzer.results
Exemple #7
0
    def run(self, config):
        logger.init_logger(config.log_uri)

        # Load market data
        log.info('Loading data...')
        data = market_data.load_market_data(config.tickers, config.ticker_types, config.data_sources, \
            config.start_date, config.end_date, config.history_window, config.csv_data_uri)
        log.info('Data loaded!')
        print

        # Create the trading algorithm
        trading_algorithm = TradingAlgorithm.create_trading_algorithm(config.algorithm_uri, config.tickers, \
            config.history_window, config.algorithm_parameters)

        # Setup and run the backtester
        backtester = Backtester(0, trading_algorithm, config.cash,
                                config.commission, config.ticker_spreads)
        log.info('Running the backtester...')
        backtester.run(data, config.start_date, config.end_date)
        log.info('Ran backtester!')
        print

        return backtester.results
Exemple #8
0
    def test_load_market_data_with_same_ticker(self):
        # Setup input values for all data sources
        tickers = [
            'SPY',
            'SPY',
        ]
        ticker_types = [
            'dummy',
            'dummy',
        ]
        data_sources = [
            'CSV',
            'CSV',
        ]
        start_date = pd.to_datetime('2015-12-01')
        end_date = pd.to_datetime('2016-01-29')
        history_window = 0
        csv_data_uri = './support_files'

        # Load market data
        with self.assertRaises(ValueError):
            data = market_data.load_market_data(tickers, ticker_types, data_sources, start_date, \
                end_date, history_window, csv_data_uri)