Exemplo n.º 1
0
    def test_get_account_info_call_raise_exception_UserWarning_when_callback_make_TypeError(
        self, ):
        trader = SimulationTrader()
        trader.is_initialized = True

        with self.assertRaises(UserWarning):
            trader.get_account_info()
Exemplo n.º 2
0
    def test_get_account_info_call_raise_exception_UserWarning_when_is_initialized_False(
        self, ):
        trader = SimulationTrader()
        trader.is_initialized = False

        with self.assertRaises(UserWarning):
            trader.get_account_info()
Exemplo n.º 3
0
    def test_send_request_call_raise_exception_UserWarning_when_callback_make_TypeError(
            self):
        trader = SimulationTrader()
        trader.is_initialized = True

        with self.assertRaises(UserWarning):
            trader.send_request(None, None)
Exemplo n.º 4
0
 def test_get_account_info_call_callback_with_virtual_market_get_balance_result(
         self):
     trader = SimulationTrader()
     trader.is_initialized = True
     trader.market.get_balance = MagicMock(return_value="banana")
     self.assertEqual(trader.get_account_info(), "banana")
     trader.market.get_balance.assert_called_once()
Exemplo n.º 5
0
    def test_send_request_call_raise_exception_UserWarning_when_is_initialized_False(
            self):
        trader = SimulationTrader()
        trader.is_initialized = False

        with self.assertRaises(UserWarning):
            trader.send_request(None, None)
Exemplo n.º 6
0
    def test_initialize_simulation_set_is_initialized_False_when_invalid_market(
            self):
        trader = SimulationTrader()
        trader.market = "make exception"
        with self.assertRaises(AttributeError):
            trader.initialize_simulation("mango", 500, 5000)

        self.assertEqual(trader.is_initialized, False)
Exemplo n.º 7
0
    def test_initialize_simulation_initialize_virtual_market(self):
        trader = SimulationTrader()
        trader.market.initialize = MagicMock()
        trader.market.deposit = MagicMock()

        trader.initialize_simulation("mango", 500, 5000)

        trader.market.initialize.assert_called_once_with("mango", 500, 5000)
        self.assertEqual(trader.is_initialized, True)
Exemplo n.º 8
0
    def test_send_request_call_callback_with_result_of_market_handle_quest(
            self):
        trader = SimulationTrader()
        trader.is_initialized = True

        dummy_requests = [{
            "id": "mango",
            "type": "orange",
            "price": 500,
            "amount": 10
        }]
        callback = MagicMock()
        trader.market.handle_request = MagicMock(return_value="banana")
        trader.send_request(dummy_requests, callback)
        trader.market.handle_request.assert_called_once_with(dummy_requests[0])
        callback.assert_called_once_with("banana")
Exemplo n.º 9
0
    def test_get_account_info_call_raise_exception_UserWarning_when_market_is_invalid(
        self, ):
        trader = SimulationTrader()
        trader.is_initialized = True
        trader.market = "make exception"

        with self.assertRaises(UserWarning):
            trader.get_account_info()
Exemplo n.º 10
0
    def test_send_request_call_raise_exception_UserWarning_when_market_is_invalid(
            self):
        trader = SimulationTrader()
        trader.is_initialized = True
        trader.market = "make exception"

        with self.assertRaises(UserWarning):
            trader.send_request(None, None)
Exemplo n.º 11
0
    def test_ITG_run_simulation_with_bnh_strategy(self):
        trading_snapshot = simulation_data.get_data("bnh_snapshot")
        operator = SimulationOperator()
        strategy = StrategyBuyAndHold()
        strategy.is_simulation = True
        count = 100
        budget = 100000
        interval = 0.001
        time_limit = 15
        end_str = "2020-04-30T16:30:00"

        data_provider = SimulationDataProvider()
        data_provider.initialize_simulation(end=end_str, count=count)
        trader = SimulationTrader()
        trader.initialize_simulation(end=end_str, count=count, budget=budget)
        analyzer = Analyzer()
        analyzer.is_simulation = True

        operator.initialize(
            data_provider,
            strategy,
            trader,
            analyzer,
            budget=budget,
        )

        operator.set_interval(interval)
        operator.start()
        start_time = time.time()
        while operator.state == "running":
            time.sleep(0.5)
            if time.time() - start_time > time_limit:
                self.assertTrue(False, "Time out")
                break

        trading_results = operator.get_trading_results()
        self.check_equal_results_list(trading_results, trading_snapshot)

        waiting = True
        start_time = time.time()
        report = None

        def callback(return_report):
            nonlocal report
            nonlocal waiting
            report = return_report
            waiting = False
            self.assertFalse(waiting)

        operator.get_score(callback)

        while waiting:
            time.sleep(0.5)
            if time.time() - start_time > time_limit:
                self.assertTrue(False, "Time out")
                break

        self.assertIsNotNone(report)
        self.assertEqual(report[0], 100000)
        self.assertEqual(report[1], 97220)
        self.assertEqual(report[2], -2.78)
        self.assertEqual(report[3]["KRW-BTC"], -2.693)
