Beispiel #1
0
def register_market_calibration(price_process_name, calibration_params):
    created_event = MarketCalibration.Created(entity_id=create_uuid4(),
                                              price_process_name=price_process_name,
                                              calibration_params=calibration_params)
    call_result = MarketCalibration.mutator(event=created_event)
    publish(created_event)
    return call_result
 def test_register_dependency_graph(self):
     contract_specification_id = create_uuid4()
     dependency_graph = register_dependency_graph(contract_specification_id)
     self.assertIsInstance(dependency_graph, DependencyGraph)
     assert isinstance(dependency_graph, DependencyGraph)
     self.assertEqual(dependency_graph.contract_specification_id,
                      contract_specification_id)
    def test_register_call_result(self):
        contract_specification_id = create_uuid4()
        contract_valuation_id = create_uuid4()
        call_id = create_uuid4()

        call_result_id = make_call_result_id(contract_valuation_id, call_id)
        self.assertRaises(KeyError, self.app.call_result_repo.__getitem__, call_result_id)

        register_call_result(call_id=call_id, result_value=123, perturbed_values={},
                             contract_valuation_id=contract_valuation_id,
                             contract_specification_id=contract_specification_id,
                             involved_market_names=[])

        call_result = self.app.call_result_repo[call_result_id]
        assert isinstance(call_result, CallResult)
        self.assertEqual(call_result.result_value, 123)
 def _test_handle_result(self):
     app = get_quantdsl_app()
     call_id = create_uuid4()
     app.register_call_requirement(call_id, '1 + 2', datetime.datetime.now())
     app.register_call_dependencies(call_id, [])
     app.register_call_dependents(call_id, [])
     celery_handle_result(call_id, 3)
     return
def register_market_calibration(price_process_name, calibration_params):
    created_event = MarketCalibration.Created(
        entity_id=create_uuid4(),
        price_process_name=price_process_name,
        calibration_params=calibration_params)
    call_result = MarketCalibration.mutator(event=created_event)
    publish(created_event)
    return call_result
    def test_register_call_result(self):
        dependency_graph_id = create_uuid4()
        contract_valuation_id = create_uuid4()
        call_id = create_uuid4()

        call_result_id = make_call_result_id(contract_valuation_id, call_id)
        self.assertRaises(KeyError, self.app.call_result_repo.__getitem__,
                          call_result_id)

        register_call_result(call_id=call_id,
                             result_value=123,
                             perturbed_values={},
                             contract_valuation_id=contract_valuation_id,
                             dependency_graph_id=dependency_graph_id)

        call_result = self.app.call_result_repo[call_result_id]
        assert isinstance(call_result, CallResult)
        self.assertEqual(call_result.result_value, 123)
Beispiel #7
0
def register_contract_specification(source_code, observation_date=None):
    created_event = ContractSpecification.Created(
        entity_id=create_uuid4(),
        source_code=source_code,
        observation_date=observation_date,
    )
    contract_specification = ContractSpecification.mutator(event=created_event)
    publish(created_event)
    return contract_specification
def register_contract_specification(source_code, observation_date=None):
    created_event = ContractSpecification.Created(
        entity_id=create_uuid4(),
        source_code=source_code,
        observation_date=observation_date,
    )
    contract_specification = ContractSpecification.mutator(event=created_event)
    publish(created_event)
    return contract_specification
Beispiel #9
0
    def test_register_call_result(self):
        call_id = create_uuid4()

        self.assertRaises(KeyError, self.app.call_result_repo.__getitem__, call_id)

        self.app.register_call_result(call_id=call_id, result_value=123)

        call_result = self.app.call_result_repo[call_id]
        assert isinstance(call_result, CallResult)
        self.assertEqual(call_result.result_value, 123)
