// obtains the data and calculates the grid of results
        private static void calculate(CalculationRunner runner)
        {
            // the trades that will have measures calculated
            IList <Trade> trades = createSwapTrades();

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

            // load quotes
            ImmutableMap <QuoteId, double> quotesCcp1 = QuotesCsvLoader.load(VAL_DATE, QUOTES_RESOURCE_CCP1);
            ImmutableMap <QuoteId, double> quotesCcp2 = QuotesCsvLoader.load(VAL_DATE, QUOTES_RESOURCE_CCP2);

            // load fixings
            ImmutableMap <ObservableId, LocalDateDoubleTimeSeries> fixings = FixingSeriesCsvLoader.load(FIXINGS_RESOURCE);

            // create the market data
            MarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValueMap(quotesCcp1).addValueMap(quotesCcp2).addTimeSeriesMap(fixings).build();

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

            // load the curve definition
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> defnsCcp1 = RatesCalibrationCsvLoader.load(GROUPS_RESOURCE_CCP1, SETTINGS_RESOURCE_CCP1, CALIBRATION_RESOURCE_CCP1);
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> defnsCcp2 = RatesCalibrationCsvLoader.load(GROUPS_RESOURCE_CCP2, SETTINGS_RESOURCE_CCP2, CALIBRATION_RESOURCE_CCP2);
            RatesCurveGroupDefinition curveGroupDefinitionCcp1 = defnsCcp1[CURVE_GROUP_NAME_CCP1].filtered(VAL_DATE, refData);
            RatesCurveGroupDefinition curveGroupDefinitionCcp2 = defnsCcp2[CURVE_GROUP_NAME_CCP2].filtered(VAL_DATE, refData);

            // the configuration that defines how to create the curves when a curve group is requested
            MarketDataConfig marketDataConfig = MarketDataConfig.builder().add(CURVE_GROUP_NAME_CCP1, curveGroupDefinitionCcp1).add(CURVE_GROUP_NAME_CCP2, curveGroupDefinitionCcp2).build();

            // the complete set of rules for calculating measures
            CalculationFunctions  functions       = StandardComponents.calculationFunctions();
            RatesMarketDataLookup ratesLookupCcp1 = RatesMarketDataLookup.of(curveGroupDefinitionCcp1);
            RatesMarketDataLookup ratesLookupCcp2 = RatesMarketDataLookup.of(curveGroupDefinitionCcp2);
            // choose RatesMarketDataLookup instance based on counterparty
            TradeCounterpartyCalculationParameter perCounterparty = TradeCounterpartyCalculationParameter.of(ImmutableMap.of(CCP1_ID, ratesLookupCcp1, CCP2_ID, ratesLookupCcp2), ratesLookupCcp1);
            CalculationRules rules = CalculationRules.of(functions, perCounterparty);

            // calibrate the curves and calculate the results
            MarketDataRequirements reqs = MarketDataRequirements.of(rules, trades, columns, refData);
            MarketData             calibratedMarketData = marketDataFactory().create(reqs, marketDataConfig, marketData, refData);
            Results results = runner.calculate(rules, trades, columns, calibratedMarketData, refData);

            // use the report runner to transform the engine results into a trade report
            ReportCalculationResults calculationResults = ReportCalculationResults.of(VAL_DATE, trades, columns, results, functions, refData);
            TradeReportTemplate      reportTemplate     = ExampleData.loadTradeReportTemplate("swap-report-template2");
            TradeReport tradeReport = TradeReport.of(calculationResults, reportTemplate);

            tradeReport.writeAsciiTable(System.out);
        }
        // obtains the data and calculates the grid of results
        private static void calculate(CalculationRunner runner)
        {
            // the trades that will have measures calculated
            IList <Trade> trades = createSwapTrades();

            // the columns, specifying the measures to be calculated
            IList <Column> columns = ImmutableList.of(Column.of(Measures.LEG_INITIAL_NOTIONAL), Column.of(Measures.PRESENT_VALUE), Column.of(Measures.LEG_PRESENT_VALUE), Column.of(Measures.PV01_CALIBRATED_SUM), Column.of(Measures.PAR_RATE), Column.of(Measures.ACCRUED_INTEREST), Column.of(Measures.PV01_CALIBRATED_BUCKETED), Column.of(AdvancedMeasures.PV01_SEMI_PARALLEL_GAMMA_BUCKETED));

            // load quotes
            ImmutableMap <QuoteId, double> quotes = QuotesCsvLoader.load(VAL_DATE, QUOTES_RESOURCE);

            // load fixings
            ImmutableMap <ObservableId, LocalDateDoubleTimeSeries> fixings = FixingSeriesCsvLoader.load(FIXINGS_RESOURCE);

            // create the market data
            MarketData marketData = MarketData.of(VAL_DATE, quotes, fixings);

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

            // load the curve definition
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> defns = RatesCalibrationCsvLoader.load(GROUPS_RESOURCE, SETTINGS_RESOURCE, CALIBRATION_RESOURCE);
            RatesCurveGroupDefinition curveGroupDefinition = defns[CURVE_GROUP_NAME].filtered(VAL_DATE, refData);

            // the configuration that defines how to create the curves when a curve group is requested
            MarketDataConfig marketDataConfig = MarketDataConfig.builder().add(CURVE_GROUP_NAME, curveGroupDefinition).build();

            // the complete set of rules for calculating measures
            CalculationFunctions  functions   = StandardComponents.calculationFunctions();
            RatesMarketDataLookup ratesLookup = RatesMarketDataLookup.of(curveGroupDefinition);
            CalculationRules      rules       = CalculationRules.of(functions, ratesLookup);

            // calibrate the curves and calculate the results
            MarketDataRequirements reqs = MarketDataRequirements.of(rules, trades, columns, refData);
            MarketData             calibratedMarketData = marketDataFactory().create(reqs, marketDataConfig, marketData, refData);
            Results results = runner.calculate(rules, trades, columns, calibratedMarketData, refData);

            // use the report runner to transform the engine results into a trade report
            ReportCalculationResults calculationResults = ReportCalculationResults.of(VAL_DATE, trades, columns, results, functions, refData);
            TradeReportTemplate      reportTemplate     = ExampleData.loadTradeReportTemplate("swap-report-template");
            TradeReport tradeReport = TradeReport.of(calculationResults, reportTemplate);

            tradeReport.writeAsciiTable(System.out);
        }
