コード例 #1
0
    def test_update_portfolio(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT],
                      "BTC/USD",
                      70,
                      10,
                      70)

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(limit_buy, True)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300

        last_prices = []
        for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK):
            last_prices.insert(i, {})
            last_prices[i]["price"] = random.uniform(69, 71)

        limit_buy.last_prices = last_prices
        limit_buy.update_order_status()

        portfolio_inst.update_portfolio(limit_buy)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 20
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 20
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 300

        # Test buy order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET],
                        "BTC/USD",
                        80,
                        8,
                        80)

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(market_sell, True)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 300

        last_prices = [{
            "price": 80
        }]

        market_sell.last_prices = last_prices
        market_sell.update_order_status()

        # when filling market sell
        portfolio_inst.update_portfolio(market_sell)
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 12
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 940
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 940

        self.stop(trader_inst)
コード例 #2
0
    def test_update_portfolio_with_filled_orders(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET],
            "BTC/USD", 70, 3, 70)
        last_prices = [{"price": 70}]

        market_sell.last_prices = last_prices
        market_sell.update_order_status()

        # Test sell order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT],
            "BTC/USD", 100, 4.2, 100)

        # Test stop loss order
        stop_loss = StopLossOrder(trader_inst)
        stop_loss.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS],
            "BTC/USD", 80, 4.2, 80)

        limit_sell.add_linked_order(stop_loss)
        stop_loss.add_linked_order(limit_sell)

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT],
            "BTC/USD", 50, 2, 50)

        last_prices = []
        for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK):
            last_prices.insert(i, {})
            last_prices[i]["price"] = random.uniform(49, 51)

        limit_buy.last_prices = last_prices
        limit_buy.update_order_status()

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(market_sell, True)
        portfolio_inst.update_portfolio_available(limit_sell, True)
        portfolio_inst.update_portfolio_available(stop_loss, True)
        portfolio_inst.update_portfolio_available(limit_buy, True)

        assert portfolio_inst.get_currency_portfolio(
            "BTC", Portfolio.AVAILABLE) == 2.8
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 900

        # when cancelling limit sell, market sell and stop orders
        portfolio_inst.update_portfolio_available(stop_loss, False)
        portfolio_inst.update_portfolio_available(limit_sell, False)

        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 7
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 900

        # when filling limit buy
        portfolio_inst.update_portfolio(limit_buy)

        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 9
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 900
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.TOTAL) == 12
        assert portfolio_inst.get_currency_portfolio("USD",
                                                     Portfolio.TOTAL) == 900

        # when filling market sell
        portfolio_inst.update_portfolio(market_sell)

        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 9
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 1110
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.TOTAL) == 9
        assert portfolio_inst.get_currency_portfolio("USD",
                                                     Portfolio.TOTAL) == 1110

        self.stop(trader_inst)
コード例 #3
0
    def test_update_portfolio(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT],
            "BTC/USD", 70, 10, 70)

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(limit_buy, True)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 10
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 300

        last_prices = []
        for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK):
            last_prices.insert(i, {})
            last_prices[i]["price"] = random.uniform(69, 71)

        limit_buy.last_prices = last_prices
        limit_buy.update_order_status()

        portfolio_inst.update_portfolio(limit_buy)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 20
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 300
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.TOTAL) == 20
        assert portfolio_inst.get_currency_portfolio("USD",
                                                     Portfolio.TOTAL) == 300

        # Test buy order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.new(
            OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET],
            "BTC/USD", 80, 8, 80)

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(market_sell, True)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 12
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 300

        last_prices = [{"price": 80}]

        market_sell.last_prices = last_prices
        market_sell.update_order_status()

        # when filling market sell
        portfolio_inst.update_portfolio(market_sell)
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.AVAILABLE) == 12
        assert portfolio_inst.get_currency_portfolio(
            "USD", Portfolio.AVAILABLE) == 940
        assert portfolio_inst.get_currency_portfolio("BTC",
                                                     Portfolio.TOTAL) == 12
        assert portfolio_inst.get_currency_portfolio("USD",
                                                     Portfolio.TOTAL) == 940

        self.stop(trader_inst)
コード例 #4
0
    def test_update_portfolio_with_filled_orders(self):
        config, portfolio_inst, _, trader_inst = self.init_default()

        # Test buy order
        market_sell = SellMarketOrder(trader_inst)
        market_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_MARKET],
                        "BTC/USD",
                        70,
                        3,
                        70)
        last_prices = [{
            "price": 70
        }]

        market_sell.last_prices = last_prices
        market_sell.update_order_status()

        # Test sell order
        limit_sell = SellLimitOrder(trader_inst)
        limit_sell.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.SELL_LIMIT],
                       "BTC/USD",
                       100,
                       4.2,
                       100)

        # Test stop loss order
        stop_loss = StopLossOrder(trader_inst)
        stop_loss.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.STOP_LOSS],
                      "BTC/USD",
                      80,
                      4.2,
                      80)

        limit_sell.add_linked_order(stop_loss)
        stop_loss.add_linked_order(limit_sell)

        # Test buy order
        limit_buy = BuyLimitOrder(trader_inst)
        limit_buy.new(OrderConstants.TraderOrderTypeClasses[TraderOrderType.BUY_LIMIT],
                      "BTC/USD",
                      50,
                      2,
                      50)

        last_prices = []
        for i in range(0, SIMULATOR_LAST_PRICES_TO_CHECK):
            last_prices.insert(i, {})
            last_prices[i]["price"] = random.uniform(49, 51)

        limit_buy.last_prices = last_prices
        limit_buy.update_order_status()

        # update portfolio with creations
        portfolio_inst.update_portfolio_available(market_sell, True)
        portfolio_inst.update_portfolio_available(limit_sell, True)
        portfolio_inst.update_portfolio_available(stop_loss, True)
        portfolio_inst.update_portfolio_available(limit_buy, True)

        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 2.8
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 900

        # when cancelling limit sell, market sell and stop orders
        portfolio_inst.update_portfolio_available(stop_loss, False)
        portfolio_inst.update_portfolio_available(limit_sell, False)

        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 7
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 900

        # when filling limit buy
        portfolio_inst.update_portfolio(limit_buy)

        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 900
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 12
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 900

        # when filling market sell
        portfolio_inst.update_portfolio(market_sell)

        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 9
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.AVAILABLE) == 1110
        assert portfolio_inst.get_currency_portfolio("BTC", Portfolio.TOTAL) == 9
        assert portfolio_inst.get_currency_portfolio("USD", Portfolio.TOTAL) == 1110

        self.stop(trader_inst)