Beispiel #1
0
    def test_adjust_sell_amount_without_enable(self):
        follower = XueQiuFollower()

        mock_user = mock.MagicMock()
        follower._users = [mock_user]

        follower._adjust_sell = False
        amount = follower._adjust_sell_amount("169101", 1000)
        self.assertEqual(amount, amount)
Beispiel #2
0
    def test_adjust_sell_amount_without_enable(self):
        follower = XueQiuFollower()

        mock_user = mock.MagicMock()
        follower._users = [mock_user]

        follower._adjust_sell = False
        amount = follower._adjust_sell_amount("169101", 1000)
        self.assertEqual(amount, amount)
Beispiel #3
0
class TestXqFollower(unittest.TestCase):
    def setUp(self):
        self.follower = XueQiuFollower()
        cookies = os.getenv("EZ_TEST_XQ_COOKIES")
        if not cookies:
            return
        self.follower.login(cookies=cookies)

    def test_extract_transactions(self):
        result = self.follower.extract_transactions(TEST_XQ_PORTOFOLIO_HISTORY)
        self.assertTrue(len(result) == 1)
Beispiel #4
0
def follower(platform, **kwargs):
    """用于生成特定的券商对象
    :param platform:平台支持 ['jq', 'joinquant', '聚宽’]
    :param initial_assets: [雪球参数] 控制雪球初始资金,默认为一万,
        总资金由 initial_assets * 组合当前净值 得出
    :param total_assets: [雪球参数] 控制雪球总资金,无默认值,
        若设置则覆盖 initial_assets
    :return the class of follower

    Usage::

        >>> import easytrader
        >>> user = easytrader.use('xq')
        >>> user.prepare('xq.json')
        >>> jq = easytrader.follower('jq')
        >>> jq.login(user='******', password='******')
        >>> jq.follow(users=user, strategies=['strategies_link'])
    """
    if platform.lower() in ["rq", "ricequant", "米筐"]:
        return RiceQuantFollower()
    if platform.lower() in ["jq", "joinquant", "聚宽"]:
        return JoinQuantFollower()
    if platform.lower() in ["xq", "xueqiu", "雪球"]:
        return XueQiuFollower(**kwargs)
    raise NotImplementedError
Beispiel #5
0
    def test_adjust_sell_amount(self):
        follower = XueQiuFollower()

        mock_user = mock.MagicMock()
        follower._users = [mock_user]
        mock_user.position = TEST_POSITION

        follower._adjust_sell = True
        test_cases = [
            ("169101", 600, 600),
            ("169101", 700, 600),
            ("000000", 100, 100),
            ("sh169101", 700, 600),
        ]
        for stock_code, sell_amount, excepted_amount in test_cases:
            amount = follower._adjust_sell_amount(stock_code, sell_amount)
            self.assertEqual(amount, excepted_amount)
Beispiel #6
0
    def test_adjust_sell_amount(self):
        follower = XueQiuFollower()

        mock_user = mock.MagicMock()
        follower._users = [mock_user]
        mock_user.position = TEST_POSITION

        follower._adjust_sell = True
        test_cases = [
            ("169101", 600, 600),
            ("169101", 700, 600),
            ("000000", 100, 100),
            ("sh169101", 700, 600),
        ]
        for stock_code, sell_amount, excepted_amount in test_cases:
            amount = follower._adjust_sell_amount(stock_code, sell_amount)
            self.assertEqual(amount, excepted_amount)
Beispiel #7
0
    def test_adjust_sell_should_only_work_when_sell(self):
        follower = XueQiuFollower()
        follower._adjust_sell = True
        test_transaction = {
            "weight": 10,
            "prev_weight": 0,
            "price": 10,
            "stock_symbol": "162411",
            "created_at": int(time.time() * 1000),
        }
        test_assets = 1000

        mock_adjust_sell_amount = mock.MagicMock()
        follower._adjust_sell_amount = mock_adjust_sell_amount

        follower.project_transactions(
            transactions=[test_transaction], assets=test_assets
        )
        mock_adjust_sell_amount.assert_not_called()

        mock_adjust_sell_amount.reset_mock()
        test_transaction["prev_weight"] = test_transaction["weight"] + 1
        follower.project_transactions(
            transactions=[test_transaction], assets=test_assets
        )
        mock_adjust_sell_amount.assert_called()
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
    def test_adjust_sell_should_only_work_when_sell(self):
        follower = XueQiuFollower()
        follower._adjust_sell = True
        test_transaction = {
            "weight": 10,
            "prev_weight": 0,
            "price": 10,
            "stock_symbol": "162411",
            "created_at": int(time.time() * 1000),
        }
        test_assets = 1000

        mock_adjust_sell_amount = mock.MagicMock()
        follower._adjust_sell_amount = mock_adjust_sell_amount

        follower.project_transactions(transactions=[test_transaction],
                                      assets=test_assets)
        mock_adjust_sell_amount.assert_not_called()

        mock_adjust_sell_amount.reset_mock()
        test_transaction["prev_weight"] = test_transaction["weight"] + 1
        follower.project_transactions(transactions=[test_transaction],
                                      assets=test_assets)
        mock_adjust_sell_amount.assert_called()
Beispiel #11
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)
Beispiel #12
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)
Beispiel #13
0
 def setUp(self):
     self.follower = XueQiuFollower()
     cookies = os.getenv("EZ_TEST_XQ_COOKIES")
     if not cookies:
         return
     self.follower.login(cookies=cookies)