Esempio n. 3
0
        public static void Main(string[] arg)
        {
            int nbRrpWarm = 2;
            int nbRunPerf = 2;

            /* Load the curve configurations from csv files */
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> configs = RatesCalibrationCsvLoader.load(GROUP_RESOURCE, SETTINGS_RESOURCE, NODES_RESOURCE);

            /* Construct a swaps */
            ResolvedSwapTrade[] swaps = new ResolvedSwapTrade[NB_COUPONS * NB_TENORS];
            for (int loopswap = 0; loopswap < NB_COUPONS; loopswap++)
            {
                for (int looptenor = 0; looptenor < NB_TENORS; looptenor++)
                {
                    double coupon = SWAP_COUPON + loopswap * SWAP_COUPON_RANGE / NB_COUPONS;
                    swaps[looptenor * NB_COUPONS + loopswap] = GBP_FIXED_6M_LIBOR_6M.createTrade(VALUATION_DATE, SWAP_PERIOD_TO_START, Tenor.of(Period.ofYears(TENOR_START + looptenor)), BuySell.BUY, SWAP_NOTIONAL, coupon, REF_DATA).resolve(REF_DATA);
                }
            }

            /* Warm-up */
            Pair <MultiCurrencyAmount[], CurrencyParameterSensitivities[]> r = Pair.of(new MultiCurrencyAmount[0], new CurrencyParameterSensitivities[0]);

            for (int i = 0; i < nbRrpWarm; i++)
            {
                r = computation(configs, swaps);
            }

            long start, end;

            start = DateTimeHelper.CurrentUnixTimeMillis();
            for (int i = 0; i < nbRunPerf; i++)
            {
                r = computation(configs, swaps);
            }

            end = DateTimeHelper.CurrentUnixTimeMillis();
            Console.WriteLine("Computation time: " + (end - start) + " ms");

            Console.WriteLine("Performance estimate for curve calibration, " + (NB_COUPONS * NB_TENORS) + " trades and " + nbRunPerf + " repetitions.\n" + Arrays.ToString(r.First) + Arrays.ToString(r.Second));
        }
        private const double BP1 = 1.0E-4;   // Scaling by 1 bp.

        public static void Main(string[] arg)
        {
            /* Load the curve configurations from csv files */
            IList <IDictionary <CurveGroupName, RatesCurveGroupDefinition> > configs = new List <IDictionary <CurveGroupName, RatesCurveGroupDefinition> >();

            for (int loopconfig = 0; loopconfig < NB_SETTINGS; loopconfig++)
            {
                configs.Add(RatesCalibrationCsvLoader.load(GROUP_RESOURCE, SETTINGS_RESOURCE[loopconfig], NODES_RESOURCE));
            }

            /* Construct a swap */
            ResolvedSwapTrade swap = GBP_FIXED_6M_LIBOR_6M.createTrade(VALUATION_DATE, SWAP_PERIOD_TO_START, Tenor.of(SWAP_TENOR), BuySell.BUY, SWAP_NOTIONAL, SWAP_COUPON, REF_DATA).resolve(REF_DATA);

            /* Calibrate curves */
            ImmutableRatesProvider[] multicurve = new ImmutableRatesProvider[3];
            for (int loopconfig = 0; loopconfig < NB_SETTINGS; loopconfig++)
            {
                multicurve[loopconfig] = CALIBRATOR.calibrate(configs[loopconfig][CONFIG_NAME], MARKET_QUOTES, REF_DATA);
            }

            /* Computes PV and bucketed PV01 */
            MultiCurrencyAmount[]            pv  = new MultiCurrencyAmount[NB_SETTINGS];
            CurrencyParameterSensitivities[] mqs = new CurrencyParameterSensitivities[NB_SETTINGS];
            for (int loopconfig = 0; loopconfig < NB_SETTINGS; loopconfig++)
            {
                pv[loopconfig] = PRICER_SWAP.presentValue(swap, multicurve[loopconfig]);
                PointSensitivities             pts = PRICER_SWAP.presentValueSensitivity(swap, multicurve[loopconfig]);
                CurrencyParameterSensitivities ps  = multicurve[loopconfig].parameterSensitivity(pts);
                mqs[loopconfig] = MQC.sensitivity(ps, multicurve[loopconfig]);
            }

            /* Export to csv files. */
            for (int loopconfig = 0; loopconfig < NB_SETTINGS; loopconfig++)
            {
                ExportUtils.export(mqs[loopconfig], BP1, PATH_RESULTS + CONFIG_STR + SETTINGS_SUFFIX[loopconfig] + "-mqs" + SUFFIX_CSV);
                ExportUtils.export(pv[loopconfig], PATH_RESULTS + CONFIG_STR + SETTINGS_SUFFIX[loopconfig] + "-pv" + SUFFIX_CSV);
            }

            Console.WriteLine("Calibration and export finished: " + CONFIG_STR);
        }
