Esempio n. 1
0
    def test_ob_results_bigger_depth_price(self):
        exchange = ztom.ccxtExchangeWrapper.load_from_id("binance")
        exchange.set_offline_mode("test_data/markets.json",
                                  "test_data/tickers.csv")
        exchange.load_markets()

        order_books, working_triangle = self._prepare_bot_for_test_start_amount(
        )

        order_books_data_2 = {
            "asks": list([[0.0000200000, 100]]),
            "bids": list([[0.0000200000, 100]])
        }

        order_books["MANA/BTC"] = ztom.OrderBook("MANA/BTC",
                                                 order_books_data_2["asks"],
                                                 order_books_data_2["bids"])

        expected_result = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 5)

        self.assertEqual(0.034501, expected_result["result_amount"]
                         )  # equal to 2st ob quantity in ETH for 100 Mana
        self.assertEqual(1.2267693074999997, expected_result["result"])
        self.assertAlmostEqual(0.028, expected_result["filled_start_amount"],
                               3)

        order_books_data_2 = {
            "asks": list([[0.0000200000, 100], [0.0000300000, 100]]),
            "bids": list([[0.0000200000, 100]])
        }

        order_books["MANA/BTC"] = ztom.OrderBook("MANA/BTC",
                                                 order_books_data_2["asks"],
                                                 order_books_data_2["bids"])

        # set the start amount is greater than max start amount from the short order book (0.028)
        expected_result2 = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 0.029)

        self.assertLess(expected_result["result_amount"],
                        expected_result2["result_amount"])
Esempio n. 2
0
    def test_restrict_amount_to_bid_from_order_book_force_start_tri(self):
        order_books, working_triangle = self._prepare_bot_for_test_start_amount(
        )

        order_books_data_2 = {
            "asks": list([[0.0000200000, 10], [0.0000300000, 100]]),
            "bids": list([[0.0000200000, 100]])
        }

        order_books["MANA/BTC"] = ztom.OrderBook("MANA/BTC",
                                                 order_books_data_2["asks"],
                                                 order_books_data_2["bids"])

        self.tribot.balance = 1
        bal_to_bid = 5

        # should return None for everything because OB results are less than threshold
        self.assertEqual((None, None, None),
                         self.tribot.restrict_amount_to_bid_from_order_book(
                             bal_to_bid, working_triangle, order_books, False))

        expected_result, ob_result, bid_from_order_book = self.tribot.restrict_amount_to_bid_from_order_book(
            bal_to_bid, working_triangle, order_books, True)

        self.assertAlmostEqual(0.02, bid_from_order_book, 4)
Esempio n. 3
0
    def _prepare_bot_for_test_start_amount(self):
        import test_data.order_books_data as td  # getting sample order books

        self.tribot.load_config_from_file(self.default_config)
        self.tribot.init_exchange()
        self.tribot.exchange.offline = True

        working_triangle = dict()
        working_triangle["result"] = 1.006
        working_triangle["ob_result"] = 1.006

        working_triangle["symbol1"] = "ETH/BTC"
        working_triangle["symbol2"] = "MANA/BTC"
        working_triangle["symbol3"] = "MANA/ETH"

        working_triangle["leg1-order"] = "sell"
        working_triangle["leg2-order"] = "buy"
        working_triangle["leg3-order"] = "sell"

        order_books_data = td.get_order_books()

        order_books = dict()
        for key, ob in order_books_data.items():
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"],
                                                       ob["asks"], ob["bids"])

        return (order_books, working_triangle)
Esempio n. 4
0
File: bot.py Progetto: ztomsy/ztom
    def get_order_books_async(self, symbols: list):
        """
        returns the dict of {"symbol": OrderBook} in offline mode the order book is single line - ticker price and big
         amount
        :param symbols: list of symbols to get orderbooks
        :return: returns the dict of {"symbol": OrderBook}
        """
        i = 0
        ob_array = list()

        while len(ob_array) < len(symbols) and i < self.max_oder_books_fetch_attempts:
            i += 1
            try:
                ob_array = self.exchange.get_order_books_async(symbols)
            except Exception as e:
                self.log(self.LOG_ERROR, "Error while fetching order books exchange_id:{} session_uuid:{}"
                                         " fetch_num:{}:".
                         format(self.exchange_id, self.session_uuid, self.fetch_number))
                self.log(self.LOG_ERROR, "Exception: {}".format(type(e).__name__))
                self.log(self.LOG_ERROR, "Exception body:", e.args)

                self.log(self.LOG_ERROR, "Sleeping before next request")
                time.sleep(self.request_sleep)

        if len(ob_array) < len(symbols):
            raise Exception("Could not fetch all order books. Fetched: {}".format(len(ob_array)))

        order_books = dict()
        for ob in ob_array:
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"], ob["asks"], ob["bids"])

        return order_books
