Exemplo n.º 1
0
    async def fetch_binance_trading_pairs() -> List[str]:
        from hummingbot.market.binance.binance_market import BinanceMarket

        async with aiohttp.ClientSession() as client:
            async with client.get(BINANCE_ENDPOINT,
                                  timeout=API_CALL_TIMEOUT) as response:
                if response.status == 200:
                    try:
                        data = await response.json()
                        trading_pair_structs = data.get("symbols")
                        raw_trading_pairs = list(
                            map(lambda details: details.get("symbol"),
                                trading_pair_structs))
                        # Binance API has an error where they have a symbol called 123456
                        # The symbol endpoint is
                        # https://api.binance.com/api/v1/exchangeInfo
                        if "123456" in raw_trading_pairs:
                            raw_trading_pairs.remove("123456")
                        return [
                            BinanceMarket.convert_from_exchange_trading_pair(p)
                            for p in raw_trading_pairs
                        ]
                    except Exception:
                        pass
                        # Do nothing if the request fails -- there will be no autocomplete for binance trading pairs
                return []
Exemplo n.º 2
0
    def connect_market(exchange, *api_details):
        market = None
        if exchange == "binance":
            market = BinanceMarket(api_details[0], api_details[1])
        elif exchange == "bittrex":
            market = BittrexMarket(api_details[0], api_details[1])
        elif exchange == "coinbase_pro":
            market = CoinbaseProMarket(api_details[0], api_details[1],
                                       api_details[2])
        elif exchange == "huobi":
            market = HuobiMarket(api_details[0], api_details[1])
        elif exchange == "kucoin":
            market = KucoinMarket(api_details[0], api_details[2],
                                  api_details[1])
        elif exchange == "liquid":
            market = LiquidMarket(api_details[0], api_details[1])
        elif exchange == "kraken":
            market = KrakenMarket(api_details[0], api_details[1])
        elif exchange == "beaxy":
            market = BeaxyMarket(api_details[0], api_details[1])
        elif exchange == "eterbase":
            market = EterbaseMarket(api_details[0], api_details[1],
                                    api_details[2])

        return market
Exemplo n.º 3
0
    async def fetch_binance_trading_pairs(self) -> List[str]:
        try:
            from hummingbot.market.binance.binance_market import BinanceMarket
            client: aiohttp.ClientSession = self.http_client()
            async with client.get(BINANCE_ENDPOINT,
                                  timeout=API_CALL_TIMEOUT) as response:
                if response.status == 200:
                    data = await response.json()
                    trading_pair_structs = data.get("symbols")
                    raw_trading_pairs = list(
                        map(lambda details: details.get("symbol"),
                            trading_pair_structs))
                    # Binance API has an error where they have a symbol called 123456
                    # The symbol endpoint is
                    # https://api.binance.com/api/v1/exchangeInfo
                    if "123456" in raw_trading_pairs:
                        raw_trading_pairs.remove("123456")
                    trading_pair_list: List[str] = []
                    for raw_trading_pair in raw_trading_pairs:
                        converted_trading_pair: Optional[str] = \
                            BinanceMarket.convert_from_exchange_trading_pair(raw_trading_pair)
                        if converted_trading_pair is not None:
                            trading_pair_list.append(converted_trading_pair)
                        else:
                            self.logger().debug(
                                f"Could not parse the trading pair {raw_trading_pair}, skipping it..."
                            )
                    return trading_pair_list

        except Exception:
            # Do nothing if the request fails -- there will be no autocomplete for binance trading pairs
            pass

        return []
Exemplo n.º 4
0
    async def fetch_binance_trading_pairs(self) -> List[str]:
        try:
            from hummingbot.market.binance.binance_market import BinanceMarket
            client: aiohttp.ClientSession = self.http_client()
            async with client.get(BINANCE_ENDPOINT,
                                  timeout=API_CALL_TIMEOUT) as response:
                if response.status == 200:
                    data = await response.json()
                    raw_trading_pairs = [
                        d["symbol"] for d in data["symbols"]
                        if d["status"] == "TRADING"
                    ]
                    trading_pair_list: List[str] = []
                    for raw_trading_pair in raw_trading_pairs:
                        converted_trading_pair: Optional[str] = \
                            BinanceMarket.convert_from_exchange_trading_pair(raw_trading_pair)
                        if converted_trading_pair is not None:
                            trading_pair_list.append(converted_trading_pair)
                        else:
                            self.logger().debug(
                                f"Could not parse the trading pair {raw_trading_pair}, skipping it..."
                            )
                    return trading_pair_list

        except Exception:
            # Do nothing if the request fails -- there will be no autocomplete for binance trading pairs
            pass

        return []
