Esempio n. 1
0
    async def test_close_filled_sell_limit_order_one_in_two(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(
            portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager

        # Test buy order
        limit_buy = create_order_instance(trader=trader_inst,
                                          order_type=TraderOrderType.BUY_LIMIT,
                                          symbol="BQX/BTC",
                                          current_price=4,
                                          quantity=2,
                                          price=4)

        await trader_inst.create_order(limit_buy, portfolio_manager.portfolio)

        # Test second buy order
        second_limit_buy = create_order_instance(
            trader=trader_inst,
            order_type=TraderOrderType.BUY_LIMIT,
            symbol="VEN/BTC",
            current_price=1,
            quantity=1.5,
            price=1)

        await trader_inst.create_order(second_limit_buy,
                                       portfolio_manager.portfolio)

        assert not trades_manager.trades

        with pytest.raises(KeyError):
            assert portfolio_manager.portfolio.portfolio["BQX"][
                PORTFOLIO_AVAILABLE] == 0
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_AVAILABLE] == 0.5

        # Fill only 1st one
        limit_buy.filled_price = 4
        limit_buy.status = OrderStatus.FILLED
        await trader_inst.close_filled_order(limit_buy)

        # added filled orders as filled trades
        assert len(trades_manager.trades) == 1
        assert all(trade.status is OrderStatus.FILLED
                   for trade in trades_manager.trades.values())

        assert limit_buy not in orders_manager.get_open_orders()
        assert second_limit_buy in orders_manager.get_open_orders()

        assert initial_portfolio != portfolio_manager.portfolio
        assert portfolio_manager.portfolio.portfolio["BQX"][
            PORTFOLIO_AVAILABLE] == 2
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_AVAILABLE] == 0.5
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_TOTAL] == 2

        await self.stop(exchange_manager)
Esempio n. 2
0
    async def test_notify_sell_limit_order_fill(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(
            portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager

        # Test buy order
        limit_buy = create_order_instance(trader=trader_inst,
                                          order_type=TraderOrderType.BUY_LIMIT,
                                          symbol="BQX/BTC",
                                          current_price=0.1,
                                          quantity=10,
                                          price=0.1)

        await trader_inst.create_order(limit_buy, portfolio_manager.portfolio)

        limit_buy.filled_price = limit_buy.origin_price
        limit_buy.filled_quantity = limit_buy.origin_quantity

        await trader_inst.notify_order_close(limit_buy)

        assert limit_buy not in orders_manager.get_open_orders()

        assert initial_portfolio != portfolio_manager.portfolio
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_AVAILABLE] == 9
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_TOTAL] == 9
        assert portfolio_manager.portfolio.portfolio["BQX"][
            PORTFOLIO_AVAILABLE] == 10
        assert portfolio_manager.portfolio.portfolio["BQX"][
            PORTFOLIO_TOTAL] == 10

        await self.stop(exchange_manager)