Beispiel #10
0
    def test_register_call_result(self):
        contract_specification_id = create_uuid4()
        contract_valuation_id = create_uuid4()
        call_id = create_uuid4()

        call_result_id = make_call_result_id(contract_valuation_id, call_id)
        self.assertRaises(KeyError, self.app.call_result_repo.__getitem__,
                          call_result_id)

        register_call_result(
            call_id=call_id,
            result_value=123,
            perturbed_values={},
            contract_valuation_id=contract_valuation_id,
            contract_specification_id=contract_specification_id,
            involved_market_names=[])

        call_result = self.app.call_result_repo[call_result_id]
        assert isinstance(call_result, CallResult)
        self.assertEqual(call_result.result_value, 123)
Beispiel #11
0
    def test_register_call_dependencies(self):
        call_id = create_uuid4()

        self.assertRaises(KeyError, self.app.call_dependencies_repo.__getitem__, call_id)

        dependencies = ['123', '456']

        self.app.register_call_dependencies(call_id=call_id, dependencies=dependencies)

        call_dependencies = self.app.call_dependencies_repo[call_id]
        assert isinstance(call_dependencies, CallDependencies)
        self.assertEqual(call_dependencies.dependencies, dependencies)
Beispiel #12
0
def register_market_simulation(market_calibration_id, observation_date, requirements, path_count, interest_rate):
    assert isinstance(requirements, list), type(requirements)
    created_event = MarketSimulation.Created(entity_id=create_uuid4(),
                                             market_calibration_id=market_calibration_id,
                                             requirements=requirements,
                                             observation_date=observation_date,
                                             path_count=path_count,
                                             interest_rate=interest_rate,
                                             )
    call_result = MarketSimulation.mutator(event=created_event)
    publish(created_event)
    return call_result
    def test_register_call_requirements(self):
        call_id = create_uuid4()
        contract_specification_id = create_uuid4()

        self.assertRaises(KeyError, self.app.call_requirement_repo.__getitem__, call_id)

        dsl_source = '1 + 1'
        present_time = datetime.datetime(2015, 9, 7, 0, 0, 0)

        register_call_requirement(
            call_id=call_id,
            dsl_source=dsl_source,
            present_time=present_time,
            contract_specification_id=contract_specification_id,
            cost=1,
        )

        call_requirement = self.app.call_requirement_repo[call_id]
        assert isinstance(call_requirement, CallRequirement)
        self.assertEqual(call_requirement.dsl_source, dsl_source)
        self.assertEqual(call_requirement.present_time, present_time)
        self.assertEqual(call_requirement.contract_specification_id, contract_specification_id)
    def _test_evaluate_call(self):
        # Check the example task works directly.
        # - set up the call requirement
        app = get_quantdsl_app()
        call_id = create_uuid4()
        app.register_call_requirement(call_id, '1 + 2', datetime.datetime.now())
        app.register_call_dependencies(call_id, [])
        app.register_call_dependents(call_id, [])

        celery_evaluate_call(call_id)
        call_result = app.call_result_repo[call_id]
        assert isinstance(call_result, CallResult)
        self.assertEqual(call_result.result_value, 3)
Beispiel #15
0
def register_market_simulation(market_calibration_id, market_names, fixing_dates, observation_date, path_count,
                               interest_rate):
    created_event = MarketSimulation.Created(entity_id=create_uuid4(),
                                             market_calibration_id=market_calibration_id,
                                             market_names=market_names,
                                             fixing_dates=fixing_dates,
                                             observation_date=observation_date,
                                             path_count=path_count,
                                             interest_rate=interest_rate,
                                             )
    call_result = MarketSimulation.mutator(event=created_event)
    publish(created_event)
    return call_result
Beispiel #16
0
    def test_register_simulated_price(self):

        price_time = datetime.datetime(2011, 1, 1)
        price_value = numpy.array([1.1, 1.2, 1.367345987359734598734598723459872345987235698237459862345])
        simulation_id = create_uuid4()
        self.assertRaises(KeyError, self.app.simulated_price_repo.__getitem__, simulation_id)

        price = register_simulated_price(simulation_id, '#1', price_time, price_value)

        assert isinstance(price, SimulatedPrice), price
        assert price.id
        price = self.app.simulated_price_repo[make_simulated_price_id(simulation_id, '#1', price_time)]
        assert isinstance(price, SimulatedPrice)
        numpy.testing.assert_equal(price.value, price_value)
