Created
December 13, 2013 13:34
-
-
Save febintt/7944280 to your computer and use it in GitHub Desktop.
Market value of a portfolio from Historical CLOSE value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies | |
* | |
* Please see distribution for license. | |
*/ | |
package com.opengamma.algotree.function; | |
import java.util.Collections; | |
import java.util.Set; | |
import org.threeten.bp.Period; | |
import com.opengamma.core.historicaltimeseries.HistoricalTimeSeries; | |
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.ValueRequirement; | |
import com.opengamma.engine.value.ValueSpecification; | |
import com.opengamma.financial.OpenGammaCompilationContext; | |
import com.opengamma.financial.analytics.timeseries.DateConstraint; | |
import com.opengamma.financial.analytics.timeseries.HistoricalTimeSeriesFunctionUtils; | |
import com.opengamma.master.historicaltimeseries.HistoricalTimeSeriesResolutionResult; | |
import com.opengamma.master.historicaltimeseries.HistoricalTimeSeriesResolver; | |
/** | |
* | |
*/ | |
public class MarketValueFromLastHistoricalValueFunction extends | |
AbstractFunction.NonCompiledInvoker { | |
/** Value name */ | |
public static final String VALUE_NAME = "CLOSE"; | |
/** Name of the result.*/ | |
public static final String MARKET_VALUE_NAME = "MARKET_VALUE_FROM_CLOSE"; | |
@Override | |
public boolean canApplyTo(final FunctionCompilationContext context, | |
final ComputationTarget target) { | |
return true; | |
} | |
@Override | |
public String getShortName() { | |
return "Market value from LastHistoricalValue for " + MARKET_VALUE_NAME; | |
} | |
@Override | |
public ComputationTargetType getTargetType() { | |
return ComputationTargetType.POSITION_OR_TRADE; | |
} | |
@Override | |
public Set<ValueRequirement> getRequirements( | |
final FunctionCompilationContext context, final ComputationTarget target, | |
final ValueRequirement desiredValue) { | |
final HistoricalTimeSeriesResolver resolver = OpenGammaCompilationContext | |
.getHistoricalTimeSeriesResolver(context); | |
final String fieldName = VALUE_NAME; | |
final HistoricalTimeSeriesResolutionResult timeSeries = resolver.resolve( | |
target.getPositionOrTrade().getSecurity().getExternalIdBundle(), null, null, null, | |
fieldName, null); | |
if (timeSeries == null) { | |
return null; | |
} | |
return Collections.singleton(HistoricalTimeSeriesFunctionUtils | |
.createHTSRequirement(timeSeries, fieldName, null, true, | |
DateConstraint.VALUATION_TIME.minus(Period.ofDays(1)), true)); | |
} | |
@Override | |
public Set<ValueSpecification> getResults( | |
final FunctionCompilationContext context, final ComputationTarget target) { | |
return Collections.singleton(new ValueSpecification(MARKET_VALUE_NAME, | |
target.toSpecification(), createValueProperties().get())); | |
} | |
@Override | |
public Set<ComputedValue> execute( | |
final FunctionExecutionContext executionContext, | |
final FunctionInputs inputs, final ComputationTarget target, | |
final Set<ValueRequirement> desiredValues) { | |
final HistoricalTimeSeries hts = (HistoricalTimeSeries) inputs | |
.getAllValues().iterator().next().getValue(); | |
final ValueRequirement desiredValue = desiredValues.iterator().next(); | |
return Collections.singleton(new ComputedValue(new ValueSpecification( | |
desiredValue.getValueName(), target.toSpecification(), desiredValue | |
.getConstraints()), hts.getTimeSeries().getLatestValue() * target.getPositionOrTrade().getQuantity().doubleValue())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment