Exemple #1
0
    async def _create_not_loaded_order(self, new_order: Order,
                                       portfolio) -> Order:
        """
        Creates an exchange managed order
        """
        if not self.simulate and not new_order.is_self_managed():
            created_order = await self.exchange_manager.exchange.create_order(
                new_order.order_type, new_order.symbol,
                new_order.origin_quantity, new_order.origin_price,
                new_order.origin_stop_price)

            self.logger.info(
                f"Created order on {self.exchange_manager.exchange_name}: {created_order}"
            )

            # get real order from exchange
            new_order = Order(self)
            new_order.update_from_raw(created_order)

            # rebind linked portfolio to new order instance
            new_order.linked_portfolio = portfolio

        # update the availability of the currency in the portfolio
        portfolio.update_portfolio_available(new_order, is_new_order=True)

        return new_order
Exemple #2
0
    async def test_parse_order_type(self):
        _, exchange_manager, trader_inst = await self.init_default()

        order_to_test = Order(trader_inst)

        ccxt_order_buy_market = {
            "side": TradeOrderSide.BUY,
            "type": TradeOrderType.MARKET
        }

        order_to_test.update_from_raw(ccxt_order_buy_market)
        assert order_to_test.order_type == TraderOrderType.BUY_MARKET

        ccxt_order_buy_limit = {
            "side": TradeOrderSide.BUY,
            "type": TradeOrderType.LIMIT
        }
        assert parse_order_type(ccxt_order_buy_limit) == (
            TradeOrderSide.BUY, TraderOrderType.BUY_LIMIT)

        ccxt_order_sell_market = {
            "side": TradeOrderSide.SELL,
            "type": TradeOrderType.MARKET
        }
        assert parse_order_type(ccxt_order_sell_market) == (
            TradeOrderSide.SELL, TraderOrderType.SELL_MARKET)

        ccxt_order_sell_limit = {
            "side": TradeOrderSide.SELL,
            "type": TradeOrderType.LIMIT
        }
        assert parse_order_type(ccxt_order_sell_limit) == (
            TradeOrderSide.SELL, TraderOrderType.SELL_LIMIT)

        await self.stop(exchange_manager)
Exemple #3
0
async def test_update(trader):
    config, exchange_manager_inst, trader_inst = trader

    # with real trader
    order_inst = Order(trader_inst)
    order_inst.update(order_type=TraderOrderType.BUY_MARKET,
                      symbol="BTC/USDT",
                      current_price=10000,
                      quantity=1)

    assert order_inst.order_type == TraderOrderType.BUY_MARKET
    assert order_inst.symbol == "BTC/USDT"
    assert order_inst.created_last_price == 10000
    assert order_inst.origin_quantity == 1
    assert order_inst.creation_time != 0
    assert order_inst.get_currency_and_market() == ('BTC', 'USDT')
    assert order_inst.side is None
    assert order_inst.status == OrderStatus.OPEN
    assert order_inst.filled_quantity != order_inst.origin_quantity

    order_inst.update(order_type=TraderOrderType.STOP_LOSS_LIMIT,
                      symbol="ETH/BTC",
                      quantity=0.1,
                      quantity_filled=5.2,
                      price=0.12,
                      stop_price=0.9)
    assert order_inst.origin_stop_price == 0.9
    assert order_inst.origin_price == 0.12
Exemple #4
0
async def test_simulated_update(trader_simulator):
    config, exchange_manager_inst, trader_inst = trader_simulator
    order_sim_inst = Order(trader_inst)

    order_sim_inst.update(order_type=TraderOrderType.SELL_MARKET,
                          symbol="LTC/USDT",
                          quantity=100,
                          price=3.22)
    assert order_sim_inst.status == OrderStatus.OPEN
    assert order_sim_inst.filled_quantity == order_sim_inst.origin_quantity == 100
