Beispiel #1
0
        public virtual void test_scenarioMarketData()
        {
            ScenarioMarketData    marketDataCalibrated = StandardComponents.marketDataFactory().createMultiScenario(REQUIREMENTS, SCENARIO_CONFIG, SCENARIO_MARKET_DATA, REF_DATA, ScenarioDefinition.empty());
            Results               results = CALC_RUNNER.calculateMultiScenario(RULES, TARGETS, COLUMN, marketDataCalibrated, REF_DATA);
            CurrencyScenarioArray pvs     = results.get(0, 0, typeof(CurrencyScenarioArray)).Value;
            CurrencyAmount        pv0     = PRICER.presentValue(OPTION_TRADE.resolve(REF_DATA), EXP_RATES, EXP_VOLS).convertedTo(USD, EXP_RATES);
            CurrencyAmount        pv1     = PRICER.presentValue(OPTION_TRADE.resolve(REF_DATA), EXP_RATES_1, EXP_VOLS_1).convertedTo(USD, EXP_RATES_1);

            assertEquals(pvs.get(0), pv0);
            assertEquals(pvs.get(1), pv1);
        }
        // obtains the data and calculates the grid of results
        private static void calculate(CalculationRunner runner)
        {
            // the trade that will have measures calculated
            IList <Trade> trades = ImmutableList.of(createVanillaFixedVsLibor3mSwap());

            // the columns, specifying the measures to be calculated
            IList <Column> columns = ImmutableList.of(Column.of(Measures.PRESENT_VALUE), Column.of(Measures.PV01_CALIBRATED_SUM));

            // use the built-in example market data
            ExampleMarketDataBuilder marketDataBuilder = ExampleMarketData.builder();

            // the complete set of rules for calculating measures
            LocalDate            valuationDate = LocalDate.of(2014, 1, 22);
            CalculationFunctions functions     = StandardComponents.calculationFunctions();
            CalculationRules     rules         = CalculationRules.of(functions, Currency.USD, marketDataBuilder.ratesLookup(valuationDate));

            // mappings that select which market data to apply perturbations to
            // this applies the perturbations above to all curves
            PerturbationMapping <Curve> mapping = PerturbationMapping.of(MarketDataFilter.ofIdType(typeof(CurveId)), CurveParallelShifts.absolute(0, ONE_BP));

            // create a scenario definition containing the single mapping above
            // this creates two scenarios - one for each perturbation in the mapping
            ScenarioDefinition scenarioDefinition = ScenarioDefinition.ofMappings(mapping);

            // build a market data snapshot for the valuation date
            MarketData marketData = marketDataBuilder.buildSnapshot(valuationDate);

            // the reference data, such as holidays and securities
            ReferenceData refData = ReferenceData.standard();

            // calculate the results
            MarketDataRequirements reqs = MarketDataRequirements.of(rules, trades, columns, refData);
            ScenarioMarketData     scenarioMarketData = marketDataFactory().createMultiScenario(reqs, MarketDataConfig.empty(), marketData, refData, scenarioDefinition);
            Results results = runner.calculateMultiScenario(rules, trades, columns, scenarioMarketData, refData);

            // TODO Replace the results processing below with a report once the reporting framework supports scenarios

            // The results are lists of currency amounts containing one value for each scenario
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: com.opengamma.strata.data.scenario.ScenarioArray<?> pvList = (com.opengamma.strata.data.scenario.ScenarioArray<?>) results.get(0, 0).getValue();
            ScenarioArray <object> pvList = (ScenarioArray <object>)results.get(0, 0).Value;
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: com.opengamma.strata.data.scenario.ScenarioArray<?> pv01List = (com.opengamma.strata.data.scenario.ScenarioArray<?>) results.get(0, 1).getValue();
            ScenarioArray <object> pv01List = (ScenarioArray <object>)results.get(0, 1).Value;

            double       pvBase       = ((CurrencyAmount)pvList.get(0)).Amount;
            double       pvShifted    = ((CurrencyAmount)pvList.get(1)).Amount;
            double       pv01Base     = ((CurrencyAmount)pv01List.get(0)).Amount;
            NumberFormat numberFormat = new DecimalFormat("###,##0.00", new DecimalFormatSymbols(Locale.ENGLISH));

            Console.WriteLine("                         PV (base) = " + numberFormat.format(pvBase));
            Console.WriteLine("             PV (1 bp curve shift) = " + numberFormat.format(pvShifted));
            Console.WriteLine("PV01 (algorithmic differentiation) = " + numberFormat.format(pv01Base));
            Console.WriteLine("          PV01 (finite difference) = " + numberFormat.format(pvShifted - pvBase));
        }