Exemplo n.º 5
0
    def _initialize_markets(self, market_names: List[Tuple[str, List[str]]]):
        ethereum_rpc_url = global_config_map.get("ethereum_rpc_url").value
        for market_name, symbols in market_names:
            if market_name == "ddex" and self.wallet:
                market = DDEXMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "binance":
                binance_api_key = global_config_map.get(
                    "binance_api_key").value
                binance_api_secret = global_config_map.get(
                    "binance_api_secret").value
                market = BinanceMarket(
                    ethereum_rpc_url=ethereum_rpc_url,
                    binance_api_key=binance_api_key,
                    binance_api_secret=binance_api_secret,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "radar_relay" and self.wallet:
                market = RadarRelayMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "bamboo_relay" and self.wallet:
                market = BambooRelayMarket(wallet=self.wallet,
                                           web3_url=ethereum_rpc_url,
                                           symbols=symbols)

            elif market_name == "coinbase_pro":
                coinbase_pro_api_key = global_config_map.get(
                    "coinbase_pro_api_key").value
                coinbase_pro_secret_key = global_config_map.get(
                    "coinbase_pro_secret_key").value
                coinbase_pro_passphrase = global_config_map.get(
                    "coinbase_pro_passphrase").value

                market = CoinbaseProMarket(
                    ethereum_rpc_url=ethereum_rpc_url,
                    coinbase_pro_api_key=coinbase_pro_api_key,
                    coinbase_pro_secret_key=coinbase_pro_secret_key,
                    coinbase_pro_passphrase=coinbase_pro_passphrase,
                    symbols=symbols,
                    trading_required=self._trading_required)

            else:
                raise ValueError(f"Market name {market_name} is invalid.")

            self.markets[market_name]: MarketBase = market
Exemplo n.º 6
0
    def setUpClass(cls):
        global MAINNET_RPC_URL

        cls.ev_loop = asyncio.get_event_loop()

        if API_MOCK_ENABLED:
            cls.web_app = HummingWebApp.get_instance()
            cls.web_app.add_host_to_mock(cls.base_api_url, ["/api/v1/ping", "/api/v1/time", "/api/v1/ticker/24hr"])
            cls.web_app.start()
            cls.ev_loop.run_until_complete(cls.web_app.wait_til_started())
            cls._patcher = mock.patch("aiohttp.client.URL")
            cls._url_mock = cls._patcher.start()
            cls._url_mock.side_effect = cls.web_app.reroute_local

            cls._req_patcher = unittest.mock.patch.object(requests.Session, "request", autospec=True)
            cls._req_url_mock = cls._req_patcher.start()
            cls._req_url_mock.side_effect = HummingWebApp.reroute_request
            cls.web_app.update_response("get", cls.base_api_url, "/api/v3/account", FixtureBinance.GET_ACCOUNT)
            cls.web_app.update_response("get", cls.base_api_url, "/api/v1/exchangeInfo",
                                        FixtureBinance.EXCHANGE_INFO)
            cls.web_app.update_response("get", cls.base_api_url, "/wapi/v3/tradeFee.html",
                                        FixtureBinance.GET_TRADE_FEES)
            cls.web_app.update_response("post", cls.base_api_url, "/api/v1/userDataStream",
                                        FixtureBinance.GET_LISTEN_KEY)
            cls.web_app.update_response("put", cls.base_api_url, "/api/v1/userDataStream",
                                        FixtureBinance.GET_LISTEN_KEY)
            cls.web_app.update_response("get", cls.base_api_url, "/api/v1/depth",
                                        FixtureBinance.LINKETH_SNAP, params={'symbol': 'LINKETH'})
            cls.web_app.update_response("get", cls.base_api_url, "/api/v1/depth",
                                        FixtureBinance.ZRXETH_SNAP, params={'symbol': 'ZRXETH'})
            ws_base_url = "wss://stream.binance.com:9443/ws"
            cls._ws_user_url = f"{ws_base_url}/{FixtureBinance.GET_LISTEN_KEY['listenKey']}"
            HummingWsServerFactory.start_new_server(cls._ws_user_url)
            HummingWsServerFactory.start_new_server(f"{ws_base_url}/linketh@depth/zrxeth@depth")
            cls._ws_patcher = unittest.mock.patch("websockets.connect", autospec=True)
            cls._ws_mock = cls._ws_patcher.start()
            cls._ws_mock.side_effect = HummingWsServerFactory.reroute_ws_connect

            cls._t_nonce_patcher = unittest.mock.patch("hummingbot.market.binance.binance_market.get_tracking_nonce")
            cls._t_nonce_mock = cls._t_nonce_patcher.start()
        cls.clock: Clock = Clock(ClockMode.REALTIME)
        cls.market: BinanceMarket = BinanceMarket(
            API_KEY, API_SECRET,
            order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
            user_stream_tracker_data_source_type=UserStreamTrackerDataSourceType.EXCHANGE_API,
            trading_pairs=["LINKETH", "ZRXETH"]
        )
        print("Initializing Binance market... this will take about a minute.")
        cls.ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
        cls.clock.add_iterator(cls.market)
        cls.stack: contextlib.ExitStack = contextlib.ExitStack()
        cls._clock = cls.stack.enter_context(cls.clock)
        cls.ev_loop.run_until_complete(cls.wait_til_ready())
        print("Ready.")
Exemplo n.º 7
0
 async def fetch_prices(self):
     client = await self._http_client()
     async with client.request("GET", self.price_feed_url) as resp:
         records = await resp.json()
         for record in records:
             trading_pair = BinanceMarket.convert_from_exchange_trading_pair(
                 record["symbol"])
             if record["bidPrice"] is not None and record[
                     "askPrice"] is not None:
                 self._price_dict[trading_pair] =\
                     (Decimal(record["bidPrice"]) + Decimal(record["askPrice"])) / Decimal("2")
     self._ready_event.set()
def binance_mid_price(trading_pair: str) -> Optional[Decimal]:
    resp = requests.get(url=BINANCE_PRICE_URL)
    records = resp.json()
    result = None
    for record in records:
        pair = BinanceMarket.convert_from_exchange_trading_pair(
            record["symbol"])
        if trading_pair == pair and record["bidPrice"] is not None and record[
                "askPrice"] is not None:
            result = (Decimal(record["bidPrice"]) +
                      Decimal(record["askPrice"])) / Decimal("2")
            break
    return result
    def setUpClass(cls):
        global MAINNET_RPC_URL

        cls.clock: Clock = Clock(ClockMode.REALTIME)
        cls.market: BinanceMarket = BinanceMarket(
            conf.binance_api_key, conf.binance_api_secret,
            order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
            user_stream_tracker_data_source_type=UserStreamTrackerDataSourceType.EXCHANGE_API,
            symbols=["ZRXETH", "IOSTETH"]
        )
        print("Initializing Binance market... this will take about a minute.")
        cls.ev_loop: asyncio.BaseEventLoop = asyncio.get_event_loop()
        cls.clock.add_iterator(cls.market)
        cls.stack: contextlib.ExitStack = contextlib.ExitStack()
        cls._clock = cls.stack.enter_context(cls.clock)
        cls.ev_loop.run_until_complete(cls.wait_til_ready())
        print("Ready.")
Exemplo n.º 10
0
    async def fetch_binance_trading_pairs() -> List[str]:
        from hummingbot.market.binance.binance_market import BinanceMarket

        async with aiohttp.ClientSession() as client:
            async with client.get(BINANCE_ENDPOINT,
                                  timeout=API_CALL_TIMEOUT) as response:
                if response.status == 200:
                    try:
                        data = await response.json()
                        trading_pair_structs = data.get("symbols")
                        raw_trading_pairs = list(
                            map(lambda details: details.get("symbol"),
                                trading_pair_structs))
                        return [
                            BinanceMarket.convert_from_exchange_trading_pair(p)
                            for p in raw_trading_pairs
                        ]
                    except Exception:
                        pass
                        # Do nothing if the request fails -- there will be no autocomplete for binance trading pairs
                return []
Exemplo n.º 11
0
    def _initialize_markets(self, market_names: List[Tuple[str, List[str]]]):
        ethereum_rpc_url = global_config_map.get("ethereum_rpc_url").value

        # aggregate symbols if there are duplicate markets
        market_symbols_map = {}
        for market_name, symbols in market_names:
            if market_name not in market_symbols_map:
                market_symbols_map[market_name] = []
            market_symbols_map[market_name] += symbols

        for market_name, symbols in market_symbols_map.items():
            if global_config_map.get("paper_trade_enabled").value:
                self._notify(f"\nPaper trade is enabled for market {market_name}")
                try:
                    market = create_paper_trade_market(market_name, symbols)
                except Exception:
                    raise
                paper_trade_account_balance = global_config_map.get("paper_trade_account_balance").value
                for asset, balance in paper_trade_account_balance:
                    market.set_balance(asset, balance)

            elif market_name == "ddex" and self.wallet:
                market = DDEXMarket(wallet=self.wallet,
                                    ethereum_rpc_url=ethereum_rpc_url,
                                    order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
                                    symbols=symbols,
                                    trading_required=self._trading_required)

            elif market_name == "idex" and self.wallet:
                idex_api_key: str = global_config_map.get("idex_api_key").value
                try:
                    market = IDEXMarket(idex_api_key=idex_api_key,
                                        wallet=self.wallet,
                                        ethereum_rpc_url=ethereum_rpc_url,
                                        order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
                                        symbols=symbols,
                                        trading_required=self._trading_required)
                except Exception as e:
                    self.logger().error(str(e))

            elif market_name == "binance":
                binance_api_key = global_config_map.get("binance_api_key").value
                binance_api_secret = global_config_map.get("binance_api_secret").value
                market = BinanceMarket(binance_api_key,
                                       binance_api_secret,
                                       order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
                                       symbols=symbols,
                                       trading_required=self._trading_required)

            elif market_name == "radar_relay" and self.wallet:
                market = RadarRelayMarket(wallet=self.wallet,
                                          ethereum_rpc_url=ethereum_rpc_url,
                                          symbols=symbols,
                                          trading_required=self._trading_required)

            elif market_name == "bamboo_relay" and self.wallet:
                use_coordinator = global_config_map.get("bamboo_relay_use_coordinator").value
                pre_emptive_soft_cancels = global_config_map.get("bamboo_relay_pre_emptive_soft_cancels").value
                market = BambooRelayMarket(wallet=self.wallet,
                                           ethereum_rpc_url=ethereum_rpc_url,
                                           symbols=symbols,
                                           use_coordinator=use_coordinator,
                                           pre_emptive_soft_cancels=pre_emptive_soft_cancels,
                                           trading_required=self._trading_required)

            elif market_name == "coinbase_pro":
                coinbase_pro_api_key = global_config_map.get("coinbase_pro_api_key").value
                coinbase_pro_secret_key = global_config_map.get("coinbase_pro_secret_key").value
                coinbase_pro_passphrase = global_config_map.get("coinbase_pro_passphrase").value

                market = CoinbaseProMarket(coinbase_pro_api_key,
                                           coinbase_pro_secret_key,
                                           coinbase_pro_passphrase,
                                           symbols=symbols,
                                           trading_required=self._trading_required)
            elif market_name == "huobi":
                huobi_api_key = global_config_map.get("huobi_api_key").value
                huobi_secret_key = global_config_map.get("huobi_secret_key").value
                market = HuobiMarket(huobi_api_key,
                                     huobi_secret_key,
                                     order_book_tracker_data_source_type=OrderBookTrackerDataSourceType.EXCHANGE_API,
                                     symbols=symbols,
                                     trading_required=self._trading_required)
            else:
                raise ValueError(f"Market name {market_name} is invalid.")

            self.markets[market_name]: MarketBase = market

        self.markets_recorder = MarketsRecorder(
            self.trade_fill_db,
            list(self.markets.values()),
            in_memory_config_map.get("strategy_file_path").value,
            in_memory_config_map.get("strategy").value
        )
        self.markets_recorder.start()
Exemplo n.º 12
0
    def _initialize_markets(self, market_names: List[Tuple[str, List[str]]]):
        ethereum_rpc_url = global_config_map.get("ethereum_rpc_url").value

        # aggregate trading_pairs if there are duplicate markets
        market_trading_pairs_map = {}
        for market_name, trading_pairs in market_names:
            if market_name not in market_trading_pairs_map:
                market_trading_pairs_map[market_name] = []
            market_class: MarketBase = MARKET_CLASSES.get(
                market_name, MarketBase)
            for trading_pair in trading_pairs:
                exchange_trading_pair: str = market_class.convert_to_exchange_trading_pair(
                    trading_pair)
                market_trading_pairs_map[market_name].append(
                    exchange_trading_pair)

        for market_name, trading_pairs in market_trading_pairs_map.items():
            if global_config_map.get("paper_trade_enabled").value:
                try:
                    market = create_paper_trade_market(market_name,
                                                       trading_pairs)
                except Exception:
                    raise
                paper_trade_account_balance = global_config_map.get(
                    "paper_trade_account_balance").value
                for asset, balance in paper_trade_account_balance.items():
                    market.set_balance(asset, balance)

            elif market_name == "binance":
                binance_api_key = global_config_map.get(
                    "binance_api_key").value
                binance_api_secret = global_config_map.get(
                    "binance_api_secret").value
                market = BinanceMarket(
                    binance_api_key,
                    binance_api_secret,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required,
                )

            elif market_name == "radar_relay":
                assert self.wallet is not None
                market = RadarRelayMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required,
                )

            elif market_name == "bamboo_relay":
                assert self.wallet is not None
                use_coordinator = global_config_map.get(
                    "bamboo_relay_use_coordinator").value
                pre_emptive_soft_cancels = global_config_map.get(
                    "bamboo_relay_pre_emptive_soft_cancels").value
                market = BambooRelayMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    trading_pairs=trading_pairs,
                    use_coordinator=use_coordinator,
                    pre_emptive_soft_cancels=pre_emptive_soft_cancels,
                    trading_required=self._trading_required,
                )

            elif market_name == "coinbase_pro":
                coinbase_pro_api_key = global_config_map.get(
                    "coinbase_pro_api_key").value
                coinbase_pro_secret_key = global_config_map.get(
                    "coinbase_pro_secret_key").value
                coinbase_pro_passphrase = global_config_map.get(
                    "coinbase_pro_passphrase").value

                market = CoinbaseProMarket(
                    coinbase_pro_api_key,
                    coinbase_pro_secret_key,
                    coinbase_pro_passphrase,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            elif market_name == "huobi":
                huobi_api_key = global_config_map.get("huobi_api_key").value
                huobi_secret_key = global_config_map.get(
                    "huobi_secret_key").value
                market = HuobiMarket(
                    huobi_api_key,
                    huobi_secret_key,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            elif market_name == "liquid":
                liquid_api_key = global_config_map.get("liquid_api_key").value
                liquid_secret_key = global_config_map.get(
                    "liquid_secret_key").value

                market = LiquidMarket(
                    liquid_api_key,
                    liquid_secret_key,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    user_stream_tracker_data_source_type=
                    UserStreamTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            elif market_name == "dolomite":
                assert self.wallet is not None
                is_test_net: bool = global_config_map.get(
                    "ethereum_chain_name").value == "DOLOMITE_TEST"
                market = DolomiteMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    isTestNet=is_test_net,
                    trading_required=self._trading_required,
                )
            elif market_name == "bittrex":
                bittrex_api_key = global_config_map.get(
                    "bittrex_api_key").value
                bittrex_secret_key = global_config_map.get(
                    "bittrex_secret_key").value
                market = BittrexMarket(
                    bittrex_api_key,
                    bittrex_secret_key,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            elif market_name == "kucoin":
                kucoin_api_key = global_config_map.get("kucoin_api_key").value
                kucoin_secret_key = global_config_map.get(
                    "kucoin_secret_key").value
                kucoin_passphrase = global_config_map.get(
                    "kucoin_passphrase").value
                market = KucoinMarket(
                    kucoin_api_key,
                    kucoin_passphrase,
                    kucoin_secret_key,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            elif market_name == "eterbase":
                eterbase_api_key = global_config_map.get(
                    "eterbase_api_key").value
                eterbase_secret_key = global_config_map.get(
                    "eterbase_secret_key").value
                eterbase_account = global_config_map.get(
                    "eterbase_account").value
                market = EterbaseMarket(
                    eterbase_api_key,
                    eterbase_secret_key,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required,
                    eterbase_account=eterbase_account)
            elif market_name == "kraken":
                kraken_api_key = global_config_map.get("kraken_api_key").value
                kraken_secret_key = global_config_map.get(
                    "kraken_secret_key").value
                market = KrakenMarket(
                    kraken_api_key,
                    kraken_secret_key,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    trading_pairs=trading_pairs,
                    trading_required=self._trading_required)
            else:
                raise ValueError(f"Market name {market_name} is invalid.")

            self.markets[market_name]: MarketBase = market

        self.markets_recorder = MarketsRecorder(
            self.trade_fill_db,
            list(self.markets.values()),
            self.strategy_file_name,
            self.strategy_name,
        )
        self.markets_recorder.start()
Exemplo n.º 13
0
    def test_orders_saving_and_restoration(self):
        config_path: str = "test_config"
        strategy_name: str = "test_strategy"
        sql: SQLConnectionManager = SQLConnectionManager(
            SQLConnectionType.TRADE_FILLS, db_path=self.db_path)
        order_id: Optional[str] = None
        recorder: MarketsRecorder = MarketsRecorder(sql, [self.market],
                                                    config_path, strategy_name)
        recorder.start()

        try:
            self.assertEqual(0, len(self.market.tracking_states))

            # Try to put limit buy order for 0.02 ETH worth of ZRX, and watch for order creation event.
            current_bid_price: Decimal = self.market.get_price("ZRXETH", True)
            bid_price: Decimal = current_bid_price * Decimal("0.8")
            quantize_bid_price: Decimal = self.market.quantize_order_price(
                "ZRXETH", bid_price)

            amount: Decimal = Decimal("0.02") / bid_price
            quantized_amount: Decimal = self.market.quantize_order_amount(
                "ZRXETH", amount)

            order_id = self.market.buy("ZRXETH", quantized_amount,
                                       OrderType.LIMIT, quantize_bid_price)
            [order_created_event] = self.run_parallel(
                self.market_logger.wait_for(BuyOrderCreatedEvent))
            order_created_event: BuyOrderCreatedEvent = order_created_event
            self.assertEqual(order_id, order_created_event.order_id)

            # Verify tracking states
            self.assertEqual(1, len(self.market.tracking_states))
            self.assertEqual(order_id,
                             list(self.market.tracking_states.keys())[0])

            # Verify orders from recorder
            recorded_orders: List[
                Order] = recorder.get_orders_for_config_and_market(
                    config_path, self.market)
            self.assertEqual(1, len(recorded_orders))
            self.assertEqual(order_id, recorded_orders[0].id)

            # Verify saved market states
            saved_market_states: MarketState = recorder.get_market_states(
                config_path, self.market)
            self.assertIsNotNone(saved_market_states)
            self.assertIsInstance(saved_market_states.saved_state, dict)
            self.assertGreater(len(saved_market_states.saved_state), 0)

            # Close out the current market and start another market.
            self.clock.remove_iterator(self.market)
            for event_tag in self.events:
                self.market.remove_listener(event_tag, self.market_logger)
            self.market: BinanceMarket = BinanceMarket(
                binance_api_key=conf.binance_api_key,
                binance_api_secret=conf.binance_api_secret,
                order_book_tracker_data_source_type=
                OrderBookTrackerDataSourceType.EXCHANGE_API,
                user_stream_tracker_data_source_type=
                UserStreamTrackerDataSourceType.EXCHANGE_API,
                trading_pairs=["ZRXETH", "IOSTETH"])
            for event_tag in self.events:
                self.market.add_listener(event_tag, self.market_logger)
            recorder.stop()
            recorder = MarketsRecorder(sql, [self.market], config_path,
                                       strategy_name)
            recorder.start()
            saved_market_states = recorder.get_market_states(
                config_path, self.market)
            self.clock.add_iterator(self.market)
            self.assertEqual(0, len(self.market.limit_orders))
            self.assertEqual(0, len(self.market.tracking_states))
            self.market.restore_tracking_states(
                saved_market_states.saved_state)
            self.assertEqual(1, len(self.market.limit_orders))
            self.assertEqual(1, len(self.market.tracking_states))

            # Cancel the order and verify that the change is saved.
            self.market.cancel("ZRXETH", order_id)
            self.run_parallel(self.market_logger.wait_for(OrderCancelledEvent))
            order_id = None
            self.assertEqual(0, len(self.market.limit_orders))
            self.assertEqual(0, len(self.market.tracking_states))
            saved_market_states = recorder.get_market_states(
                config_path, self.market)
            self.assertEqual(0, len(saved_market_states.saved_state))
        finally:
            if order_id is not None:
                self.market.cancel("ZRXETH", order_id)
                self.run_parallel(
                    self.market_logger.wait_for(OrderCancelledEvent))

            recorder.stop()
            os.unlink(self.db_path)
Exemplo n.º 14
0
    def test_orders_saving_and_restoration(self):
        config_path: str = "test_config"
        strategy_name: str = "test_strategy"
        sql: SQLConnectionManager = SQLConnectionManager(
            SQLConnectionType.TRADE_FILLS, db_path=self.db_path)
        order_id: Optional[str] = None
        recorder: MarketsRecorder = MarketsRecorder(sql, [self.market],
                                                    config_path, strategy_name)
        recorder.start()

        try:
            self.assertEqual(0, len(self.market.tracking_states))

            # Try to put limit buy order for 0.02 ETH worth of ZRX, and watch for order creation event.
            current_bid_price: Decimal = self.market.get_price("LINKETH", True)
            bid_price: Decimal = current_bid_price * Decimal("0.8")
            quantize_bid_price: Decimal = self.market.quantize_order_price(
                "LINKETH", bid_price)

            amount: Decimal = 1
            quantized_amount: Decimal = self.market.quantize_order_amount(
                "LINKETH", amount)

            if API_MOCK_ENABLED:
                resp = self.order_response(FixtureBinance.ORDER_BUY_NOT_FILLED,
                                           1000001, "buy", "LINKETH")
                self.web_app.update_response("post", self.base_api_url,
                                             "/api/v3/order", resp)
            order_id = self.market.buy("LINKETH", quantized_amount,
                                       OrderType.LIMIT, quantize_bid_price)
            [order_created_event] = self.run_parallel(
                self.market_logger.wait_for(BuyOrderCreatedEvent))
            order_created_event: BuyOrderCreatedEvent = order_created_event
            self.assertEqual(order_id, order_created_event.order_id)

            # Verify tracking states
            self.assertEqual(1, len(self.market.tracking_states))
            self.assertEqual(order_id,
                             list(self.market.tracking_states.keys())[0])

            # Verify orders from recorder
            recorded_orders: List[
                Order] = recorder.get_orders_for_config_and_market(
                    config_path, self.market)
            self.assertEqual(1, len(recorded_orders))
            self.assertEqual(order_id, recorded_orders[0].id)

            # Verify saved market states
            saved_market_states: MarketState = recorder.get_market_states(
                config_path, self.market)
            self.assertIsNotNone(saved_market_states)
            self.assertIsInstance(saved_market_states.saved_state, dict)
            self.assertGreater(len(saved_market_states.saved_state), 0)

            # Close out the current market and start another market.
            self.clock.remove_iterator(self.market)
            for event_tag in self.events:
                self.market.remove_listener(event_tag, self.market_logger)
            self.market: BinanceMarket = BinanceMarket(
                binance_api_key=API_KEY,
                binance_api_secret=API_SECRET,
                order_book_tracker_data_source_type=
                OrderBookTrackerDataSourceType.EXCHANGE_API,
                user_stream_tracker_data_source_type=
                UserStreamTrackerDataSourceType.EXCHANGE_API,
                trading_pairs=["LINKETH", "ZRXETH"])
            for event_tag in self.events:
                self.market.add_listener(event_tag, self.market_logger)
            recorder.stop()
            recorder = MarketsRecorder(sql, [self.market], config_path,
                                       strategy_name)
            recorder.start()
            saved_market_states = recorder.get_market_states(
                config_path, self.market)
            self.clock.add_iterator(self.market)
            self.assertEqual(0, len(self.market.limit_orders))
            self.assertEqual(0, len(self.market.tracking_states))
            self.market.restore_tracking_states(
                saved_market_states.saved_state)
            self.assertEqual(1, len(self.market.limit_orders))
            self.assertEqual(1, len(self.market.tracking_states))

            # Cancel the order and verify that the change is saved.
            if API_MOCK_ENABLED:
                resp = self.fixture(FixtureBinance.CANCEL_ORDER,
                                    origClientOrderId=order_id,
                                    side="BUY")
                self.web_app.update_response(
                    "delete",
                    self.base_api_url,
                    "/api/v3/order",
                    resp,
                    params={'origClientOrderId': order_id})
            self.market.cancel("LINKETH", order_id)
            self.run_parallel(self.market_logger.wait_for(OrderCancelledEvent))
            order_id = None
            self.assertEqual(0, len(self.market.limit_orders))
            self.assertEqual(0, len(self.market.tracking_states))
            saved_market_states = recorder.get_market_states(
                config_path, self.market)
            self.assertEqual(0, len(saved_market_states.saved_state))
        finally:
            if order_id is not None:
                self.market.cancel("LINKETH", order_id)
                self.run_parallel(
                    self.market_logger.wait_for(OrderCancelledEvent))

            recorder.stop()
            os.unlink(self.db_path)
Exemplo n.º 15
0
    def _initialize_markets(self, market_names: List[Tuple[str, List[str]]]):
        ethereum_rpc_url = global_config_map.get("ethereum_rpc_url").value

        # aggregate symbols if there are duplicate markets
        market_symbols_map = {}
        for market_name, symbols in market_names:
            if market_name not in market_symbols_map:
                market_symbols_map[market_name] = []
            market_symbols_map[market_name] += symbols

        for market_name, symbols in market_symbols_map.items():
            if market_name == "ddex" and self.wallet:
                market = DDEXMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "idex" and self.wallet:
                try:
                    market = IDEXMarket(
                        wallet=self.wallet,
                        ethereum_rpc_url=ethereum_rpc_url,
                        order_book_tracker_data_source_type=
                        OrderBookTrackerDataSourceType.EXCHANGE_API,
                        symbols=symbols,
                        trading_required=self._trading_required)
                except Exception as e:
                    self.logger().error(str(e))

            elif market_name == "binance":
                binance_api_key = global_config_map.get(
                    "binance_api_key").value
                binance_api_secret = global_config_map.get(
                    "binance_api_secret").value
                market = BinanceMarket(
                    binance_api_key,
                    binance_api_secret,
                    order_book_tracker_data_source_type=
                    OrderBookTrackerDataSourceType.EXCHANGE_API,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "radar_relay" and self.wallet:
                market = RadarRelayMarket(
                    wallet=self.wallet,
                    ethereum_rpc_url=ethereum_rpc_url,
                    symbols=symbols,
                    trading_required=self._trading_required)

            elif market_name == "bamboo_relay" and self.wallet:
                market = BambooRelayMarket(wallet=self.wallet,
                                           ethereum_rpc_url=ethereum_rpc_url,
                                           symbols=symbols)

            elif market_name == "coinbase_pro":
                coinbase_pro_api_key = global_config_map.get(
                    "coinbase_pro_api_key").value
                coinbase_pro_secret_key = global_config_map.get(
                    "coinbase_pro_secret_key").value
                coinbase_pro_passphrase = global_config_map.get(
                    "coinbase_pro_passphrase").value

                market = CoinbaseProMarket(
                    coinbase_pro_api_key,
                    coinbase_pro_secret_key,
                    coinbase_pro_passphrase,
                    symbols=symbols,
                    trading_required=self._trading_required)

            else:
                raise ValueError(f"Market name {market_name} is invalid.")

            self.markets[market_name]: MarketBase = market

        self.markets_recorder = MarketsRecorder(
            self.trade_fill_db, list(self.markets.values()),
            in_memory_config_map.get("strategy_file_path").value,
            in_memory_config_map.get("strategy").value)
        self.markets_recorder.start()