Exemplo n.º 12
0
    def test_ITG_simulation_trader_full(self):
        trader = SimulationTrader()
        end_date = "2020-04-30T16:30:00"
        trader.initialize_simulation(end=end_date, count=50, budget=50000)

        # 1 거래 요청 - buy
        request = [{
            "id": "request_1",
            "type": "buy",
            "price": 11372000.0,
            "amount": 0.0009,
            "date_time": "2020-04-30T14:40:00",
        }]
        expected_result = {
            "request": {
                "id": "request_1",
                "type": "buy",
                "price": 11372000.0,
                "amount": 0.0009,
                "date_time": "2020-04-30T14:40:00",
            },
            "type": "buy",
            "price": 11372000.0,
            "amount": 0.0009,
            "msg": "success",
            "balance": 39760,
            "state": "done",
            "date_time": "2020-04-30T15:40:00",
        }

        result = None

        def send_request_callback(callback_result):
            nonlocal result
            result = callback_result

        trader.send_request(request, send_request_callback)
        self.assertEqual(result, expected_result)

        # 2 거래 요청 - sell
        request = [{
            "id": "request_2",
            "type": "sell",
            "price": 11292000.0,
            "amount": 0.0003,
            "date_time": "2020-04-30T14:41:00",
        }]
        expected_result = {
            "request": {
                "id": "request_2",
                "type": "sell",
                "price": 11292000.0,
                "amount": 0.0003,
                "date_time": "2020-04-30T14:41:00",
            },
            "type": "sell",
            "price": 11292000.0,
            "amount": 0.0003,
            "msg": "success",
            "balance": 43146,
            "state": "done",
            "date_time": "2020-04-30T15:41:00",
        }

        result = None

        def send_request_callback(callback_result):
            nonlocal result
            result = callback_result

        trader.send_request(request, send_request_callback)
        self.assertEqual(result, expected_result)

        # 3 계좌 정보 요청
        expected_account_info = {
            "balance": 43146,
            "asset": {
                "KRW-BTC": (11372000.0, 0.0006)
            },
            "quote": {
                "KRW-BTC": 11370000.0
            },
            "date_time": "2020-04-30T15:42:00",
        }

        account_info = trader.get_account_info()
        self.assertEqual(account_info, expected_account_info)

        # 4 거래 요청 - buy 큰 금액으로 매수 시도
        request = [{
            "id": "request_3",
            "type": "sell",
            "price": 13292000.0,
            "amount": 0.0003,
            "date_time": "2020-04-30T14:43:00",
        }]
        expected_result = "pass"
        result = None

        def send_request_callback(callback_result):
            nonlocal result
            result = callback_result

        trader.send_request(request, send_request_callback)
        self.assertEqual(result, expected_result)

        # 5 계좌 정보 요청 - 변함 없음 확인
        expected_account_info = {
            "balance": 43146,
            "asset": {
                "KRW-BTC": (11372000.0, 0.0006)
            },
            "quote": {
                "KRW-BTC": 11365000.0
            },
            "date_time": "2020-04-30T15:43:00",
        }

        account_info = trader.get_account_info()
        self.assertEqual(account_info, expected_account_info)

        # 6 거래 요청 - sell 보유 수량보다 큰 수량 매도
        request = [{
            "id": "request_4",
            "type": "sell",
            "price": 11302000.0,
            "amount": 0.0013,
            "date_time": "2020-04-30T14:44:00",
        }]
        expected_result = {
            "request": {
                "id": "request_4",
                "type": "sell",
                "price": 11302000.0,
                "amount": 0.0013,
                "date_time": "2020-04-30T14:44:00",
            },
            "type": "sell",
            "price": 11302000.0,
            "amount": 0.0006,
            "msg": "success",
            "balance": 49924,
            "state": "done",
            "date_time": "2020-04-30T15:43:00",
        }

        result = None

        def send_request_callback(callback_result):
            nonlocal result
            result = callback_result

        trader.send_request(request, send_request_callback)
        print(result)
        self.assertEqual(result, expected_result)

        # 7 계좌 정보 요청
        expected_account_info = {
            "balance": 49924,
            "asset": {},
            "quote": {
                "KRW-BTC": 11358000.0
            },
            "date_time": "2020-04-30T15:44:00",
        }

        account_info = trader.get_account_info()
        self.assertEqual(account_info, expected_account_info)