def test_lse_algorithm(self): lse = trading.TradingEnvironment( bm_symbol='^FTSE', exchange_tz='Europe/London' ) with lse: sim_params = factory.create_simulation_parameters( start=datetime(2012, 5, 1, tzinfo=pytz.utc), end=datetime(2012, 6, 30, tzinfo=pytz.utc) ) algo = TestAlgo(self, sim_params=sim_params) trade_source = factory.create_daily_trade_source( [8229], 200, sim_params ) algo.set_sources([trade_source]) gen = algo.get_generator() results = list(gen) self.assertEqual(len(results), 42) # May 7, 2012 was an LSE holiday, confirm the 4th trading # day was May 8. self.assertEqual(results[4]['daily_perf']['period_open'], datetime(2012, 5, 8, 8, 30, tzinfo=pytz.utc))
def test_factory_daily(self): sim_params = factory.create_simulation_parameters() trade_source = factory.create_daily_trade_source([133], sim_params) prev = None for trade in trade_source: if prev: self.assertTrue(trade.dt > prev.dt) prev = trade
def test_factory_daily(self): sim_params = factory.create_simulation_parameters() trade_source = factory.create_daily_trade_source([133], 200, sim_params) prev = None for trade in trade_source: if prev: self.assertTrue(trade.dt > prev.dt) prev = trade
def test_factory_daily(self): trading_environment = factory.create_trading_environment() trade_source = factory.create_daily_trade_source([133], 200, trading_environment) prev = None for trade in trade_source: if prev: self.assertTrue(trade.dt > prev.dt) prev = trade
def test_factory_daily(self): trading_environment = factory.create_trading_environment() trade_source = factory.create_daily_trade_source( [133], 200, trading_environment ) prev = None for trade in trade_source: if prev: self.assertTrue(trade.dt > prev.dt) prev = trade
def test_progress(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ sim_params = factory.create_simulation_parameters( start=datetime(2008, 1, 1, tzinfo=pytz.utc), end=datetime(2008, 1, 5, tzinfo=pytz.utc)) algo = TestAlgo(self, sim_params=sim_params) trade_source = factory.create_daily_trade_source([8229], 3, sim_params) algo.set_sources([trade_source]) gen = algo.get_generator() results = list(gen) self.assertEqual(results[-2]['progress'], 1.0)
def test_progress(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ sim_params = factory.create_simulation_parameters( start=datetime(2008, 1, 1, tzinfo=pytz.utc), end=datetime(2008, 1, 5, tzinfo=pytz.utc) ) algo = TestAlgo(self, sim_params=sim_params) trade_source = factory.create_daily_trade_source([8229], 3, sim_params) algo.set_sources([trade_source]) gen = algo.get_generator() results = list(gen) self.assertEqual(results[-2]["progress"], 1.0)
def test_generator_dates(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ sim_params = factory.create_simulation_parameters( start=datetime(2011, 7, 30, tzinfo=pytz.utc), end=datetime(2012, 7, 30, tzinfo=pytz.utc)) algo = TestAlgo(self, identifiers=[8229], sim_params=sim_params) trade_source = factory.create_daily_trade_source([8229], sim_params) algo.set_sources([trade_source]) gen = algo.get_generator() self.assertTrue(list(gen)) self.assertTrue(algo.slippage.latest_date) self.assertTrue(algo.latest_date)
def test_generator_dates(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ sim_params = factory.create_simulation_parameters( start=datetime(2011, 7, 30, tzinfo=pytz.utc), end=datetime(2012, 7, 30, tzinfo=pytz.utc) ) algo = TestAlgo(self, sim_params=sim_params) trade_source = factory.create_daily_trade_source([8229], 200, sim_params) algo.set_sources([trade_source]) gen = algo.get_generator() self.assertTrue(list(gen)) self.assertTrue(algo.slippage.latest_date) self.assertTrue(algo.latest_date)
def test_generator_dates(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ algo = TestAlgo(self) trading_environment = factory.create_trading_environment( start=datetime(2012, 1, 3, tzinfo=pytz.utc), end=datetime(2012, 7, 30, tzinfo=pytz.utc)) trade_source = factory.create_daily_trade_source([8229], 200, trading_environment) algo.set_sources([trade_source]) gen = algo.get_generator(trading_environment) self.assertTrue(list(gen)) self.assertTrue(algo.slippage.latest_date) self.assertTrue(algo.latest_date)
def test_lse_algorithm(self): lse = trading.TradingEnvironment(bm_symbol='^FTSE', exchange_tz='Europe/London') with lse: sim_params = factory.create_simulation_parameters( start=datetime(2012, 5, 1, tzinfo=pytz.utc), end=datetime(2012, 6, 30, tzinfo=pytz.utc)) algo = TestAlgo(self, sim_params=sim_params) trade_source = factory.create_daily_trade_source([8229], 200, sim_params) algo.set_sources([trade_source]) gen = algo.get_generator() results = list(gen) self.assertEqual(len(results), 42) # May 7, 2012 was an LSE holiday, confirm the 4th trading # day was May 8. self.assertEqual(results[4]['daily_perf']['period_open'], datetime(2012, 5, 8, 8, 31, tzinfo=pytz.utc))
def test_generator_dates(self): """ Ensure the pipeline of generators are in sync, at least as far as their current dates. """ algo = TestAlgo(self) trading_environment = factory.create_trading_environment( start=datetime(2011, 7, 30, tzinfo=pytz.utc), end=datetime(2012, 7, 30, tzinfo=pytz.utc) ) trade_source = factory.create_daily_trade_source( [8229], 200, trading_environment ) algo.set_sources([trade_source]) gen = algo.get_generator(trading_environment) self.assertTrue(list(gen)) self.assertTrue(algo.slippage.latest_date) self.assertTrue(algo.latest_date)
def create_test_zipline(**config): """ :param config: A configuration object that is a dict with: - environment - a \ :py:class:`zipline.finance.trading.TradingEnvironment` - sid - an integer, which will be used as the security ID. - order_count - the number of orders the test algo will place, defaults to 100 - order_amount - the number of shares per order, defaults to 100 - trade_count - the number of trades to simulate, defaults to 101 to ensure all orders are processed. - algorithm - optional parameter providing an algorithm. defaults to :py:class:`zipline.test.algorithms.TestAlgorithm` - trade_source - optional parameter to specify trades, if present. If not present :py:class:`zipline.sources.SpecificEquityTrades` is the source, with daily frequency in trades. - slippage: optional parameter that configures the :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. Expects an object with a simulate mehod, such as :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. :py:mod:`zipline.finance.trading` - transforms: optional parameter that provides a list of StatefulTransform objects. """ assert isinstance(config, dict) sid_list = config.get('sid_list') if not sid_list: sid = config.get('sid') sid_list = [sid] concurrent_trades = config.get('concurrent_trades', False) #-------------------- # Trading Environment #-------------------- if 'environment' in config: trading_environment = config['environment'] else: trading_environment = factory.create_trading_environment() if 'order_count' in config: order_count = config['order_count'] else: order_count = 100 if 'order_amount' in config: order_amount = config['order_amount'] else: order_amount = 100 if 'trade_count' in config: trade_count = config['trade_count'] else: # to ensure all orders are filled, we provide one more # trade than order trade_count = 101 #------------------- # Create the Algo #------------------- if 'algorithm' in config: test_algo = config['algorithm'] else: test_algo = TestAlgorithm( sid, order_amount, order_count ) #------------------- # Trade Source #------------------- if 'trade_source' in config: trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( sid_list, trade_count, trading_environment, concurrent=concurrent_trades ) test_algo.set_sources([trade_source]) #------------------- # Transforms #------------------- transforms = config.get('transforms', None) if transforms is not None: test_algo.set_transforms(transforms) #------------------- # Slippage # ------------------ slippage = config.get('slippage', None) if slippage is not None: test_algo.set_slippage(slippage) # ------------------ # generator/simulator sim = test_algo.get_generator(trading_environment) return sim
def create_test_zipline(**config): """ :param config: A configuration object that is a dict with: - sid - an integer, which will be used as the asset ID. - order_count - the number of orders the test algo will place, defaults to 100 - order_amount - the number of shares per order, defaults to 100 - trade_count - the number of trades to simulate, defaults to 101 to ensure all orders are processed. - algorithm - optional parameter providing an algorithm. defaults to :py:class:`zipline.test.algorithms.TestAlgorithm` - trade_source - optional parameter to specify trades, if present. If not present :py:class:`zipline.sources.SpecificEquityTrades` is the source, with daily frequency in trades. - slippage: optional parameter that configures the :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. Expects an object with a simulate mehod, such as :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. :py:mod:`zipline.finance.trading` """ assert isinstance(config, dict) try: sid_list = config['sid_list'] except KeyError: try: sid_list = [config['sid']] except KeyError: raise Exception("simfactory create_test_zipline() requires " "argument 'sid_list' or 'sid'") concurrent_trades = config.get('concurrent_trades', False) if 'order_count' in config: order_count = config['order_count'] else: order_count = 100 if 'order_amount' in config: order_amount = config['order_amount'] else: order_amount = 100 # ------------------- # Create the Algo # ------------------- if 'algorithm' in config: test_algo = config['algorithm'] else: test_algo = TestAlgorithm(sid_list[0], order_amount, order_count, sim_params=config.get( 'sim_params', factory.create_simulation_parameters()), slippage=config.get('slippage'), identifiers=sid_list) # ------------------- # Trade Source # ------------------- if 'trade_source' in config: trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( sid_list, test_algo.sim_params, concurrent=concurrent_trades) if trade_source: test_algo.set_sources([trade_source]) # ------------------- # Benchmark source # ------------------- test_algo.benchmark_return_source = config.get('benchmark_source', None) # ------------------ # generator/simulator sim = test_algo.get_generator() return sim
def create_test_zipline(**config): """ :param config: A configuration object that is a dict with: - sid - an integer, which will be used as the asset ID. - order_count - the number of orders the test algo will place, defaults to 100 - order_amount - the number of shares per order, defaults to 100 - trade_count - the number of trades to simulate, defaults to 101 to ensure all orders are processed. - algorithm - optional parameter providing an algorithm. defaults to :py:class:`zipline.test.algorithms.TestAlgorithm` - trade_source - optional parameter to specify trades, if present. If not present :py:class:`zipline.sources.SpecificEquityTrades` is the source, with daily frequency in trades. - slippage: optional parameter that configures the :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. Expects an object with a simulate mehod, such as :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. :py:mod:`zipline.finance.trading` """ assert isinstance(config, dict) try: sid_list = config['sid_list'] except KeyError: try: sid_list = [config['sid']] except KeyError: raise Exception("simfactory create_test_zipline() requires " "argument 'sid_list' or 'sid'") concurrent_trades = config.get('concurrent_trades', False) if 'order_count' in config: order_count = config['order_count'] else: order_count = 100 if 'order_amount' in config: order_amount = config['order_amount'] else: order_amount = 100 # ------------------- # Create the Algo # ------------------- if 'algorithm' in config: test_algo = config['algorithm'] else: test_algo = TestAlgorithm( sid_list[0], order_amount, order_count, sim_params=config.get('sim_params', factory.create_simulation_parameters()), slippage=config.get('slippage'), identifiers=sid_list ) # ------------------- # Trade Source # ------------------- if 'trade_source' in config: trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( sid_list, test_algo.sim_params, test_algo.trading_environment, concurrent=concurrent_trades, ) if trade_source: test_algo.set_sources([trade_source]) # ------------------- # Benchmark source # ------------------- test_algo.benchmark_return_source = config.get('benchmark_source', None) # ------------------ # generator/simulator sim = test_algo.get_generator() return sim
def create_test_zipline(**config): """ :param config: A configuration object that is a dict with: - sid - an integer, which will be used as the security ID. - order_count - the number of orders the test algo will place, defaults to 100 - order_amount - the number of shares per order, defaults to 100 - trade_count - the number of trades to simulate, defaults to 101 to ensure all orders are processed. - algorithm - optional parameter providing an algorithm. defaults to :py:class:`zipline.test.algorithms.TestAlgorithm` - trade_source - optional parameter to specify trades, if present. If not present :py:class:`zipline.sources.SpecificEquityTrades` is the source, with daily frequency in trades. - slippage: optional parameter that configures the :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. Expects an object with a simulate mehod, such as :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. :py:mod:`zipline.finance.trading` - transforms: optional parameter that provides a list of StatefulTransform objects. """ assert isinstance(config, dict) sid_list = config.get('sid_list') if not sid_list: sid = config.get('sid') sid_list = [sid] concurrent_trades = config.get('concurrent_trades', False) if 'order_count' in config: order_count = config['order_count'] else: order_count = 100 if 'order_amount' in config: order_amount = config['order_amount'] else: order_amount = 100 if 'trade_count' in config: trade_count = config['trade_count'] else: # to ensure all orders are filled, we provide one more # trade than order trade_count = 101 #------------------- # Create the Algo #------------------- if 'algorithm' in config: test_algo = config['algorithm'] else: test_algo = TestAlgorithm(sid, order_amount, order_count, sim_params=config.get( 'sim_params', factory.create_simulation_parameters())) #------------------- # Trade Source #------------------- if 'trade_source' in config: trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( sid_list, trade_count, test_algo.sim_params, concurrent=concurrent_trades) if trade_source: test_algo.set_sources([trade_source]) #------------------- # Benchmark source #------------------- test_algo.benchmark_return_source = config.get('benchmark_source', None) #------------------- # Transforms #------------------- transforms = config.get('transforms', None) if transforms is not None: test_algo.set_transforms(transforms) #------------------- # Slippage # ------------------ slippage = config.get('slippage', None) if slippage is not None: test_algo.set_slippage(slippage) # ------------------ # generator/simulator sim = test_algo.get_generator() return sim
def create_test_zipline(**config): """ :param config: A configuration object that is a dict with: - sid - an integer, which will be used as the asset ID. - order_count - the number of orders the test algo will place, defaults to 100 - order_amount - the number of shares per order, defaults to 100 - trade_count - the number of trades to simulate, defaults to 101 to ensure all orders are processed. - algorithm - optional parameter providing an algorithm. defaults to :py:class:`zipline.test.algorithms.TestAlgorithm` - trade_source - optional parameter to specify trades, if present. If not present :py:class:`zipline.sources.SpecificEquityTrades` is the source, with daily frequency in trades. - slippage: optional parameter that configures the :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. Expects an object with a simulate mehod, such as :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. :py:mod:`zipline.finance.trading` """ assert isinstance(config, dict) try: sid_list = config['sid_list'] except KeyError: try: sid_list = [config['sid']] except KeyError: raise Exception("simfactory create_test_zipline() requires " "argument 'sid_list' or 'sid'") concurrent_trades = config.get('concurrent_trades', False) order_count = config.get('order_count', 100) order_amount = config.get('order_amount', 100) # # 更改为A股交易日历 trading_calendar = config.get('trading_calendar', get_calendar()) # ------------------- # Create the Algo # ------------------- if 'algorithm' in config: test_algo = config['algorithm'] else: test_algo = TestAlgorithm(sid_list[0], order_amount, order_count, sim_params=config.get( 'sim_params', factory.create_simulation_parameters()), trading_calendar=trading_calendar, slippage=config.get('slippage'), identifiers=sid_list) # ------------------- # Trade Source # ------------------- if 'skip_data' not in config: if 'trade_source' in config: trade_source = config['trade_source'] else: trade_source = factory.create_daily_trade_source( sid_list, test_algo.sim_params, test_algo.trading_environment, trading_calendar, concurrent=concurrent_trades, ) trades_by_sid = {} for trade in trade_source: if trade.sid not in trades_by_sid: trades_by_sid[trade.sid] = [] trades_by_sid[trade.sid].append(trade) data_portal = create_data_portal_from_trade_history( config['env'].asset_finder, trading_calendar, config['tempdir'], config['sim_params'], trades_by_sid) test_algo.data_portal = data_portal # ------------------- # Benchmark source # ------------------- test_algo.benchmark_return_source = config.get('benchmark_source', None) # ------------------ # generator/simulator sim = test_algo.get_generator() return sim