Esempio n. 3
0
    async def test_close_filled_stop_buy_order(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager

        # Test buy order
        stop_order = create_order_instance(trader=trader_inst,
                                           order_type=TraderOrderType.BUY_LIMIT,
                                           symbol="BQX/BTC",
                                           current_price=4,
                                           quantity=2,
                                           price=4,
                                           side=TradeOrderSide.BUY)

        await trader_inst.create_order(stop_order, portfolio_manager.portfolio)

        assert not trades_manager.trades

        await stop_order.on_fill(force_fill=True)

        assert stop_order not in orders_manager.get_open_orders()

        assert not initial_portfolio == portfolio_manager.portfolio.portfolio

        # added filled orders as filled trades
        assert len(trades_manager.trades) == 1
        assert all(trade.status is stop_order.status for trade in trades_manager.trades.values())

        await self.stop(exchange_manager)
Esempio n. 4
0
 async def _sell_everything(self, symbol, inverted, timeout=None):
     created_orders = []
     order_type = TraderOrderType.BUY_MARKET if inverted else TraderOrderType.SELL_MARKET
     async with self.exchange_manager.exchange_personal_data.portfolio_manager.portfolio.lock:
         current_symbol_holding, current_market_quantity, _, price, symbol_market = \
             await get_pre_order_data(self.exchange_manager, symbol, timeout=timeout)
         if inverted:
             if price > 0:
                 quantity = current_market_quantity / price
             else:
                 quantity = 0
         else:
             quantity = current_symbol_holding
         for order_quantity, order_price in check_and_adapt_order_details_if_necessary(
                 quantity, price, symbol_market):
             current_order = create_order_instance(
                 trader=self,
                 order_type=order_type,
                 symbol=symbol,
                 current_price=order_price,
                 quantity=order_quantity,
                 price=order_price)
             created_orders.append(await self.create_order(
                 current_order, self.exchange_manager.
                 exchange_personal_data.portfolio_manager.portfolio))
     return created_orders
Esempio n. 5
0
    async def test_notify_sell_limit_order_cancel_one_in_two(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(
            portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager

        # Test buy order
        limit_buy = create_order_instance(trader=trader_inst,
                                          order_type=TraderOrderType.BUY_LIMIT,
                                          symbol="BQX/BTC",
                                          current_price=4,
                                          quantity=2,
                                          price=4)

        await trader_inst.create_order(limit_buy, portfolio_manager.portfolio)

        # Test second buy order
        second_limit_buy = create_order_instance(
            trader=trader_inst,
            order_type=TraderOrderType.BUY_LIMIT,
            symbol="VEN/BTC",
            current_price=1,
            quantity=1.5,
            price=1)

        await trader_inst.create_order(second_limit_buy,
                                       portfolio_manager.portfolio)

        # Cancel only 1st one
        await trader_inst.notify_order_close(limit_buy, True)

        assert limit_buy not in orders_manager.get_open_orders()
        assert second_limit_buy in orders_manager.get_open_orders()

        assert initial_portfolio != portfolio_manager.portfolio
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_AVAILABLE] == 8.5
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_TOTAL] == 10

        await self.stop(exchange_manager)
Esempio n. 6
0
 async def create_artificial_order(self, order_type, symbol, current_price,
                                   quantity, price, linked_portfolio):
     """
     Creates an OctoBot managed order
     """
     await self.create_order(
         create_order_instance(trader=self,
                               order_type=order_type,
                               symbol=symbol,
                               current_price=current_price,
                               quantity=quantity,
                               price=price,
                               linked_portfolio=linked_portfolio))
Esempio n. 7
0
 async def create_artificial_order(self, order_type, symbol, current_price,
                                   quantity, price, linked_portfolio):
     """
     Creates an OctoBot managed order (managed orders example: stop loss that is not published on the exchange and
     that is maintained internally).
     """
     await self.create_order(
         create_order_instance(trader=self,
                               order_type=order_type,
                               symbol=symbol,
                               current_price=current_price,
                               quantity=quantity,
                               price=price,
                               linked_portfolio=linked_portfolio))
Esempio n. 8
0
    async def test_close_filled_sell_limit_order(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(
            portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager
        trades_manager = exchange_manager.exchange_personal_data.trades_manager

        # Test buy order
        limit_buy = create_order_instance(trader=trader_inst,
                                          order_type=TraderOrderType.BUY_LIMIT,
                                          symbol="BQX/BTC",
                                          current_price=0.1,
                                          quantity=10,
                                          price=0.1)

        await trader_inst.create_order(limit_buy, portfolio_manager.portfolio)

        limit_buy.filled_price = limit_buy.origin_price
        limit_buy.filled_quantity = limit_buy.origin_quantity
        limit_buy.status = OrderStatus.FILLED

        assert not trades_manager.trades

        await trader_inst.close_filled_order(limit_buy)

        assert limit_buy not in orders_manager.get_open_orders()

        # added filled orders as filled trades
        assert len(trades_manager.trades) == 1
        assert all(trade.status is OrderStatus.FILLED
                   for trade in trades_manager.trades.values())

        assert initial_portfolio != portfolio_manager.portfolio
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_AVAILABLE] == 9
        assert portfolio_manager.portfolio.portfolio["BTC"][
            PORTFOLIO_TOTAL] == 9
        assert portfolio_manager.portfolio.portfolio["BQX"][
            PORTFOLIO_AVAILABLE] == 10
        assert portfolio_manager.portfolio.portfolio["BQX"][
            PORTFOLIO_TOTAL] == 10

        await self.stop(exchange_manager)
Esempio n. 9
0
    async def test_notify_sell_limit_order_cancel(self):
        _, exchange_manager, trader_inst = await self.init_default()
        portfolio_manager = exchange_manager.exchange_personal_data.portfolio_manager
        initial_portfolio = copy.deepcopy(
            portfolio_manager.portfolio.portfolio)
        orders_manager = exchange_manager.exchange_personal_data.orders_manager

        # Test buy order
        limit_buy = create_order_instance(trader=trader_inst,
                                          order_type=TraderOrderType.BUY_LIMIT,
                                          symbol="BQX/BTC",
                                          current_price=4,
                                          quantity=2,
                                          price=4)

        await trader_inst.create_order(limit_buy, portfolio_manager.portfolio)

        await trader_inst.notify_order_close(limit_buy, True)

        assert limit_buy not in orders_manager.get_open_orders()

        assert initial_portfolio == portfolio_manager.portfolio.portfolio

        await self.stop(exchange_manager)