Esempio n. 1
0
    def test_slippage(self):
        follower = XueQiuFollower()
        mock_user = mock.MagicMock()

        test_price = 1.0
        follower.slippage = 0.05

        # test buy
        test_trade_cmd = {
            "strategy": "test_strategy",
            "strategy_name": "test_strategy",
            "action": "buy",
            "stock_code": "162411",
            "amount": 100,
            "price": 1.0,
            "datetime": datetime.datetime.now(),
        }
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        excepted_price = test_price * (1 + follower.slippage)
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], excepted_price)

        # test sell
        test_trade_cmd["action"] = "sell"
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        excepted_price = test_price * (1 - follower.slippage)
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], excepted_price)
Esempio n. 2
0
    def test_slippage(self):
        follower = XueQiuFollower()
        mock_user = mock.MagicMock()

        test_price = 1.0
        follower.slippage = 0.05

        # test buy
        test_trade_cmd = {
            "strategy": "test_strategy",
            "strategy_name": "test_strategy",
            "action": "buy",
            "stock_code": "162411",
            "amount": 100,
            "price": 1.0,
            "datetime": datetime.datetime.now(),
        }
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        excepted_price = test_price * (1 + follower.slippage)
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], excepted_price)

        # test sell
        test_trade_cmd["action"] = "sell"
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        excepted_price = test_price * (1 - follower.slippage)
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], excepted_price)
Esempio n. 3
0
    def test_slippage_with_default(self):
        follower = XueQiuFollower()
        mock_user = mock.MagicMock()

        # test default no slippage
        test_price = 1.0
        test_trade_cmd = {
            "strategy": "test_strategy",
            "strategy_name": "test_strategy",
            "action": "buy",
            "stock_code": "162411",
            "amount": 100,
            "price": 1.0,
            "datetime": datetime.datetime.now(),
        }
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], test_price)
Esempio n. 4
0
    def test_slippage_with_default(self):
        follower = XueQiuFollower()
        mock_user = mock.MagicMock()

        # test default no slippage
        test_price = 1.0
        test_trade_cmd = {
            "strategy": "test_strategy",
            "strategy_name": "test_strategy",
            "action": "buy",
            "stock_code": "162411",
            "amount": 100,
            "price": 1.0,
            "datetime": datetime.datetime.now(),
        }
        follower._execute_trade_cmd(
            trade_cmd=test_trade_cmd,
            users=[mock_user],
            expire_seconds=10,
            entrust_prop="limit",
            send_interval=10,
        )
        _, kwargs = getattr(mock_user, test_trade_cmd["action"]).call_args
        self.assertAlmostEqual(kwargs["price"], test_price)