Beispiel #17
0
    def test_register_call_requirements(self):
        call_id = create_uuid4()

        self.assertRaises(KeyError, self.app.call_requirement_repo.__getitem__, call_id)

        dsl_source = '1 + 1'
        effective_present_time = datetime.datetime(2015, 9, 7, 0, 0, 0)

        self.app.register_call_requirement(call_id=call_id, dsl_source=dsl_source,
                                      effective_present_time=effective_present_time)

        call_requirement = self.app.call_requirement_repo[call_id]
        assert isinstance(call_requirement, CallRequirement)
        self.assertEqual(call_requirement.dsl_source, dsl_source)
        self.assertEqual(call_requirement.effective_present_time, effective_present_time)
Beispiel #18
0
def register_market_simulation(market_calibration_id, market_names,
                               fixing_dates, observation_date, path_count,
                               interest_rate):
    created_event = MarketSimulation.Created(
        entity_id=create_uuid4(),
        market_calibration_id=market_calibration_id,
        market_names=market_names,
        fixing_dates=fixing_dates,
        observation_date=observation_date,
        path_count=path_count,
        interest_rate=interest_rate,
    )
    call_result = MarketSimulation.mutator(event=created_event)
    publish(created_event)
    return call_result
Beispiel #19
0
    def test_register_call_requirements(self):
        call_id = create_uuid4()
        contract_specification_id = create_uuid4()

        self.assertRaises(KeyError, self.app.call_requirement_repo.__getitem__,
                          call_id)

        dsl_source = '1 + 1'
        present_time = datetime.datetime(2015, 9, 7, 0, 0, 0)

        register_call_requirement(
            call_id=call_id,
            dsl_source=dsl_source,
            present_time=present_time,
            contract_specification_id=contract_specification_id,
            cost=1,
        )

        call_requirement = self.app.call_requirement_repo[call_id]
        assert isinstance(call_requirement, CallRequirement)
        self.assertEqual(call_requirement.dsl_source, dsl_source)
        self.assertEqual(call_requirement.present_time, present_time)
        self.assertEqual(call_requirement.contract_specification_id,
                         contract_specification_id)
def register_market_simulation(market_calibration_id, observation_date, requirements, path_count, interest_rate,
                               perturbation_factor):

    # Todo: Eliminate duplicate requirements before publishing the event.

    assert isinstance(requirements, list), type(requirements)
    created_event = MarketSimulation.Created(entity_id=create_uuid4(),
                                             market_calibration_id=market_calibration_id,
                                             requirements=requirements,
                                             observation_date=observation_date,
                                             path_count=path_count,
                                             interest_rate=interest_rate,
                                             perturbation_factor=perturbation_factor
                                             )
    call_result = MarketSimulation.mutator(event=created_event)
    publish(created_event)
    return call_result
Beispiel #21
0
def register_market_simulation(market_calibration_id, observation_date,
                               requirements, path_count, interest_rate,
                               perturbation_factor):

    # Todo: Eliminate duplicate requirements before publishing the event.

    assert isinstance(requirements, list), type(requirements)
    created_event = MarketSimulation.Created(
        entity_id=create_uuid4(),
        market_calibration_id=market_calibration_id,
        requirements=requirements,
        observation_date=observation_date,
        path_count=path_count,
        interest_rate=interest_rate,
        perturbation_factor=perturbation_factor)
    call_result = MarketSimulation.mutator(event=created_event)
    publish(created_event)
    return call_result
def create_contract_valuation_id():
    return create_uuid4()
Beispiel #23
0
def register_contract_valuation(dependency_graph_id):
    created_event = ContractValuation.Created(
        entity_id=create_uuid4(), dependency_graph_id=dependency_graph_id)
    contract_specification = ContractValuation.mutator(event=created_event)
    publish(created_event)
    return contract_specification
def register_contract_specification(specification):
    created_event = ContractSpecification.Created(entity_id=create_uuid4(), specification=specification)
    contract_specification = ContractSpecification.mutator(event=created_event)
    publish(created_event)
    return contract_specification
