def test_get_request_return_None_when_balance_is_smaller_than_min_price( self): bnh = StrategyBuyAndHold() bnh.initialize(900, 10) bnh.is_simulation = False dummy_info = {} dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) bnh.balance = 9.5 requests = bnh.get_request() self.assertEqual(requests, None)
def test_get_request_return_turn_over_when_target_budget_is_too_small_at_simulation( self): bnh = StrategyBuyAndHold() bnh.initialize(100, 100) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["price"], 0) self.assertEqual(requests[0]["amount"], 0)
def test_get_request_return_turn_over_when_balance_is_smaller_than_min_price_at_simulation( self, ): bnh = StrategyBuyAndHold() bnh.initialize(900, 10) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000 bnh.update_trading_info(dummy_info) bnh.balance = 9.5 requests = bnh.get_request() self.assertEqual(requests[0]["price"], 0) self.assertEqual(requests[0]["amount"], 0)
def test_get_request_return_same_datetime_at_simulation(self): bnh = StrategyBuyAndHold() bnh.initialize(1000, 100) bnh.is_simulation = True dummy_info = {} dummy_info["date_time"] = "2020-02-25T15:41:09" dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["date_time"], "2020-02-25T15:41:09") dummy_info["date_time"] = "2020-02-25T23:59:59" dummy_info["closing_price"] = 20000000 bnh.update_trading_info(dummy_info) requests = bnh.get_request() self.assertEqual(requests[0]["date_time"], "2020-02-25T23:59:59")
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)