Exemple #5
0
 def _create_trade_from_raw(self, raw_trade):
     order = Order(self.trader)
     order.order_id = raw_trade[ExchangeConstantsOrderColumns.ID.value]
     order.origin_price = raw_trade[
         ExchangeConstantsOrderColumns.PRICE.value]
     order.origin_quantity = raw_trade[
         ExchangeConstantsOrderColumns.AMOUNT.value]
     order.symbol = raw_trade[ExchangeConstantsOrderColumns.SYMBOL.value]
     order.currency, order.market = self.exchange_manager.get_exchange_quote_and_base(
         raw_trade[ExchangeConstantsOrderColumns.SYMBOL.value])
     order.filled_quantity = raw_trade[
         ExchangeConstantsOrderColumns.AMOUNT.value]
     order.filled_quantity = None  # TODO
     order.filled_price = None  # TODO
     order.total_cost = None  # TODO
     order.order_type = None  # TODO
     order.fee = None  # TODO
     order.side = None  # TODO
     order.canceled_time = None  # TODO
     order.executed_time = None  # TODO
     return Trade(order)
Exemple #6
0
async def test_get_profitability(trader_simulator):
    config, exchange_manager_inst, trader_inst = trader_simulator

    # Test filled_price > create_last_price
    # test side SELL
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.SELL
    order_filled_sup_side_sell_inst.filled_price = 10
    order_filled_sup_side_sell_inst.created_last_price = 9
    assert order_filled_sup_side_sell_inst.get_profitability() == (-(1 - 10 / 9))

    # test side BUY
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.BUY
    order_filled_sup_side_sell_inst.filled_price = 15.114778
    order_filled_sup_side_sell_inst.created_last_price = 7.265
    assert order_filled_sup_side_sell_inst.get_profitability() == (1 - 15.114778 / 7.265)

    # Test filled_price < create_last_price
    # test side SELL
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.SELL
    order_filled_sup_side_sell_inst.filled_price = 11.556877
    order_filled_sup_side_sell_inst.created_last_price = 20
    assert order_filled_sup_side_sell_inst.get_profitability() == (1 - 20 / 11.556877)

    # test side BUY
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.BUY
    order_filled_sup_side_sell_inst.filled_price = 8
    order_filled_sup_side_sell_inst.created_last_price = 14.35
    assert order_filled_sup_side_sell_inst.get_profitability() == (-(1 - 14.35 / 8))

    # Test filled_price == create_last_price
    # test side SELL
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.SELL
    order_filled_sup_side_sell_inst.filled_price = 1517374.4567
    order_filled_sup_side_sell_inst.created_last_price = 1517374.4567
    assert order_filled_sup_side_sell_inst.get_profitability() == 0

    # test side BUY
    order_filled_sup_side_sell_inst = Order(trader_inst)
    order_filled_sup_side_sell_inst.side = TradeOrderSide.BUY
    order_filled_sup_side_sell_inst.filled_price = 0.4275587387858527
    order_filled_sup_side_sell_inst.created_last_price = 0.4275587387858527
    assert order_filled_sup_side_sell_inst.get_profitability() == 0
    async def test_parse_exchange_order_to_trade_instance(self):
        _, exchange_manager, trader_inst = await self.init_default()

        timestamp = time.time()
        order_to_test = Order(trader_inst)
        exchange_order = {
            "status": OrderStatus.PARTIALLY_FILLED.value,
            "symbol": self.DEFAULT_SYMBOL,
            # "fee": 0.001,
            "price": 10.1444215411,
            "cost": 100.1444215411,
            "filled": 1.568415145687741563132,
            "timestamp": timestamp
        }

        order_to_test.update_from_raw(exchange_order)

        assert order_to_test.status == OrderStatus.PARTIALLY_FILLED
        assert order_to_test.filled_quantity == 1.568415145687741563132
        assert order_to_test.filled_price == 10.1444215411
        # assert order_to_test.fee == 0.001
        assert order_to_test.total_cost == 100.1444215411

        await self.stop(exchange_manager)
Exemple #8
0
 def _create_order_from_raw(self, raw_order):
     order = Order(self.trader)
     order.update_from_raw(raw_order)
     return order
Exemple #9
0
 def update_close_orders(self):
     for symbol in self.exchange_manager.exchange_config.get_traded_pairs():
         for close_order in self.exchange_manager.get_closed_orders(symbol):
             self.parse_exchange_order_to_trade_instance(
                 close_order, Order(self))