Esempio n. 5
0
        // calculates the PV results for the instruments used in calibration from the config
        private static Pair <IList <Trade>, Results> calculate(CalculationRunner runner)
        {
            // the reference data, such as holidays and securities
            ReferenceData refData = ReferenceData.standard();

            // load quotes
            ImmutableMap <QuoteId, double> quotes = QuotesCsvLoader.load(VAL_DATE, QUOTES_RESOURCE);

            // load time series
            IDictionary <ObservableId, LocalDateDoubleTimeSeries> fixings = FixingSeriesCsvLoader.load(FIXING_RESOURCE);

            // create the market data
            MarketData marketData = ImmutableMarketData.builder(VAL_DATE).addValueMap(quotes).addTimeSeriesMap(fixings).build();

            // load the curve definition
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> defns = RatesCalibrationCsvLoader.load(GROUPS_RESOURCE, SETTINGS_RESOURCE, CALIBRATION_RESOURCE);
            RatesCurveGroupDefinition curveGroupDefinition = defns[CURVE_GROUP_NAME].filtered(VAL_DATE, refData);

            // extract the trades used for calibration
            IList <Trade> trades = curveGroupDefinition.CurveDefinitions.stream().flatMap(defn => defn.Nodes.stream()).filter(node => !(node is IborFixingDepositCurveNode)).map(node => node.trade(1d, marketData, refData)).collect(toImmutableList());

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

            // the configuration that defines how to create the curves when a curve group is requested
            MarketDataConfig marketDataConfig = MarketDataConfig.builder().add(CURVE_GROUP_NAME, curveGroupDefinition).build();

            // the complete set of rules for calculating measures
            CalculationFunctions  functions   = StandardComponents.calculationFunctions();
            RatesMarketDataLookup ratesLookup = RatesMarketDataLookup.of(curveGroupDefinition);
            CalculationRules      rules       = CalculationRules.of(functions, ratesLookup);

            // calibrate the curves and calculate the results
            MarketDataRequirements reqs = MarketDataRequirements.of(rules, trades, columns, refData);
            MarketData             calibratedMarketData = marketDataFactory().create(reqs, marketDataConfig, marketData, refData);
            Results results = runner.calculate(rules, trades, columns, calibratedMarketData, refData);

            return(Pair.of(trades, results));
        }
        private const double BP1 = 1.0E-4;   // Scaling by 1 bp.

        public static void Main(string[] arg)
        {
            /* Load the curve configurations from csv files */
            IDictionary <CurveGroupName, RatesCurveGroupDefinition> configs = RatesCalibrationCsvLoader.load(GROUP_RESOURCE, SETTINGS_RESOURCE, NODES_RESOURCE);

            /* Calibrate curves */
            ImmutableRatesProvider multicurve = CALIBRATOR.calibrate(configs[CONFIG_NAME], MARKET_QUOTES, REF_DATA);

            /* Construct a swap */
            ResolvedSwapTrade swap = GBP_FIXED_6M_LIBOR_6M.createTrade(VALUATION_DATE, SWAP_PERIOD_TO_START, Tenor.of(SWAP_TENOR), BuySell.BUY, SWAP_NOTIONAL, SWAP_COUPON, REF_DATA).resolve(REF_DATA);

            /* Computes PV and bucketed PV01 */
            MultiCurrencyAmount            pv  = PRICER_SWAP.presentValue(swap, multicurve);
            PointSensitivities             pts = PRICER_SWAP.presentValueSensitivity(swap, multicurve);
            CurrencyParameterSensitivities ps  = multicurve.parameterSensitivity(pts);
            CurrencyParameterSensitivities mqs = MQC.sensitivity(ps, multicurve);

            /* Export to csv files. */
            ExportUtils.export(mqs, BP1, PATH_RESULTS + CONFIG_STR + "-delta" + SUFFIX_CSV);
            ExportUtils.export(pv, PATH_RESULTS + CONFIG_STR + "-pv" + SUFFIX_CSV);

            Console.WriteLine("Calibration and export finished: " + CONFIG_STR);
        }