Beispiel #3
0
        // obtains the data and calculates the grid of results
        private static void calculate(CalculationRunner runner)
        {
            // the trades for which to calculate a P&L series
            IList <Trade> trades = ImmutableList.of(createTrade());

            // the columns, specifying the measures to be calculated
            IList <Column> columns = ImmutableList.of(Column.of(Measures.PRESENT_VALUE));

            // use the built-in example historical scenario market data
            ExampleMarketDataBuilder marketDataBuilder = ExampleMarketDataBuilder.ofResource(MARKET_DATA_RESOURCE_ROOT);

            // the complete set of rules for calculating measures
            CalculationFunctions functions = StandardComponents.calculationFunctions();
            CalculationRules     rules     = CalculationRules.of(functions, marketDataBuilder.ratesLookup(LocalDate.of(2015, 4, 23)));

            // load the historical calibrated curves from which we will build our scenarios
            // these curves are provided in the example data environment
            SortedDictionary <LocalDate, RatesCurveGroup> historicalCurves = marketDataBuilder.loadAllRatesCurves();

            // sorted list of dates for the available series of curves
            // the entries in the P&L vector we produce will correspond to these dates
            IList <LocalDate> scenarioDates = new List <LocalDate>(historicalCurves.Keys);

            // build the historical scenarios
            ScenarioDefinition historicalScenarios = buildHistoricalScenarios(historicalCurves, scenarioDates);

            // build a market data snapshot for the valuation date
            // this is the base snapshot which will be perturbed by the scenarios
            LocalDate  valuationDate = LocalDate.of(2015, 4, 23);
            MarketData marketData    = marketDataBuilder.buildSnapshot(valuationDate);

            // the reference data, such as holidays and securities
            ReferenceData refData = ReferenceData.standard();

            // calculate the results
            MarketDataRequirements reqs = MarketDataRequirements.of(rules, trades, columns, refData);
            ScenarioMarketData     scenarioMarketData = marketDataFactory().createMultiScenario(reqs, MarketDataConfig.empty(), marketData, refData, historicalScenarios);
            Results results = runner.calculateMultiScenario(rules, trades, columns, scenarioMarketData, refData);

            // the results contain the one measure requested (Present Value) for each scenario
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: com.opengamma.strata.data.scenario.ScenarioArray<?> scenarioValuations = (com.opengamma.strata.data.scenario.ScenarioArray<?>) results.get(0, 0).getValue();
            ScenarioArray <object> scenarioValuations = (ScenarioArray <object>)results.get(0, 0).Value;

            outputPnl(scenarioDates, scenarioValuations);
        }
        //-------------------------------------------------------------------------
        public virtual void calculate()
        {
            ImmutableList <CalculationTarget> targets = ImmutableList.of(TARGET);
            Column column1 = Column.of(TestingMeasures.PRESENT_VALUE);
            Column column2 = Column.of(TestingMeasures.BUCKETED_PV01);
            ImmutableList <Column> columns = ImmutableList.of(column1, column2);
            CalculationRules       rules   = CalculationRules.of(CalculationFunctions.empty());
            MarketData             md      = MarketData.empty(date(2016, 6, 30));
            ScenarioMarketData     smd     = ScenarioMarketData.empty();

            // use of try-with-resources checks class is AutoCloseable
            using (CalculationRunner test = CalculationRunner.of(MoreExecutors.newDirectExecutorService()))
            {
                assertThat(test.calculate(rules, targets, columns, md, REF_DATA).get(0, 0).Failure).True;
                assertThat(test.calculateMultiScenario(rules, targets, columns, smd, REF_DATA).get(0, 0).Failure).True;
            }
        }