com.opengamma.financial.analytics.model.pnl.FXForwardYieldCurvePnLFunction.java Source code

Java tutorial

Introduction

Here is the source code for com.opengamma.financial.analytics.model.pnl.FXForwardYieldCurvePnLFunction.java

Source

/**
 * Copyright (C) 2013 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.model.pnl;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.opengamma.engine.ComputationTarget;
import com.opengamma.engine.function.AbstractFunction;
import com.opengamma.engine.function.FunctionCompilationContext;
import com.opengamma.engine.function.FunctionExecutionContext;
import com.opengamma.engine.function.FunctionInputs;
import com.opengamma.engine.target.ComputationTargetType;
import com.opengamma.engine.value.ComputedValue;
import com.opengamma.engine.value.ValueProperties;
import com.opengamma.engine.value.ValuePropertyNames;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.engine.value.ValueSpecification;
import com.opengamma.financial.analytics.TenorLabelledLocalDateDoubleTimeSeriesMatrix1D;
import com.opengamma.timeseries.date.localdate.LocalDateDoubleTimeSeries;
import com.opengamma.util.async.AsynchronousExecution;

/**
 * Produces the aggregated P&L series for a curve for an FX Forward.
 */
public class FXForwardYieldCurvePnLFunction extends AbstractFunction.NonCompiledInvoker {

    @Override
    public ComputationTargetType getTargetType() {
        return ComputationTargetType.POSITION;
    }

    @Override
    public Set<ValueSpecification> getResults(FunctionCompilationContext context, ComputationTarget target) {
        return ImmutableSet.of(new ValueSpecification(ValueRequirementNames.PNL_SERIES, target.toSpecification(),
                ValueProperties.all()));
    }

    @Override
    public Set<ValueRequirement> getRequirements(FunctionCompilationContext context, ComputationTarget target,
            ValueRequirement desiredValue) {
        return ImmutableSet.of(new ValueRequirement(ValueRequirementNames.YIELD_CURVE_PNL_SERIES,
                target.toSpecification(), desiredValue.getConstraints()));
    }

    @Override
    public Set<ValueSpecification> getResults(FunctionCompilationContext context, ComputationTarget target,
            Map<ValueSpecification, ValueRequirement> inputs) {
        ValueProperties properties = inputs.entrySet().iterator().next().getKey().getProperties().copy()
                .withoutAny(ValuePropertyNames.FUNCTION).with(ValuePropertyNames.FUNCTION, getUniqueId()).get();
        return ImmutableSet
                .of(new ValueSpecification(ValueRequirementNames.PNL_SERIES, target.toSpecification(), properties));
    }

    @Override
    public Set<ComputedValue> execute(FunctionExecutionContext executionContext, FunctionInputs inputs,
            ComputationTarget target, Set<ValueRequirement> desiredValues) throws AsynchronousExecution {
        ValueRequirement desiredValue = desiredValues.iterator().next();
        TenorLabelledLocalDateDoubleTimeSeriesMatrix1D nodalPnlSeries = (TenorLabelledLocalDateDoubleTimeSeriesMatrix1D) inputs
                .getValue(ValueRequirementNames.YIELD_CURVE_PNL_SERIES);
        LocalDateDoubleTimeSeries result = nodalPnlSeries.getValues()[0];
        for (int i = 1; i < nodalPnlSeries.size(); i++) {
            result = result.add(nodalPnlSeries.getValues()[i]);
        }
        ValueSpecification resultSpec = new ValueSpecification(ValueRequirementNames.PNL_SERIES,
                target.toSpecification(), desiredValue.getConstraints());
        return ImmutableSet.of(new ComputedValue(resultSpec, result));
    }

}