Esempio n. 5
0
    def test_order_books_save(self):
        self.tribot.load_config_from_file(self.default_config)

        order_books_data = dict()
        order_books_data["ETH/BTC"] = dict()
        order_books_data["MANA/BTC"] = dict()
        order_books_data["MANA/ETH"] = dict()

        order_books_data["ETH/BTC"]["symbol"] = "ETH/BTC"
        order_books_data["ETH/BTC"]["asks"] = list(
            [[0.0712040000, 4.9020000000], [0.0712270000, 1.6860000000],
             [0.0712340000, 1.4230000000], [0.0712350000, 21.5900000000],
             [0.0712430000, 9.6350000000], [0.0712480000, 0.0200000000]])

        order_books_data["ETH/BTC"]["bids"] = list(
            [[0.0711150000, 11.9230000000], [0.0711120000, 10.0000000000],
             [0.0711010000, 0.0840000000], [0.0710940000, 19.6610000000],
             [0.0710930000, 0.0180000000], [0.0710900000, 0.0010000000]])

        order_books_data["MANA/BTC"]["symbol"] = "MANA/BTC"
        order_books_data["MANA/BTC"]["asks"] = list(
            [[0.0000200000, 18603122.0000000000],
             [0.0000229900, 929.0000000000], [0.0000232300, 11343.0000000000],
             [0.0000234500, 9452.0000000000], [0.0000243700, 182.0000000000],
             [0.0000250000, 3440682.0000000000], [0.0000250200, 41.0000000000],
             [0.0000250900, 41.0000000000]])

        order_books_data["MANA/BTC"]["bids"] = list(
            [[0.0000199900, 12632.0000000000], [0.0000199500, 3955.0000000000],
             [0.0000199000, 1070.0000000000], [0.0000198900, 21643.0000000000],
             [0.0000198800, 100000.0000000000],
             [0.0000198300, 1000.0000000000], [0.0000198000, 2771.0000000000],
             [0.0000197500, 130.0000000000]])

        order_books_data["MANA/ETH"]["symbol"] = "MANA/ETH"
        order_books_data["MANA/ETH"]["asks"] = list(
            [[0.0003533100, 7495.0000000000], [0.0003533200, 11321.0000000000],
             [0.0003599900, 3148.0000000000], [0.0003600000, 818.0000000000],
             [0.0003638600, 100.0000000000], [0.0003680000, 7785.0000000000],
             [0.0003700000, 421.0000000000], [0.0003713400, 3400.0000000000],
             [0.0003745000, 1286.0000000000], [0.0003745100, 1132.0000000000],
             [0.0003849300, 4826.0000000000]])

        order_books_data["MANA/ETH"]["bids"] = list(
            [[0.0003450100, 3113.0000000000], [0.0003450000, 12948.0000000000],
             [0.0003443000, 2729.0000000000], [0.0003432800, 2380.0000000000],
             [0.0003421900, 200.0000000000], [0.0003420500, 2925.0000000000],
             [0.0003400000, 45.0000000000], [0.0003390000, 777.0000000000],
             [0.0003300000, 29963.0000000000], [0.0003287100, 181.0000000000],
             [0.0003283400, 5062.0000000000]])

        order_books = dict()
        for key, ob in order_books_data.items():
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"],
                                                       ob["asks"], ob["bids"])

        self.tribot.save_order_books(str(uuid.uuid4()), order_books)
