@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Object positionOrNode = getTarget(target); final ConventionBundleSource conventionSource = OpenGammaExecutionContext.getConventionBundleSource(executionContext); final ConventionBundle bundle = conventionSource.getConventionBundle( ExternalId.of(InMemoryConventionBundleMaster.SIMPLE_NAME_SCHEME, "USD_CAPM")); final Clock snapshotClock = executionContext.getValuationClock(); final LocalDate now = snapshotClock.zonedDateTime().toLocalDate(); final ValueRequirement desiredValue = desiredValues.iterator().next(); final ValueProperties constraints = desiredValue.getConstraints(); final Period samplingPeriod = getSamplingPeriod(constraints.getValues(ValuePropertyNames.SAMPLING_PERIOD)); final LocalDate startDate = now.minus(samplingPeriod); final HistoricalTimeSeries riskFreeRateTSObject = (HistoricalTimeSeries) inputs.getValue(ValueRequirementNames.HISTORICAL_TIME_SERIES); final Object assetPnLObject = inputs.getValue( new ValueRequirement( ValueRequirementNames.PNL_SERIES, positionOrNode)); // TODO replace with return series when portfolio weights are in if (assetPnLObject == null) { throw new OpenGammaRuntimeException("Asset P&L was null"); } final Object assetFairValueObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.FAIR_VALUE, positionOrNode)); if (assetFairValueObject == null) { throw new OpenGammaRuntimeException("Asset fair value was null"); } final Object betaObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.CAPM_BETA, positionOrNode)); if (betaObject == null) { throw new OpenGammaRuntimeException("Beta was null"); } final double beta = (Double) betaObject; final double fairValue = (Double) assetFairValueObject; DoubleTimeSeries<?> assetReturnTS = ((DoubleTimeSeries<?>) assetPnLObject).divide(fairValue); DoubleTimeSeries<?> riskFreeReturnTS = riskFreeRateTSObject.getTimeSeries().divide(100 * DAYS_PER_YEAR); DoubleTimeSeries<?>[] series = TimeSeriesIntersector.intersect(riskFreeReturnTS, assetReturnTS); riskFreeReturnTS = series[0]; assetReturnTS = series[1]; final TreynorRatioCalculator calculator = getCalculator(constraints.getValues(ValuePropertyNames.EXCESS_RETURN_CALCULATOR)); final double ratio = calculator.evaluate(assetReturnTS, riskFreeReturnTS, beta); final ValueProperties resultProperties = getResultProperties(desiredValues.iterator().next()); return Sets.newHashSet( new ComputedValue( new ValueSpecification( new ValueRequirement( ValueRequirementNames.TREYNOR_RATIO, positionOrNode, resultProperties), getUniqueId()), ratio)); }
@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Clock snapshotClock = executionContext.getValuationClock(); final ZonedDateTime now = snapshotClock.zonedDateTime(); final FinancialSecurity security = (FinancialSecurity) target.getSecurity(); final Currency payCurrency, receiveCurrency; if (security instanceof FXForwardSecurity) { final FXForwardSecurity forward = (FXForwardSecurity) security; payCurrency = forward.getPayCurrency(); receiveCurrency = forward.getReceiveCurrency(); } else { final NonDeliverableFXForwardSecurity ndf = (NonDeliverableFXForwardSecurity) security; payCurrency = ndf.getPayCurrency(); receiveCurrency = ndf.getReceiveCurrency(); } final ForexDefinition definition = (ForexDefinition) security.accept(VISITOR); final ValueRequirement desiredValue = desiredValues.iterator().next(); final String payCurveName = desiredValue.getConstraint(ValuePropertyNames.PAY_CURVE); final String receiveCurveName = desiredValue.getConstraint(ValuePropertyNames.RECEIVE_CURVE); final String payCurveConfig = desiredValue.getConstraint(PAY_CURVE_CALC_CONFIG); final String receiveCurveConfig = desiredValue.getConstraint(RECEIVE_CURVE_CALC_CONFIG); final String fullPutCurveName = payCurveName + "_" + payCurrency.getCode(); final String fullCallCurveName = receiveCurveName + "_" + receiveCurrency.getCode(); final YieldAndDiscountCurve payFundingCurve = getCurve(inputs, payCurrency, payCurveName, payCurveConfig); final YieldAndDiscountCurve receiveFundingCurve = getCurve(inputs, receiveCurrency, receiveCurveName, receiveCurveConfig); final YieldAndDiscountCurve[] curves; final Map<String, Currency> curveCurrency = new HashMap<String, Currency>(); curveCurrency.put(fullPutCurveName, payCurrency); curveCurrency.put(fullCallCurveName, receiveCurrency); final String[] allCurveNames; if (FXUtils.isInBaseQuoteOrder( payCurrency, receiveCurrency)) { // To get Base/quote in market standard order. curves = new YieldAndDiscountCurve[] {payFundingCurve, receiveFundingCurve}; allCurveNames = new String[] {fullPutCurveName, fullCallCurveName}; } else { curves = new YieldAndDiscountCurve[] {receiveFundingCurve, payFundingCurve}; allCurveNames = new String[] {fullCallCurveName, fullPutCurveName}; } final YieldCurveBundle yieldCurves = new YieldCurveBundle(allCurveNames, curves); final ValueProperties.Builder properties = getResultProperties( payCurveName, receiveCurveName, payCurveConfig, receiveCurveConfig, target); final ValueSpecification spec = new ValueSpecification( ValueRequirementNames.VALUE_THETA, target.toSpecification(), properties.get()); final ConstantSpreadHorizonThetaCalculator calculator = ConstantSpreadHorizonThetaCalculator.getInstance(); final MultipleCurrencyAmount theta = calculator.getTheta(definition, now, allCurveNames, yieldCurves, DAYS_TO_MOVE_FORWARD); return Collections.singleton(new ComputedValue(spec, theta)); }
@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final FinancialSecurity security = (FinancialSecurity) target.getSecurity(); final Currency currency = FinancialSecurityUtils.getCurrency(security); final Clock snapshotClock = executionContext.getValuationClock(); final ZonedDateTime now = snapshotClock.zonedDateTime(); final HistoricalTimeSeriesBundle timeSeries = HistoricalTimeSeriesFunctionUtils.getHistoricalTimeSeriesInputs(executionContext, inputs); final ValueRequirement desiredValue = desiredValues.iterator().next(); final String curveCalculationConfigName = desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG); final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext); final ConfigDBCurveCalculationConfigSource curveCalculationConfigSource = new ConfigDBCurveCalculationConfigSource(configSource); final MultiCurveCalculationConfig curveCalculationConfig = curveCalculationConfigSource.getConfig(curveCalculationConfigName); if (curveCalculationConfig == null) { throw new OpenGammaRuntimeException( "Could not find curve calculation configuration named " + curveCalculationConfigName); } final String[] curveNames = curveCalculationConfig.getYieldCurveNames(); final String[] yieldCurveNames = curveNames.length == 1 ? new String[] {curveNames[0], curveNames[0]} : curveNames; final String[] curveNamesForSecurity = FixedIncomeInstrumentCurveExposureHelper.getCurveNamesForSecurity( security, yieldCurveNames[0], yieldCurveNames[1]); final YieldCurveBundle bundle = YieldCurveFunctionUtils.getAllYieldCurves( inputs, curveCalculationConfig, curveCalculationConfigSource); final InstrumentDefinition<?> definition = security.accept(_visitor); if (definition == null) { throw new OpenGammaRuntimeException("Definition for security " + security + " was null"); } final InstrumentDerivative derivative = getDerivative( security, now, timeSeries, curveNamesForSecurity, definition, _definitionConverter); return getComputedValues( derivative, bundle, security, target, curveCalculationConfigName, currency.getCode()); }
public OffsetDateTimeSpinnerModel(PeriodProvider provider) { this(Clock.systemDefaultZone().offsetDateTime(), provider); }
public OffsetDateTimeSpinnerModel() { this(Clock.systemDefaultZone().offsetDateTime()); }
@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Clock snapshotClock = executionContext.getValuationClock(); final ZonedDateTime now = snapshotClock.zonedDateTime(); final FinancialSecurity security = (FinancialSecurity) target.getSecurity(); final InstrumentDefinition<InstrumentDerivative> definition = (InstrumentDefinition<InstrumentDerivative>) security.accept(VISITOR); final Currency putCurrency = security.accept(ForexVisitors.getPutCurrencyVisitor()); final Currency callCurrency = security.accept(ForexVisitors.getCallCurrencyVisitor()); final ValueRequirement desiredValue = desiredValues.iterator().next(); final String putCurveName = desiredValue.getConstraint(PROPERTY_PUT_CURVE); final String callCurveName = desiredValue.getConstraint(PROPERTY_CALL_CURVE); final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE); final String putForwardCurveName = desiredValue.getConstraint(PROPERTY_PUT_FORWARD_CURVE); final String callForwardCurveName = desiredValue.getConstraint(PROPERTY_CALL_FORWARD_CURVE); final String putCurveCalculationMethod = desiredValue.getConstraint(PROPERTY_PUT_CURVE_CALCULATION_METHOD); final String callCurveCalculationMethod = desiredValue.getConstraint(PROPERTY_CALL_CURVE_CALCULATION_METHOD); final String interpolatorName = desiredValue.getConstraint(InterpolatedDataProperties.X_INTERPOLATOR_NAME); final String leftExtrapolatorName = desiredValue.getConstraint(InterpolatedDataProperties.LEFT_X_EXTRAPOLATOR_NAME); final String rightExtrapolatorName = desiredValue.getConstraint(InterpolatedDataProperties.RIGHT_X_EXTRAPOLATOR_NAME); final String spread = desiredValue.getConstraint(PROPERTY_CALL_SPREAD_VALUE); final double spreadValue = Double.parseDouble(spread); final String fullPutCurveName = putCurveName + "_" + putCurrency.getCode(); final String fullCallCurveName = callCurveName + "_" + callCurrency.getCode(); final String[] curveNames; if (FXUtils.isInBaseQuoteOrder( putCurrency, callCurrency)) { // To get Base/quote in market standard order. curveNames = new String[] {fullPutCurveName, fullCallCurveName}; } else { curveNames = new String[] {fullCallCurveName, fullPutCurveName}; } final YieldAndDiscountCurve putFundingCurve = getCurve(inputs, putCurrency, putCurveName); final YieldAndDiscountCurve callFundingCurve = getCurve(inputs, callCurrency, callCurveName); final YieldAndDiscountCurve[] curves; final Map<String, Currency> curveCurrency = new HashMap<String, Currency>(); curveCurrency.put(fullPutCurveName, putCurrency); curveCurrency.put(fullCallCurveName, callCurrency); final String[] allCurveNames; final Currency ccy1; final Currency ccy2; if (FXUtils.isInBaseQuoteOrder( putCurrency, callCurrency)) { // To get Base/quote in market standard order. ccy1 = putCurrency; ccy2 = callCurrency; curves = new YieldAndDiscountCurve[] {putFundingCurve, callFundingCurve}; allCurveNames = new String[] {fullPutCurveName, fullCallCurveName}; } else { curves = new YieldAndDiscountCurve[] {callFundingCurve, putFundingCurve}; allCurveNames = new String[] {fullCallCurveName, fullPutCurveName}; ccy1 = callCurrency; ccy2 = putCurrency; } final InstrumentDerivative fxOption = definition.toDerivative(now, curveNames); final YieldCurveBundle yieldCurves = new YieldCurveBundle(allCurveNames, curves); final Object spotObject = inputs.getValue(ValueRequirementNames.SPOT_RATE); if (spotObject == null) { throw new OpenGammaRuntimeException("Could not get spot rate"); } final double spot = (Double) spotObject; final ValueRequirement fxVolatilitySurfaceRequirement = getSurfaceRequirement( surfaceName, putCurrency, callCurrency, interpolatorName, leftExtrapolatorName, rightExtrapolatorName); final Object volatilitySurfaceObject = inputs.getValue(fxVolatilitySurfaceRequirement); if (volatilitySurfaceObject == null) { throw new OpenGammaRuntimeException("Could not get " + fxVolatilitySurfaceRequirement); } final SmileDeltaTermStructureParametersStrikeInterpolation smiles = (SmileDeltaTermStructureParametersStrikeInterpolation) volatilitySurfaceObject; final FXMatrix fxMatrix = new FXMatrix(ccy1, ccy2, spot); final ValueProperties.Builder properties = getResultProperties( putCurveName, putForwardCurveName, putCurveCalculationMethod, callCurveName, callForwardCurveName, callCurveCalculationMethod, surfaceName, spread, interpolatorName, leftExtrapolatorName, rightExtrapolatorName, target); final ValueSpecification spec = new ValueSpecification(_valueRequirementName, target.toSpecification(), properties.get()); final YieldCurveBundle curvesWithFX = new YieldCurveBundle(fxMatrix, curveCurrency, yieldCurves.getCurvesMap()); final SmileDeltaTermStructureDataBundle smileBundle = new SmileDeltaTermStructureDataBundle(curvesWithFX, smiles, Pair.of(ccy1, ccy2)); return getResult(fxOption, spreadValue, smileBundle, spec); }
@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Clock snapshotClock = executionContext.getValuationClock(); final ZonedDateTime now = snapshotClock.zonedDateTime(); final FinancialSecurity security = (FinancialSecurity) target.getSecurity(); final Currency putCurrency = security.accept(ForexVisitors.getPutCurrencyVisitor()); final Currency callCurrency = security.accept(ForexVisitors.getCallCurrencyVisitor()); final ValueRequirement desiredValue = desiredValues.iterator().next(); final String putCurveName = desiredValue.getConstraint(PUT_CURVE); final String callCurveName = desiredValue.getConstraint(CALL_CURVE); final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE); final String putCurveConfig = desiredValue.getConstraint(PUT_CURVE_CALC_CONFIG); final String callCurveConfig = desiredValue.getConstraint(CALL_CURVE_CALC_CONFIG); final String interpolatorName = desiredValue.getConstraint(InterpolatedDataProperties.X_INTERPOLATOR_NAME); final String leftExtrapolatorName = desiredValue.getConstraint(InterpolatedDataProperties.LEFT_X_EXTRAPOLATOR_NAME); final String rightExtrapolatorName = desiredValue.getConstraint(InterpolatedDataProperties.RIGHT_X_EXTRAPOLATOR_NAME); final String daysForward = desiredValue.getConstraint(PROPERTY_DAYS_TO_MOVE_FORWARD); final String fullPutCurveName = putCurveName + "_" + putCurrency.getCode(); final String fullCallCurveName = callCurveName + "_" + callCurrency.getCode(); final YieldAndDiscountCurve putFundingCurve = FXOptionFunctionUtils.getCurveForCurrency( inputs, putCurrency, putCurveName, putCurveConfig); final YieldAndDiscountCurve callFundingCurve = FXOptionFunctionUtils.getCurveForCurrency( inputs, callCurrency, callCurveName, callCurveConfig); final YieldAndDiscountCurve[] curves; final Map<String, Currency> curveCurrency = new HashMap<String, Currency>(); curveCurrency.put(fullPutCurveName, putCurrency); curveCurrency.put(fullCallCurveName, callCurrency); final Object baseQuotePairsObject = inputs.getValue(ValueRequirementNames.CURRENCY_PAIRS); if (baseQuotePairsObject == null) { throw new OpenGammaRuntimeException("Could not get base/quote pair data"); } final CurrencyPairs baseQuotePairs = (CurrencyPairs) baseQuotePairsObject; final CurrencyPair baseQuotePair = baseQuotePairs.getCurrencyPair(putCurrency, callCurrency); if (baseQuotePair == null) { throw new OpenGammaRuntimeException( "Could not get base/quote pair for currency pair (" + putCurrency + ", " + callCurrency + ")"); } final String[] allCurveNames; final Currency ccy1; final Currency ccy2; if (baseQuotePair .getBase() .equals(putCurrency)) { // To get Base/quote in market standard order. ccy1 = putCurrency; ccy2 = callCurrency; curves = new YieldAndDiscountCurve[] {putFundingCurve, callFundingCurve}; allCurveNames = new String[] {fullPutCurveName, fullCallCurveName}; } else { curves = new YieldAndDiscountCurve[] {callFundingCurve, putFundingCurve}; allCurveNames = new String[] {fullCallCurveName, fullPutCurveName}; ccy1 = callCurrency; ccy2 = putCurrency; } final YieldCurveBundle yieldCurves = new YieldCurveBundle(allCurveNames, curves); final Object spotObject = inputs.getValue(ValueRequirementNames.SPOT_RATE); if (spotObject == null) { throw new OpenGammaRuntimeException("Could not get spot requirement"); } final double spot = (Double) spotObject; final ValueRequirement fxVolatilitySurfaceRequirement = getSurfaceRequirement( surfaceName, putCurrency, callCurrency, interpolatorName, leftExtrapolatorName, rightExtrapolatorName); final Object volatilitySurfaceObject = inputs.getValue(fxVolatilitySurfaceRequirement); if (volatilitySurfaceObject == null) { throw new OpenGammaRuntimeException("Could not get " + fxVolatilitySurfaceRequirement); } final SmileDeltaTermStructureParametersStrikeInterpolation smiles = (SmileDeltaTermStructureParametersStrikeInterpolation) volatilitySurfaceObject; final FXMatrix fxMatrix = new FXMatrix(ccy1, ccy2, spot); final ValueProperties.Builder properties = getResultProperties(target, desiredValue, baseQuotePair); final ValueSpecification spec = new ValueSpecification( ValueRequirementNames.VALUE_THETA, target.toSpecification(), properties.get()); final YieldCurveBundle curvesWithFX = new YieldCurveBundle(fxMatrix, curveCurrency, yieldCurves.getCurvesMap()); final SmileDeltaTermStructureDataBundle smileBundle = new SmileDeltaTermStructureDataBundle(curvesWithFX, smiles, Pair.of(ccy1, ccy2)); final ForexSecurityConverter converter = new ForexSecurityConverter(baseQuotePairs); final ForexOptionVanillaDefinition definition = (ForexOptionVanillaDefinition) security.accept(converter); final MultipleCurrencyAmount theta = CALCULATOR.getTheta( definition, now, allCurveNames, smileBundle, Integer.parseInt(daysForward)); return Collections.singleton(new ComputedValue(spec, HorizonUtils.getNonZeroValue(theta))); }
@Override public Set<ComputedValue> execute( final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Clock snapshotClock = executionContext.getValuationClock(); final ZonedDateTime now = snapshotClock.zonedDateTime(); final SecuritySource securitySource = OpenGammaExecutionContext.getSecuritySource(executionContext); final SwaptionSecurity security = (SwaptionSecurity) target.getSecurity(); final ValueRequirement desiredValue = desiredValues.iterator().next(); final Currency currency = FinancialSecurityUtils.getCurrency(security); final String forwardCurveName = desiredValue.getConstraint(YieldCurveFunction.PROPERTY_FORWARD_CURVE); final String fundingCurveName = desiredValue.getConstraint(YieldCurveFunction.PROPERTY_FUNDING_CURVE); final String curveCalculationMethod = desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_METHOD); final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE); final Object forwardCurveObject = inputs.getValue( YieldCurveFunction.getCurveRequirement( currency, forwardCurveName, forwardCurveName, fundingCurveName, curveCalculationMethod)); if (forwardCurveObject == null) { throw new OpenGammaRuntimeException("Could not get forward curve"); } final Object fundingCurveObject = inputs.getValue( YieldCurveFunction.getCurveRequirement( currency, fundingCurveName, forwardCurveName, fundingCurveName, curveCalculationMethod)); if (fundingCurveObject == null) { throw new OpenGammaRuntimeException("Could not get funding curve"); } final Object volatilitySurfaceObject = inputs.getValue(getVolatilityRequirement(surfaceName, currency)); if (volatilitySurfaceObject == null) { throw new OpenGammaRuntimeException("Could not get volatility surface"); } final VolatilitySurface volatilitySurface = (VolatilitySurface) volatilitySurfaceObject; if (!(volatilitySurface.getSurface() instanceof InterpolatedDoublesSurface)) { throw new OpenGammaRuntimeException( "Expecting an InterpolatedDoublesSurface; got " + volatilitySurface.getSurface().getClass()); } final YieldAndDiscountCurve forwardCurve = (YieldAndDiscountCurve) forwardCurveObject; final YieldAndDiscountCurve fundingCurve = (YieldAndDiscountCurve) fundingCurveObject; final InstrumentDefinition<?> definition = security.accept(_visitor); final InstrumentDerivative swaption = definition.toDerivative(now, new String[] {fundingCurveName, forwardCurveName}); final ValueProperties properties = getResultProperties( currency.getCode(), forwardCurveName, fundingCurveName, curveCalculationMethod, surfaceName); final ValueSpecification spec = new ValueSpecification(_valueRequirementName, target.toSpecification(), properties); final YieldCurveBundle curves = new YieldCurveBundle( new String[] {fundingCurveName, forwardCurveName}, new YieldAndDiscountCurve[] {fundingCurve, forwardCurve}); final BlackSwaptionParameters parameters = new BlackSwaptionParameters( volatilitySurface.getSurface(), SwaptionUtils.getSwapGenerator(security, definition, securitySource)); final YieldCurveWithBlackSwaptionBundle data = new YieldCurveWithBlackSwaptionBundle(parameters, curves); return getResult(swaption, data, spec); }