def test_get_score_should_call_work_post_task_with_correct_task(self): operator = SimulationOperator() strategy = MagicMock() trader = MagicMock() analyzer = MagicMock() operator.initialize("banana", strategy, trader, analyzer) operator.worker = MagicMock() operator.state = "running" operator.get_score("dummy", index_info=7) operator.worker.post_task.assert_called_once_with({ "runnable": ANY, "callback": "dummy", "index_info": 7 }) operator.analyzer = MagicMock() operator.analyzer.get_return_report.return_value = "grape" task = { "runnable": MagicMock(), "callback": MagicMock(), "index_info": 10 } runnable = operator.worker.post_task.call_args[0][0]["runnable"] runnable(task) operator.analyzer.get_return_report.assert_called_once_with( graph_filename=ANY, index_info=10) task["callback"].assert_called_once_with("grape")
def test_get_score_call_callback_with_last_report_when_state_is_NOT_running( self): operator = SimulationOperator() strategy = MagicMock() trader = MagicMock() analyzer = MagicMock() operator.initialize("banana", strategy, trader, analyzer) operator.worker = MagicMock() operator.state = "pear" operator.last_report = {"summary": "apple_report"} callback = MagicMock() operator.get_score(callback) operator.worker.post_task.assert_not_called() callback.assert_called_once_with("apple_report")
def test__periodic_internal_get_score_should_call_get_score_correctlye( self): operator = SimulationOperator() operator.get_score = MagicMock() operator.last_periodic_turn = operator.current_turn operator._periodic_internal_get_score() operator.get_score.assert_not_called() operator.current_turn = operator.last_periodic_turn + operator.PERIODIC_RECORD_INTERVAL_TURN operator._periodic_internal_get_score() operator.get_score.assert_called_with( ANY, index_info=operator.PERIODIC_RECORD_INFO, graph_tag=ANY)
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)