def register_contract_valuation(dependency_graph_id):
    created_event = ContractValuation.Created(entity_id=create_uuid4(), dependency_graph_id=dependency_graph_id)
    contract_specification = ContractValuation.mutator(event=created_event)
    publish(created_event)
    return contract_specification
    def _test_distributed_dependency_graph_runner(self):
        # Setup the contract.
        #  - branching function calls
        dsl_source = """
def Swing(starts, ends, underlying, quantity):
    if (quantity == 0) or (starts >= ends):
        0
    else:
        Wait(starts, Choice(
            Swing(starts + TimeDelta('1d'), ends, underlying, quantity - 1) + Fixing(starts, underlying),
            Swing(starts + TimeDelta('1d'), ends, underlying, quantity)
        ))
Swing(Date('2011-01-01'), Date('2011-01-03'), 10, 50)
"""

        # Generate the dependency graph.
        dependency_graph = dsl_compile(dsl_source, is_parallel=True)
        assert isinstance(dependency_graph, DependencyGraph)

        # Remember the number of stubbed exprs - will check it after the evaluation.
        actual_len_stubbed_exprs = len(dependency_graph.call_requirements)

        kwds = {
            'interest_rate': 0,
            'present_time': datetime.datetime(2011, 1, 1, tzinfo=utc),
            'simulation_id': create_uuid4(),
        }
        app = get_quantdsl_app()
        market_simulation = app.register_market_simulation({})

        market_names = ['#1']
        for market_name in market_names:
            # NB Need enough days to cover the date range in the dsl_source.
            for i in range(0, 10):
                dt = datetime.datetime(2011, 1, 1, tzinfo=utc) + datetime.timedelta(1) * i
                value = numpy.array([10] * 2000)
                register_simulated_price(market_simulation.id, market_name, fixing_date=dt)

        # Check we've got a path to the 'celery' command line program (hopefully it's next to this python executable).
        celery_script_path = os.path.join(os.path.dirname(sys.executable), 'celery')
        self.assertTrue(os.path.exists(celery_script_path), celery_script_path)

        # Check the example task returns correct result (this assumes the celery worker IS running).
        # - invoke a celery worker process as a subprocess
        worker_cmd = [celery_script_path, 'worker', '-A', 'quantdsl.infrastructure.celery.tasks', '-l', 'info']
        # - its usage as a context manager causes a wait for it to finish after it has been terminated
        with Popen(worker_cmd) as worker:
            try:
                # Evaluate the dependency graph.
                runner = DistributedDependencyGraphRunner(dependency_graph, app=app)
                dsl_value = runner.evaluate(**kwds)

                # Get the mean of the value, if it has one.
                if isinstance(dsl_value, numpy.ndarray):
                    dsl_value = dsl_value.mean()

                # Check the value is expected.
                expected_value = 20
                self.assertEqual(dsl_value, expected_value)

                # Check the number of stubbed exprs is expected.
                expected_len_stubbed_exprs = 7
                self.assertEqual(actual_len_stubbed_exprs, expected_len_stubbed_exprs)

            finally:
                # Shutdown the celery worker.
                worker.terminate()
def create_contract_valuation_id():
    return create_uuid4()
 def test_create_uuid4(self):
     id = create_uuid4()
     self.assertIsInstance(id, six.string_types)
 def test_create_uuid4(self):
     id = create_uuid4()
     self.assertIsInstance(id, six.string_types)
def register_contract_specification(specification):
    created_event = ContractSpecification.Created(entity_id=create_uuid4(),
                                                  specification=specification)
    contract_specification = ContractSpecification.mutator(event=created_event)
    publish(created_event)
    return contract_specification
Beispiel #31
0
 def test_register_dependency_graph(self):
     contract_specification_id = create_uuid4()
     dependency_graph = register_dependency_graph(contract_specification_id)
     self.assertIsInstance(dependency_graph, DependencyGraph)
     assert isinstance(dependency_graph, DependencyGraph)
     self.assertEqual(dependency_graph.contract_specification_id, contract_specification_id)