async def _get_tools(): symbol = "BTC/USDT" config = load_test_config() config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO][ "SUB"] = 0.000000000000000000005 config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO][ "BNB"] = 0.000000000000000000005 config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]["USDT"] = 2000 exchange_manager = ExchangeManager(config, "binance") # use backtesting not to spam exchanges apis exchange_manager.is_simulated = True exchange_manager.is_backtesting = True # no backtesting file provided, except ValueError from initialize with pytest.raises(ValueError): await exchange_manager.initialize() trader = TraderSimulator(config, exchange_manager) await trader.initialize() mode = AbstractTradingMode(config, exchange_manager) consumer = AbstractTradingModeConsumer(mode) return exchange_manager, symbol, consumer
async def _get_tools(): symbol = "BTC/USDT" config = load_test_config() config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO][ "SUB"] = 0.000000000000000000005 config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO][ "BNB"] = 0.000000000000000000005 config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]["USDT"] = 2000 exchange_manager = ExchangeManager(config, "binance") # use backtesting not to spam exchanges apis exchange_manager.is_simulated = True exchange_manager.is_backtesting = True exchange_manager.backtesting = Backtesting(None, [exchange_manager.id], None, []) await exchange_manager.initialize() trader = TraderSimulator(config, exchange_manager) await trader.initialize() mode = AbstractTradingMode(config, exchange_manager) consumer = AbstractTradingModeConsumer(mode) return exchange_manager, symbol, consumer
async def test_get_risk(self): config, exchange_manager, trader_inst = await self.init_default() await self.stop(exchange_manager) config[CONFIG_TRADING][CONFIG_TRADER_RISK] = 0 trader_1 = TraderSimulator(config, exchange_manager) assert round(trader_1.risk, 2) == CONFIG_TRADER_RISK_MIN await self.stop(exchange_manager) config[CONFIG_TRADING][CONFIG_TRADER_RISK] = 2 trader_2 = TraderSimulator(config, exchange_manager) assert trader_2.risk == CONFIG_TRADER_RISK_MAX await self.stop(exchange_manager) config[CONFIG_TRADING][CONFIG_TRADER_RISK] = 0.5 trader_2 = TraderSimulator(config, exchange_manager) assert trader_2.risk == 0.5 await self.stop(exchange_manager)
async def init_default(): config = load_test_config() exchange_manager = ExchangeManager(config, TestTradeFactory.EXCHANGE_MANAGER_CLASS_STRING) await exchange_manager.initialize() trader = TraderSimulator(config, exchange_manager) await trader.initialize() return config, exchange_manager, trader
async def init_default(): config = load_test_config() config[CONFIG_BACKTESTING][CONFIG_ENABLED_OPTION] = True exchange_manager = ExchangeManager(config, ccxt.binance, is_simulated=True) await exchange_manager.initialize() exchange_inst = exchange_manager.get_exchange() exchange_simulator = exchange_inst.get_exchange() exchange_simulator.init_candles_offset([TimeFrames.ONE_HOUR, TimeFrames.FOUR_HOURS, TimeFrames.ONE_DAY], TestExchangeSimulator.DEFAULT_SYMBOL) trader_inst = TraderSimulator(config, exchange_inst, 1) return config, exchange_inst, exchange_simulator, trader_inst
async def init_default(simulated=True): config = load_test_config() exchange_manager = ExchangeManager(config, TestTrader.EXCHANGE_MANAGER_CLASS_STRING) exchange_manager.is_simulated = simulated await exchange_manager.initialize() trader = TraderSimulator(config, exchange_manager) await trader.initialize() # set afterwards backtesting attribute to force orders instant initialization exchange_manager.is_backtesting = True return config, exchange_manager, trader
async def create(self, trading_tentacles_path=CONFIG_TRADING_FILE_PATH): await self.exchange_manager.initialize() # set sandbox mode if not self.is_backtesting: self.exchange_manager.exchange.client.setSandboxMode( self.is_sandboxed) if not self.is_simulated: self.trader = Trader(self.config, self.exchange_manager) else: self.trader = TraderSimulator(self.config, self.exchange_manager) try: # check traders activation if not is_trader_enabled( self.config) and not is_trader_simulator_enabled( self.config): raise ValueError( f"No trader simulator nor real trader activated on {self.exchange_manager.exchange.name}" ) # initialize trader await self.trader.initialize() self.exchange_manager.trader = self.trader init_trading_mode_config(self.config, trading_tentacles_path) await create_trading_mode(self.config, self.exchange_manager) # add to global exchanges Exchanges.instance().add_exchange(self.exchange_manager) except Exception as e: self.logger.error( f"An error occurred when creating trader or initializing trading mode : " ) raise e
def is_simulated(self): self.exchange_manager.is_simulated = True self.exchange_manager.trader = TraderSimulator(self.config, self.exchange_manager) return self
async def backtesting_trader(backtesting_config, backtesting_exchange_manager): trader_instance = TraderSimulator(backtesting_config, backtesting_exchange_manager) return backtesting_config, backtesting_exchange_manager, trader_instance
async def simulated_trader(simulated_exchange_manager): config = load_test_config() trader_instance = TraderSimulator(config, simulated_exchange_manager) return config, simulated_exchange_manager, trader_instance
async def trader_simulator(exchange_manager): config = load_test_config() config[CONFIG_SIMULATOR][CONFIG_ENABLED_OPTION] = True trader_inst = TraderSimulator(load_test_config(), exchange_manager) await trader_inst.initialize() return config, exchange_manager, trader_inst