Esempio n. 6
0
    def test_restrict_amount_to_bid_from_order_book_default_parameters_longer_ob(
            self):
        order_books, working_triangle = self._prepare_bot_for_test_start_amount(
        )

        order_books_data_2 = {
            "asks": list([[0.0000200000, 100], [0.0000300000, 100]]),
            "bids": list([[0.0000200000, 100]])
        }

        order_books["MANA/BTC"] = ztom.OrderBook("MANA/BTC",
                                                 order_books_data_2["asks"],
                                                 order_books_data_2["bids"])

        self.tribot.balance = 1
        bal_to_bid = 5

        expected_result, ob_result, bid_from_order_book = self.tribot.restrict_amount_to_bid_from_order_book(
            bal_to_bid, working_triangle, order_books, True)

        self.assertAlmostEqual(0.02, bid_from_order_book, 4)
Esempio n. 7
0
    def _prepare_bot_for_test_start_amount(self):
        import test_data.order_books_data as td  # getting sample order books

        working_triangle = dict()
        working_triangle["result"] = 1.006
        working_triangle["ob_result"] = 1.006

        working_triangle["symbol1"] = "ETH/BTC"
        working_triangle["symbol2"] = "MANA/BTC"
        working_triangle["symbol3"] = "MANA/ETH"

        working_triangle["leg1-order"] = "sell"
        working_triangle["leg2-order"] = "buy"
        working_triangle["leg3-order"] = "sell"

        order_books_data = td.get_order_books()

        order_books = dict()
        for key, ob in order_books_data.items():
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"],
                                                       ob["asks"], ob["bids"])

        return (order_books, working_triangle)
