def start(self): try: market = execution2_config_map.get("market").value.lower() raw_market_symbol = execution2_config_map.get( "market_symbol_pair").value try: assets: Tuple[str, str] = self._initialize_market_assets( market, [raw_market_symbol])[0] except ValueError as e: self._notify(str(e)) return market_names: List[Tuple[str, List[str]]] = [(market, [raw_market_symbol])] self._initialize_wallet(token_symbols=list(set(assets))) self._initialize_markets(market_names) self.assets = set(assets) maker_data = [self.markets[market], raw_market_symbol] + list(assets) self.market_symbol_pairs = [MarketSymbolPair(*maker_data)] strategy_logging_options = Execution2Strategy.OPTION_LOG_ALL self.strategy = Execution2Strategy( market_infos=[MarketSymbolPair(*maker_data)], logging_options=strategy_logging_options) except Exception as e: self._notify(str(e)) self.logger().error("Unknown error during initialization.", exc_info=True)
def setUp(self): self.clock: Clock = Clock(ClockMode.BACKTEST, 1.0, self.start_timestamp, self.end_timestamp) self.min_profitbality = 0.005 self.maker_market: BacktestMarket = BacktestMarket() self.taker_market: BacktestMarket = BacktestMarket() self.maker_data: MockOrderBookLoader = MockOrderBookLoader( *self.maker_symbols) self.taker_data: MockOrderBookLoader = MockOrderBookLoader( *self.taker_symbols) self.maker_data.set_balanced_order_book(1.0, 0.5, 1.5, 0.01, 10) self.taker_data.set_balanced_order_book(1.0, 0.5, 1.5, 0.001, 4) self.maker_market.add_data(self.maker_data) self.taker_market.add_data(self.taker_data) self.maker_market.set_balance("COINALPHA", 5) self.maker_market.set_balance("WETH", 5) self.maker_market.set_balance("QETH", 5) self.taker_market.set_balance("COINALPHA", 5) self.taker_market.set_balance("ETH", 5) self.maker_market.set_quantization_param( QuantizationParams(self.maker_symbols[0], 5, 5, 5, 5)) self.taker_market.set_quantization_param( QuantizationParams(self.taker_symbols[0], 5, 5, 5, 5)) self.market_pair: CrossExchangeMarketPair = CrossExchangeMarketPair( MarketSymbolPair(self.maker_market, *self.maker_symbols), MarketSymbolPair(self.taker_market, *self.taker_symbols), ) logging_options: int = ( CrossExchangeMarketMakingStrategy.OPTION_LOG_ALL & (~CrossExchangeMarketMakingStrategy.OPTION_LOG_NULL_ORDER_SIZE)) self.strategy: CrossExchangeMarketMakingStrategy = CrossExchangeMarketMakingStrategy( [self.market_pair], order_size_portfolio_ratio_limit=0.3, min_profitability=self.min_profitbality, logging_options=logging_options, ) self.logging_options = logging_options self.clock.add_iterator(self.maker_market) self.clock.add_iterator(self.taker_market) self.clock.add_iterator(self.strategy) self.maker_order_fill_logger: EventLogger = EventLogger() self.taker_order_fill_logger: EventLogger = EventLogger() self.cancel_order_logger: EventLogger = EventLogger() self.maker_market.add_listener(MarketEvent.OrderFilled, self.maker_order_fill_logger) self.taker_market.add_listener(MarketEvent.OrderFilled, self.taker_order_fill_logger) self.maker_market.add_listener(MarketEvent.OrderCancelled, self.cancel_order_logger)
def setUp(self): self.clock: Clock = Clock(ClockMode.BACKTEST, 1.0, self.start_timestamp, self.end_timestamp) self.market_1: BacktestMarket = BacktestMarket() self.market_2: BacktestMarket = BacktestMarket() self.market_1_data: MockOrderBookLoader = MockOrderBookLoader( *self.market_1_symbols) self.market_2_data: MockOrderBookLoader = MockOrderBookLoader( *self.market_2_symbols) self.market_1_data.set_balanced_order_book(1.0, 0.5, 1.5, 0.01, 10) self.market_2_data.set_balanced_order_book(1.0, 0.5, 1.5, 0.005, 5) self.market_1.add_data(self.market_1_data) self.market_2.add_data(self.market_2_data) self.market_1.set_balance("COINALPHA", 500) self.market_1.set_balance("WETH", 500) self.market_2.set_balance("COINALPHA", 500) self.market_2.set_balance("ETH", 500) self.market_1.set_quantization_param( QuantizationParams(self.market_1_symbols[0], 5, 5, 5, 5)) self.market_2.set_quantization_param( QuantizationParams(self.market_2_symbols[0], 5, 5, 5, 5)) self.market_symbol_pair_1 = MarketSymbolPair(*([self.market_1] + self.market_1_symbols)) self.market_symbol_pair_2 = MarketSymbolPair(*([self.market_2] + self.market_2_symbols)) self.market_pair: ArbitrageMarketPair = ArbitrageMarketPair( *(self.market_symbol_pair_1 + self.market_symbol_pair_2)) self.logging_options: int = ArbitrageStrategy.OPTION_LOG_ALL self.strategy: ArbitrageStrategy = ArbitrageStrategy( [self.market_pair], min_profitability=0.03, logging_options=self.logging_options) self.clock.add_iterator(self.market_1) self.clock.add_iterator(self.market_2) self.clock.add_iterator(self.strategy) self.market_1_order_fill_logger: EventLogger = EventLogger() self.market_2_order_fill_logger: EventLogger = EventLogger() self.market_1.add_listener(MarketEvent.OrderFilled, self.market_1_order_fill_logger) self.market_2.add_listener(MarketEvent.OrderFilled, self.market_2_order_fill_logger)
def test_with_adjust_orders_enabled(self): self.clock.remove_iterator(self.strategy) self.clock.remove_iterator(self.maker_market) self.maker_market: BacktestMarket = BacktestMarket() self.maker_data: MockOrderBookLoader = MockOrderBookLoader( *self.maker_symbols) self.maker_data.set_balanced_order_book(1.0, 0.5, 1.5, 0.1, 10) self.maker_market.add_data(self.maker_data) self.market_pair: CrossExchangeMarketPair = CrossExchangeMarketPair( MarketSymbolPair(self.maker_market, *self.maker_symbols), MarketSymbolPair(self.taker_market, *self.taker_symbols), ) self.strategy: CrossExchangeMarketMakingStrategy = CrossExchangeMarketMakingStrategy( [self.market_pair], order_size_portfolio_ratio_limit=0.3, min_profitability=0.005, logging_options=self.logging_options, ) self.maker_market.set_balance("COINALPHA", 5) self.maker_market.set_balance("WETH", 5) self.maker_market.set_balance("QETH", 5) self.maker_market.set_quantization_param( QuantizationParams(self.maker_symbols[0], 4, 4, 4, 4)) self.clock.add_iterator(self.strategy) self.clock.add_iterator(self.maker_market) self.clock.backtest_til(self.start_timestamp + 5) self.assertEqual(1, len(self.strategy.active_bids)) self.assertEqual(1, len(self.strategy.active_asks)) bid_order: LimitOrder = self.strategy.active_bids[0][1] ask_order: LimitOrder = self.strategy.active_asks[0][1] # place above top bid (at 0.95) self.assertAlmostEqual(Decimal("0.9501"), bid_order.price) # place below top ask (at 1.05) self.assertAlmostEqual(Decimal("1.049"), ask_order.price) self.assertAlmostEqual(Decimal("3"), round(bid_order.quantity, 4)) self.assertAlmostEqual(Decimal("3"), round(ask_order.quantity, 4))
def test_profitability_with_conversion(self): self.clock.remove_iterator(self.strategy) self.market_pair: CrossExchangeMarketPair = CrossExchangeMarketPair( MarketSymbolPair(self.maker_market, *["COINALPHA-QETH", "COINALPHA", "QETH"]), MarketSymbolPair(self.taker_market, *self.taker_symbols), 2) self.maker_data: MockOrderBookLoader = MockOrderBookLoader( "COINALPHA-QETH", "COINALPHA", "QETH") self.maker_data.set_balanced_order_book(1.05263, 0.55, 1.55, 0.01, 10) self.maker_market.add_data(self.maker_data) self.strategy: CrossExchangeMarketMakingStrategy = CrossExchangeMarketMakingStrategy( [self.market_pair], 0.01, order_size_portfolio_ratio_limit=0.3, logging_options=self.logging_options) self.clock.add_iterator(self.strategy) self.clock.backtest_til(self.start_timestamp + 5) self.assertEqual(0, len(self.strategy.active_bids)) self.assertEqual(0, len(self.strategy.active_asks)) self.assertEqual( (False, False), self.strategy.has_market_making_profit_potential(self.market_pair)) self.clock.remove_iterator(self.strategy) self.strategy: CrossExchangeMarketMakingStrategy = CrossExchangeMarketMakingStrategy( [self.market_pair], 0.003, order_size_portfolio_ratio_limit=0.3, logging_options=self.logging_options) self.clock.add_iterator(self.strategy) self.clock.backtest_til(self.start_timestamp + 10) self.assertEqual(1, len(self.strategy.active_bids)) self.assertEqual(1, len(self.strategy.active_asks)) self.assertEqual( (True, True), self.strategy.has_market_making_profit_potential(self.market_pair))
def start(self): try: order_size = pure_market_making_config_map.get("order_amount").value cancel_order_wait_time = pure_market_making_config_map.get("cancel_order_wait_time").value bid_place_threshold = pure_market_making_config_map.get("bid_place_threshold").value ask_place_threshold = pure_market_making_config_map.get("ask_place_threshold").value mode = pure_market_making_config_map.get("mode").value number_of_orders = pure_market_making_config_map.get("number_of_orders").value order_start_size = pure_market_making_config_map.get("order_start_size").value order_step_size = pure_market_making_config_map.get("order_step_size").value order_interval_percent = pure_market_making_config_map.get("order_interval_percent").value maker_market = pure_market_making_config_map.get("maker_market").value.lower() raw_maker_symbol = pure_market_making_config_map.get("maker_market_symbol").value inventory_skew_enabled = pure_market_making_config_map.get("inventory_skew_enabled").value inventory_target_base_percent = pure_market_making_config_map.get("inventory_target_base_percent").value filled_order_replenish_wait_time = pure_market_making_config_map.get("filled_order_replenish_wait_time").value enable_order_filled_stop_cancellation = pure_market_making_config_map.get( "enable_order_filled_stop_cancellation").value pricing_delegate = None sizing_delegate = None filter_delegate = PassThroughFilterDelegate() if mode == "multiple": pricing_delegate = ConstantMultipleSpreadPricingDelegate(bid_place_threshold, ask_place_threshold, order_interval_percent, number_of_orders) if inventory_skew_enabled: sizing_delegate = InventorySkewMultipleSizeSizingDelegate(order_start_size, order_step_size, number_of_orders, inventory_target_base_percent) else: sizing_delegate = StaggeredMultipleSizeSizingDelegate(order_start_size, order_step_size, number_of_orders) else: # mode == "single" pricing_delegate = ConstantSpreadPricingDelegate(bid_place_threshold, ask_place_threshold) if inventory_skew_enabled: sizing_delegate = InventorySkewSingleSizeSizingDelegate(order_size, inventory_target_base_percent) else: sizing_delegate = ConstantSizeSizingDelegate(order_size) try: maker_assets: Tuple[str, str] = self._initialize_market_assets(maker_market, [raw_maker_symbol])[0] except ValueError as e: self._notify(str(e)) return market_names: List[Tuple[str, List[str]]] = [(maker_market, [raw_maker_symbol])] self._initialize_wallet(token_symbols=list(set(maker_assets))) self._initialize_markets(market_names) self.assets = set(maker_assets) maker_data = [self.markets[maker_market], raw_maker_symbol] + list(maker_assets) self.market_symbol_pairs = [MarketSymbolPair(*maker_data)] strategy_logging_options = PureMarketMakingStrategyV2.OPTION_LOG_ALL self.strategy = PureMarketMakingStrategyV2(market_infos=[MarketSymbolPair(*maker_data)], pricing_delegate=pricing_delegate, filter_delegate=filter_delegate, sizing_delegate=sizing_delegate, filled_order_replenish_wait_time=filled_order_replenish_wait_time, enable_order_filled_stop_cancellation=enable_order_filled_stop_cancellation, cancel_order_wait_time=cancel_order_wait_time, logging_options=strategy_logging_options) except Exception as e: self._notify(str(e)) self.logger().error("Unknown error during initialization.", exc_info=True)
def setUp(self): self.clock: Clock = Clock(ClockMode.BACKTEST, 60.0, self.start_timestamp, self.end_timestamp) self.clock_tick_size = 60 self.maker_market: BacktestMarket = BacktestMarket() self.maker_data: MockOrderBookLoader = MockOrderBookLoader(*self.maker_symbols) self.mid_price = 100 self.bid_threshold = 0.01 self.ask_threshold = 0.01 self.cancel_order_wait_time = 45 self.maker_data.set_balanced_order_book(mid_price=self.mid_price, min_price=1, max_price=200, price_step_size=1, volume_step_size=10) self.filter_delegate = PassThroughFilterDelegate() self.constant_pricing_delegate = ConstantSpreadPricingDelegate(self.bid_threshold, self.ask_threshold) self.constant_sizing_delegate = ConstantSizeSizingDelegate(1.0) self.equal_strategy_sizing_delegate = StaggeredMultipleSizeSizingDelegate( order_start_size=1.0, order_step_size=0, number_of_orders=5 ) self.staggered_strategy_sizing_delegate = StaggeredMultipleSizeSizingDelegate( order_start_size=1.0, order_step_size=0.5, number_of_orders=5 ) self.multiple_order_strategy_pricing_delegate = ConstantMultipleSpreadPricingDelegate( bid_spread=self.bid_threshold, ask_spread=self.ask_threshold, order_interval_size=0.01, number_of_orders=5 ) self.maker_market.add_data(self.maker_data) self.maker_market.set_balance("COINALPHA", 500) self.maker_market.set_balance("WETH", 5000) self.maker_market.set_balance("QETH", 500) self.maker_market.set_quantization_param( QuantizationParams( self.maker_symbols[0], 6, 6, 6, 6 ) ) self.market_info: MarketSymbolPair = MarketSymbolPair( *( [self.maker_market] + self.maker_symbols ) ) logging_options: int = (PureMarketMakingStrategyV2.OPTION_LOG_ALL & (~PureMarketMakingStrategyV2.OPTION_LOG_NULL_ORDER_SIZE)) self.strategy: PureMarketMakingStrategyV2 = PureMarketMakingStrategyV2( [self.market_info], filter_delegate=self.filter_delegate, sizing_delegate=self.constant_sizing_delegate, pricing_delegate=self.constant_pricing_delegate, cancel_order_wait_time=45, logging_options=logging_options ) self.multi_order_equal_strategy: PureMarketMakingStrategyV2 = PureMarketMakingStrategyV2( [self.market_info], filter_delegate=self.filter_delegate, pricing_delegate=self.multiple_order_strategy_pricing_delegate, sizing_delegate=self.equal_strategy_sizing_delegate, cancel_order_wait_time=45, logging_options=logging_options ) self.multi_order_staggered_strategy: PureMarketMakingStrategyV2 = PureMarketMakingStrategyV2( [self.market_info], filter_delegate=self.filter_delegate, pricing_delegate=self.multiple_order_strategy_pricing_delegate, sizing_delegate=self.staggered_strategy_sizing_delegate, cancel_order_wait_time=45, logging_options=logging_options ) self.logging_options = logging_options self.clock.add_iterator(self.maker_market) self.clock.add_iterator(self.strategy) self.maker_order_fill_logger: EventLogger = EventLogger() self.cancel_order_logger: EventLogger = EventLogger() self.maker_market.add_listener(MarketEvent.OrderFilled, self.maker_order_fill_logger) self.maker_market.add_listener(MarketEvent.OrderCancelled, self.cancel_order_logger)
def start(self): try: order_size = pure_market_making_config_map.get("order_amount").value cancel_order_wait_time = pure_market_making_config_map.get( "cancel_order_wait_time").value bid_place_threshold = pure_market_making_config_map.get( "bid_place_threshold").value ask_place_threshold = pure_market_making_config_map.get( "ask_place_threshold").value mode = pure_market_making_config_map.get("mode").value number_of_orders = pure_market_making_config_map.get( "number_of_orders").value order_start_size = pure_market_making_config_map.get( "order_start_size").value order_step_size = pure_market_making_config_map.get( "order_step_size").value order_interval_percent = pure_market_making_config_map.get( "order_interval_percent").value maker_market = pure_market_making_config_map.get( "maker_market").value.lower() raw_maker_symbol = pure_market_making_config_map.get( "maker_market_symbol").value.upper() pricing_delegate = None sizing_delegate = None if mode == "multiple": pricing_delegate = ConstantMultipleSpreadPricingDelegate( bid_place_threshold, ask_place_threshold, order_interval_percent, number_of_orders) sizing_delegate = StaggeredMultipleSizeSizingDelegate( order_start_size, order_step_size, number_of_orders) try: maker_assets: Tuple[str, str] = self._initialize_market_assets( maker_market, [raw_maker_symbol])[0] except ValueError as e: self._notify(str(e)) return market_names: List[Tuple[str, List[str]]] = [(maker_market, [raw_maker_symbol])] self._initialize_wallet(token_symbols=list(set(maker_assets))) self._initialize_markets(market_names) self.assets = set(maker_assets) maker_data = [self.markets[maker_market], raw_maker_symbol ] + list(maker_assets) self.market_symbol_pairs = [MarketSymbolPair(*maker_data)] strategy_logging_options = PureMarketMakingStrategyV2.OPTION_LOG_ALL self.strategy = PureMarketMakingStrategyV2( market_infos=[MarketSymbolPair(*maker_data)], pricing_delegate=pricing_delegate, sizing_delegate=sizing_delegate, legacy_order_size=order_size, legacy_bid_spread=bid_place_threshold, legacy_ask_spread=ask_place_threshold, cancel_order_wait_time=cancel_order_wait_time, logging_options=strategy_logging_options) except Exception as e: self._notify(str(e)) self.logger().error("Unknown error during initialization.", exc_info=True)
def setUp(self): self.clock: Clock = Clock(ClockMode.BACKTEST, self.clock_tick_size, self.start_timestamp, self.end_timestamp) self.market: BacktestMarket = BacktestMarket() self.maker_data: MockOrderBookLoader = MockOrderBookLoader(*self.maker_symbols) self.mid_price = 100 self.time_delay = 15 self.cancel_order_wait_time = 45 self.maker_data.set_balanced_order_book(mid_price=self.mid_price, min_price=1, max_price=200, price_step_size=1, volume_step_size=10) self.market.add_data(self.maker_data) self.market.set_balance("COINALPHA", 500) self.market.set_balance("WETH", 5000) self.market.set_balance("QETH", 500) self.market.set_quantization_param( QuantizationParams( self.maker_symbols[0], 6, 6, 6, 6 ) ) self.market_info: MarketSymbolPair = MarketSymbolPair( *( [self.market] + self.maker_symbols ) ) logging_options: int = (SimpleTradeStrategy.OPTION_LOG_ALL & (~SimpleTradeStrategy.OPTION_LOG_NULL_ORDER_SIZE)) # Define strategies to test self.limit_buy_strategy: SimpleTradeStrategy = SimpleTradeStrategy( [self.market_info], order_type="limit", order_price=99, cancel_order_wait_time=self.cancel_order_wait_time, is_buy=True, time_delay=self.time_delay, order_amount=1.0, logging_options=logging_options ) self.limit_sell_strategy: SimpleTradeStrategy = SimpleTradeStrategy( [self.market_info], order_type="limit", order_price=101, cancel_order_wait_time=self.cancel_order_wait_time, is_buy=False, time_delay=self.time_delay, order_amount=1.0, logging_options=logging_options ) self.market_buy_strategy: SimpleTradeStrategy = SimpleTradeStrategy( [self.market_info], order_type="market", order_price=None, cancel_order_wait_time=self.cancel_order_wait_time, is_buy=True, time_delay=self.time_delay, order_amount=1.0, logging_options=logging_options ) self.market_sell_strategy: SimpleTradeStrategy = SimpleTradeStrategy( [self.market_info], order_type="market", order_price=None, cancel_order_wait_time=self.cancel_order_wait_time, is_buy=False, time_delay=self.time_delay, order_amount=1.0, logging_options=logging_options ) self.logging_options = logging_options self.clock.add_iterator(self.market) self.maker_order_fill_logger: EventLogger = EventLogger() self.cancel_order_logger: EventLogger = EventLogger() self.buy_order_completed_logger: EventLogger = EventLogger() self.sell_order_completed_logger: EventLogger = EventLogger() self.market.add_listener(MarketEvent.BuyOrderCompleted, self.buy_order_completed_logger) self.market.add_listener(MarketEvent.SellOrderCompleted, self.sell_order_completed_logger) self.market.add_listener(MarketEvent.OrderFilled, self.maker_order_fill_logger) self.market.add_listener(MarketEvent.OrderCancelled, self.cancel_order_logger)