public Set<ComputedValue> execute(FunctionExecutionContext executionContext,
FunctionInputs inputs,
ComputationTarget target,
Set<ValueRequirement> desiredValues) throws AsynchronousExecution {
// 1. Unpack
final Trade trade = target.getTrade();
final Security security = trade.getSecurity();
final LocalDate tradeDate = trade.getTradeDate();
LocalDate valuationDate = ZonedDateTime.now(executionContext.getValuationClock()).toLocalDate();
final boolean isNewTrade = tradeDate.equals(valuationDate);
// Get desired TradeType: Open (traded before today), New (traded today) or All
final ValueRequirement desiredValue = desiredValues.iterator().next();
final String tradeType = desiredValue.getConstraint(PnLFunctionUtils.PNL_TRADE_TYPE_CONSTRAINT);
if (tradeType == null) {
s_logger.error("TradeType not set for: " + security.getName() +
". Choose one of {" + PnLFunctionUtils.PNL_TRADE_TYPE_OPEN + "," + PnLFunctionUtils.PNL_TRADE_TYPE_OPEN + "," + PnLFunctionUtils.PNL_TRADE_TYPE_ALL + "}");
}
// Create output specification. Check for trivial cases
final ValueSpecification valueSpecification = new ValueSpecification(getValueRequirementName(), target.toSpecification(), desiredValue.getConstraints());
if (isNewTrade && tradeType.equalsIgnoreCase(PnLFunctionUtils.PNL_TRADE_TYPE_OPEN) ||
(!isNewTrade) && tradeType.equalsIgnoreCase(PnLFunctionUtils.PNL_TRADE_TYPE_NEW)) {
return Sets.newHashSet(new ComputedValue(valueSpecification, 0.0));
}
// 2. Get inputs
// For all TradeTypes, we'll require the live Price
Double livePrice = calculateLivePrice(inputs, target);
// For PNL, we need a reference price. We have two cases:
// Open: will need the closing price and any carry
// New: will need the trade price
Double referencePrice;
Double costOfCarry = 0.0;
if (isNewTrade) {
referencePrice = trade.getPremium();
if (referencePrice == null) {
throw new NullPointerException("New Trades require a premium to compute PNL on trade date. Premium was null for " + trade.getUniqueId());
}
if ((security instanceof InterestRateFutureSecurity || security instanceof IRFutureOptionSecurity) && (trade.getPremium() > 1.0)) {
referencePrice /= 100.0;
}
} else {
referencePrice = calculateReferencePrice(inputs, target);
if (referencePrice == null) {