Esempio n. 8
0
    def test_ob_results(self):

        exchange = ztom.ccxtExchangeWrapper.load_from_id("binance")
        exchange.set_offline_mode("test_data/markets.json",
                                  "test_data/tickers.csv")
        exchange.load_markets()

        order_books_data = dict()
        order_books_data["ETH/BTC"] = dict()
        order_books_data["MANA/BTC"] = dict()
        order_books_data["MANA/ETH"] = dict()

        order_books_data["ETH/BTC"]["symbol"] = "ETH/BTC"
        order_books_data["ETH/BTC"]["asks"] = list(
            [[0.0712040000, 4.9020000000], [0.0712270000, 1.6860000000],
             [0.0712340000, 1.4230000000], [0.0712350000, 21.5900000000],
             [0.0712430000, 9.6350000000], [0.0712480000, 0.0200000000]])

        order_books_data["ETH/BTC"]["bids"] = list(
            [[0.0711150000, 11.9230000000], [0.0711120000, 10.0000000000],
             [0.0711010000, 0.0840000000], [0.0710940000, 19.6610000000],
             [0.0710930000, 0.0180000000], [0.0710900000, 0.0010000000]])

        order_books_data["MANA/BTC"]["symbol"] = "MANA/BTC"
        order_books_data["MANA/BTC"]["asks"] = list(
            [[0.0000200000, 18603122.0000000000],
             [0.0000229900, 929.0000000000], [0.0000232300, 11343.0000000000],
             [0.0000234500, 9452.0000000000], [0.0000243700, 182.0000000000],
             [0.0000250000, 3440682.0000000000], [0.0000250200, 41.0000000000],
             [0.0000250900, 41.0000000000]])

        order_books_data["MANA/BTC"]["bids"] = list(
            [[0.0000199900, 12632.0000000000], [0.0000199500, 3955.0000000000],
             [0.0000199000, 1070.0000000000], [0.0000198900, 21643.0000000000],
             [0.0000198800, 100000.0000000000],
             [0.0000198300, 1000.0000000000], [0.0000198000, 2771.0000000000],
             [0.0000197500, 130.0000000000]])

        order_books_data["MANA/ETH"]["symbol"] = "MANA/ETH"
        order_books_data["MANA/ETH"]["asks"] = list(
            [[0.0003533100, 7495.0000000000], [0.0003533200, 11321.0000000000],
             [0.0003599900, 3148.0000000000], [0.0003600000, 818.0000000000],
             [0.0003638600, 100.0000000000], [0.0003680000, 7785.0000000000],
             [0.0003700000, 421.0000000000], [0.0003713400, 3400.0000000000],
             [0.0003745000, 1286.0000000000], [0.0003745100, 1132.0000000000],
             [0.0003849300, 4826.0000000000]])

        order_books_data["MANA/ETH"]["bids"] = list(
            [[0.0003450100, 3113.0000000000], [0.0003450000, 12948.0000000000],
             [0.0003443000, 2729.0000000000], [0.0003432800, 2380.0000000000],
             [0.0003421900, 200.0000000000], [0.0003420500, 2925.0000000000],
             [0.0003400000, 45.0000000000], [0.0003390000, 777.0000000000],
             [0.0003300000, 29963.0000000000], [0.0003287100, 181.0000000000],
             [0.0003283400, 5062.0000000000]])

        order_books = dict()
        for key, ob in order_books_data.items():
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"],
                                                       ob["asks"], ob["bids"])

        working_triangle = dict()

        working_triangle["symbol1"] = "ETH/BTC"
        working_triangle["symbol2"] = "MANA/BTC"
        working_triangle["symbol3"] = "MANA/ETH"

        # starting from ETH
        working_triangle["leg1-order"] = "sell"
        working_triangle["leg2-order"] = "buy"
        working_triangle["leg3-order"] = "sell"

        expected_result = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 5.4)

        self.assertAlmostEqual(1.23, expected_result["result"], 2)

        # starting from BTC
        working_triangle["symbol1"] = "ETH/BTC"
        working_triangle["symbol2"] = "MANA/ETH"
        working_triangle["symbol3"] = "MANA/BTC"

        working_triangle["leg1-order"] = "buy"
        working_triangle["leg2-order"] = "buy"
        working_triangle["leg3-order"] = "sell"

        expected_result = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 0.1)

        self.assertAlmostEqual(0.79, expected_result["result"], 2)

        # start_amount is greater than amount in order book
        expected_result2 = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 10)

        self.assertEqual(expected_result2["result_amount"], 0.83153636)

        # 3rd orderbook is very short
        order_books_data["MANA/BTC"]["symbol"] = "MANA/BTC"
        order_books_data["MANA/BTC"]["asks"] = list([[0.0000200000, 1000]])

        order_books_data["MANA/BTC"]["bids"] = list([[0.0000200000, 1000]])

        order_books = dict()
        for key, ob in order_books_data.items():
            order_books[ob["symbol"]] = ztom.OrderBook(ob["symbol"],
                                                       ob["asks"], ob["bids"])

        expected_result = ta.order_book_results(
            exchange, working_triangle, {
                1: order_books[working_triangle["symbol1"]],
                2: order_books[working_triangle["symbol2"]],
                3: order_books[working_triangle["symbol3"]]
            }, 0.5)

        self.assertEqual(
            0.02, expected_result["result_amount"])  # equal to 3rd ob quantity
Esempio n. 9
0
    bot.exchange.init_async_exchange()

bot.load_markets()
if bot.offline:
    bot.fetch_tickers()  # for getting data for order books

symbol = zt.core.get_symbol(start_cur, dest_cur, bot.markets)

zt.ActionOrderManager.log = bot.log  # override order manager logger to the bot logger
zt.ActionOrderManager.LOG_INFO = bot.LOG_INFO
zt.ActionOrderManager.LOG_ERROR = bot.LOG_ERROR
zt.ActionOrderManager.LOG_DEBUG = bot.LOG_DEBUG
zt.ActionOrderManager.LOG_CRITICAL = bot.LOG_CRITICAL

ob_array = bot.exchange.fetch_order_book(symbol, 100)
ob = zt.OrderBook(symbol, ob_array["asks"], ob_array["bids"])

price = ob.get_depth_for_destination_currency(start_amount, dest_cur).total_price
order1 = zt.ActionOrder.create_from_start_amount(symbol, start_cur, start_amount, dest_cur, price)

bot.log(bot.LOG_INFO, "From {} -{}-> {}".format(order1.start_currency, order1.side, order1.dest_currency))
bot.log(bot.LOG_INFO, "Price: {}".format(price))


om = zt.ActionOrderManager(bot.exchange)
om.request_trades = False
om.add_order(order1)

while len(om.get_open_orders()) > 0:
    om.proceed_orders()