async def test_collect_valid_data(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["ETH/BTC"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True) as collector: assert collector.time_frames == [] assert collector.symbols == symbols assert collector.exchange_name == BINANCE assert collector.tentacles_setup_config == tentacles_setup_config await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert isinstance(collector.exchange, tentacles_exchanges.Binance) assert collector.file_path is not None assert collector.temp_file_path is not None assert not os.path.isfile(collector.temp_file_path) assert os.path.isfile(collector.file_path) async with collector_database(collector) as database: ohlcv = await database.select(enums.ExchangeDataTables.OHLCV) # use > to take into account new possible candles since collect max time is not specified assert len(ohlcv) > 6000 h_ohlcv = await database.select(enums.ExchangeDataTables.OHLCV, time_frame="1h") assert len(h_ohlcv) == BINANCE_MAX_CANDLES_COUNT eth_btc_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="ETH/BTC") assert len(eth_btc_ohlcv) == len(ohlcv)
async def run_independent_backtesting(data_files, timeout=10, use_loggers=True, run_on_common_part_only=True): independent_backtesting = None try: config_to_use = load_test_config() if use_loggers: init_logger() independent_backtesting = create_independent_backtesting( config_to_use, load_test_tentacles_config(), data_files, "", run_on_common_part_only=run_on_common_part_only) await initialize_and_run_independent_backtesting( independent_backtesting, log_errors=False) await independent_backtesting.join_backtesting_updater(timeout) return independent_backtesting except MissingTimeFrame: # ignore this exception: is due to missing of the only required time frame return independent_backtesting except asyncio.TimeoutError as e: get_logger().exception( e, True, f"Timeout after waiting for backtesting for {timeout} seconds.") # stop backtesting to prevent zombie tasks await stop_independent_backtesting(independent_backtesting) raise except Exception as e: get_logger().exception(e, True, str(e)) # stop backtesting to prevent zombie tasks await stop_independent_backtesting(independent_backtesting) raise
async def test_collect_valid_date_range(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["ETH/BTC"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True, 1549065660000, 1549670520000) as collector: assert collector.start_timestamp is not None assert collector.end_timestamp is not None await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert isinstance(collector.exchange, tentacles_exchanges.Binance) assert collector.file_path is not None assert collector.temp_file_path is not None assert os.path.isfile(collector.file_path) assert not os.path.isfile(collector.temp_file_path) async with collector_database(collector) as database: ohlcv = await database.select(enums.ExchangeDataTables.OHLCV) assert len(ohlcv) == 16833 h_ohlcv = await database.select(enums.ExchangeDataTables.OHLCV, time_frame="1h") assert len(h_ohlcv) == 168 eth_btc_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="ETH/BTC") assert len(eth_btc_ohlcv) == len(ohlcv) min_timestamp = (await database.select_min( enums.ExchangeDataTables.OHLCV, ["timestamp"], time_frame="1m"))[0][0] * 1000 assert min_timestamp <= 1549065720000 max_timestamp = (await database.select_max( enums.ExchangeDataTables.OHLCV, ["timestamp"]))[0][0] * 1000 assert max_timestamp <= 1549843200000
async def initialize(self, data_file=None): self.time_frame = None self.evaluator = self.TA_evaluator_class( test_utils_config.load_test_tentacles_config()) patch.object(self.evaluator, 'get_exchange_symbol_data', new=self._mocked_get_exchange_symbol_data) self.data_bank = DataBank(data_file) await self.data_bank.initialize() self._assert_init() self.previous_move_stop = None
async def test_collect_invalid_data(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["___ETH/BTC"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True) as collector: with pytest.raises(errors.DataCollectorError): await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert collector.exchange is not None assert collector.file_path is not None assert collector.temp_file_path is not None assert not os.path.isfile(collector.temp_file_path)
async def create_minimalist_unconnected_octobot(): # import here to prevent later web interface import issues octobot_instance = octobot.OctoBot(test_config.load_test_config(dict_only=False)) octobot_instance.initialized = True tentacles_config = test_utils_config.load_test_tentacles_config() loaders.reload_tentacle_by_tentacle_class() octobot_instance.task_manager.async_loop = asyncio.get_event_loop() octobot_instance.task_manager.create_pool_executor() octobot_instance.tentacles_setup_config = tentacles_config octobot_instance.configuration_manager.add_element(octobot_constants.TENTACLES_SETUP_CONFIG_KEY, tentacles_config) octobot_instance.exchange_producer = trading_producers.ExchangeProducer(None, octobot_instance, None, False) octobot_instance.evaluator_producer = octobot_producers.EvaluatorProducer(None, octobot_instance) octobot_instance.evaluator_producer.matrix_id = await evaluator_api.initialize_evaluators(octobot_instance.config, tentacles_config) return octobot_instance
def test_find_optimal_configuration(): with mock.patch.object(strategy_optimizer, "StrategyTestSuite", StrategyTestSuiteMock()) as test_suite_mock, \ mock.patch.object(builtins, "print", mock.Mock()) as print_mock: strategy_name = tentacles_strategies.SimpleStrategyEvaluator.get_name() optimizer = strategy_optimizer.StrategyOptimizer(test_config.load_test_config(), test_utils_config.load_test_tentacles_config(), strategy_name) optimizer.find_optimal_configuration() assert optimizer.total_nb_runs == 21 assert test_suite_mock.call_count == optimizer.total_nb_runs assert print_mock.call_count == optimizer.total_nb_runs * 2 # check each call has been different # iterate over each second print call to check run config (each strategy optimizer run prints twice) for call in print_mock.call_args_list[::2]: assert len([c for c in print_mock.call_args_list if c == call]) == 1
def _create_evaluator_with_supported_channel_signals(): evaluator = Social.TelegramChannelSignalEvaluator( test_utils_config.load_test_tentacles_config()) evaluator.logger = logging.get_logger(evaluator.get_name()) evaluator.channels_config = { "TEST-CHAN-1": { "signal_pattern": "Signal coin is : (.*)", "signal_market": "BTC" }, "TEST-CHAN-2": { "signal_pattern": "You should buy (.*)", "signal_market": "USD" } } evaluator.eval_note = commons_constants.START_PENDING_EVAL_NOTE return evaluator
async def test_collect_invalid_date_range(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["ETH/BTC"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True, 1609459200, 1577836800) \ as collector: assert collector.start_timestamp is not None assert collector.end_timestamp is not None with pytest.raises(errors.DataCollectorError): await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert isinstance(collector.exchange, tentacles_exchanges.Binance) assert collector.file_path is not None assert collector.temp_file_path is not None assert not os.path.isfile(collector.file_path) assert not os.path.isfile(collector.temp_file_path)
async def test_stop_collect(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["AAVE/USDT"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True, 1549065660000, 1632090006000) as collector: async def stop_soon(): await asyncio.sleep(5) await collector.stop(should_stop_database=False) await asyncio.gather(collector.start(), stop_soon()) assert collector.time_frames != [] assert collector.symbols == symbols assert collector.exchange_name == BINANCE assert collector.tentacles_setup_config == tentacles_setup_config assert collector.finished assert collector.exchange_manager is None assert not os.path.isfile(collector.temp_file_path) assert not os.path.isfile(collector.file_path)
def initialize(self, strategy_evaluator_class, trading_mode_class=Mode.DailyTradingMode, config=None): self.logger = bot_logging.get_logger(self.__class__.__name__) self.config = test_config.load_test_config( ) if config is None else config # remove fees self.config[commons_constants.CONFIG_SIMULATOR][ commons_constants.CONFIG_SIMULATOR_FEES][ commons_constants.CONFIG_SIMULATOR_FEES_TAKER] = 0 self.config[commons_constants.CONFIG_SIMULATOR][ commons_constants.CONFIG_SIMULATOR_FEES][ commons_constants.CONFIG_SIMULATOR_FEES_MAKER] = 0 self.strategy_evaluator_class = strategy_evaluator_class self.trading_mode_class = trading_mode_class self.tentacles_setup_config = test_utils_config.load_test_tentacles_config( ) self._update_tentacles_config(strategy_evaluator_class, trading_mode_class)
def _create_evaluator_with_supported_channel_signals(): evaluator = Social.TelegramChannelSignalEvaluator(test_utils_config.load_test_tentacles_config()) evaluator.logger = logging.get_logger(evaluator.get_name()) evaluator.channels_config = { "TEST-CHAN-1": { "signal_pattern": { "MARKET_BUY": "Side: (BUY)", "MARKET_SELL": "Side: (SELL)" }, "signal_pair": "Pair: (.*)" }, "TEST-CHAN-2": { "signal_pattern": { "MARKET_BUY": ".* : (-1)$", "MARKET_SELL": ".* : (1)$" }, "signal_pair": "(.*):" } } evaluator.eval_note = commons_constants.START_PENDING_EVAL_NOTE return evaluator
async def _init_bot(): # import here to prevent web interface import issues import octobot.octobot as octobot import octobot.constants as octobot_constants import octobot.producers as producers import octobot_commons.tests as test_config import octobot_tentacles_manager.loaders as loaders import octobot_evaluators.api as evaluators_api import tests.test_utils.config as config octobot = octobot.OctoBot(test_config.load_test_config(dict_only=False)) octobot.initialized = True tentacles_config = config.load_test_tentacles_config() loaders.reload_tentacle_by_tentacle_class() octobot.task_manager.async_loop = asyncio.get_event_loop() octobot.task_manager.create_pool_executor() octobot.tentacles_setup_config = tentacles_config octobot.configuration_manager.add_element(octobot_constants.TENTACLES_SETUP_CONFIG_KEY, tentacles_config) octobot.exchange_producer = producers.ExchangeProducer(None, octobot, None, False) octobot.evaluator_producer = producers.EvaluatorProducer(None, octobot) octobot.evaluator_producer.matrix_id = await evaluators_api.initialize_evaluators(octobot.config, tentacles_config) # Do not edit config file octobot.community_auth.edited_config = None return octobot
async def test_collect_multi_pair(): tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["ETH/BTC", "BTC/USDT", "1INCH/BTC"] async with data_collector(BINANCE, tentacles_setup_config, symbols, None, True) as collector: assert collector.time_frames == [] assert collector.symbols == symbols assert collector.exchange_name == BINANCE assert collector.tentacles_setup_config == tentacles_setup_config await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert isinstance(collector.exchange, tentacles_exchanges.Binance) assert collector.file_path is not None assert collector.temp_file_path is not None assert not os.path.isfile(collector.temp_file_path) assert os.path.isfile(collector.file_path) async with collector_database(collector) as database: ohlcv = await database.select(enums.ExchangeDataTables.OHLCV) # use > to take into account new possible candles since collect max time is not specified assert len(ohlcv) > 19316 h_ohlcv = await database.select(enums.ExchangeDataTables.OHLCV, time_frame="4h") assert len(h_ohlcv) == len(symbols) * BINANCE_MAX_CANDLES_COUNT symbols_description = json.loads( (await database.select(enums.DataTables.DESCRIPTION))[0][3]) assert all(symbol in symbols_description for symbol in symbols) eth_btc_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="ETH/BTC") assert len(eth_btc_ohlcv) > 6760 inch_btc_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="1INCH/BTC") assert len(inch_btc_ohlcv) > 5803 btc_usdt_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="BTC/USDT") assert len(btc_usdt_ohlcv) > 6743
async def test_collect_valid_data(): exchange_name = "binance" tentacles_setup_config = test_utils_config.load_test_tentacles_config() symbols = ["ETH/BTC"] async with data_collector(exchange_name, tentacles_setup_config, symbols, None, True) as collector: assert collector.time_frames == [] assert collector.symbols == symbols assert collector.exchange_name == exchange_name assert collector.tentacles_setup_config == tentacles_setup_config await collector.start() assert collector.time_frames != [] assert collector.exchange_manager is None assert isinstance(collector.exchange, tentacles_exchanges.Binance) assert collector.file_path is not None async with collector_database(collector) as database: ohlcv = await database.select(enums.ExchangeDataTables.OHLCV) assert len(ohlcv) > 6000 h_ohlcv = await database.select(enums.ExchangeDataTables.OHLCV, time_frame="1h") assert len(h_ohlcv) == 500 eth_btc_ohlcv = await database.select( enums.ExchangeDataTables.OHLCV, symbol="ETH/BTC") assert len(eth_btc_ohlcv) == len(ohlcv)