final ConventionBundleSource conventionSource = OpenGammaExecutionContext.getConventionBundleSource(executionContext);
final RegionSource regionSource = OpenGammaExecutionContext.getRegionSource(executionContext);
final Clock snapshotClock = executionContext.getValuationClock();
final ZonedDateTime now = ZonedDateTime.now(snapshotClock);
final BondSecurityConverter converter = new BondSecurityConverter(holidaySource, conventionSource, regionSource);
final FinancialSecuritySource securitySource = executionContext.getSecuritySource(FinancialSecuritySource.class);
final Collection<Security> allBonds = new ArrayList<Security>(securitySource.getBondsWithIssuerName(ISSUER_NAME));
final Iterator<Security> iter = allBonds.iterator();
while (iter.hasNext()) {
final Security sec = iter.next();
if (sec instanceof BondSecurity) {
final BondSecurity bond = (BondSecurity) sec;
if (bond.getLastTradeDate().getExpiry().isBefore(now)) {
iter.remove();
}
s_logger.info(bond.getLastTradeDate().toString());
} else {
throw new OpenGammaRuntimeException("non-bond security " + sec + " returned by getAllBondsOfIssuerType()");
}
}
final int n = allBonds.size();
final double[] t = new double[n];
final double[] ytm = new double[n];
int i = 0;
for (final Security security : allBonds) {
final GovernmentBondSecurity bondSec = (GovernmentBondSecurity) security;
final Object ytmObject = inputs.getValue(new ValueRequirement(ValueRequirementNames.YTM, ComputationTargetType.SECURITY, security.getUniqueId()));
if (ytmObject == null) {
s_logger.warn("Could not get YTM for " + security.getUniqueId());
continue;
}
if (!(ytmObject instanceof Double)) {
throw new IllegalArgumentException("YTM should be a double");
}
final InstrumentDefinition<?> definition = converter.visitGovernmentBondSecurity(bondSec);
final String bondStringName = PROPERTY_PREFIX + "_" + CURRENCY.getCode();
final InstrumentDerivative bond = definition.toDerivative(now, bondStringName);
t[i] = bond.accept(LAST_DATE);
ytm[i++] = ((Double) ytmObject / 100);
}
final DoubleMatrix1D initialValues = new DoubleMatrix1D(new double[] {1, 2, 3, 4, 2, 3 });
final ParameterizedFunction<Double, DoubleMatrix1D, Double> parameterizedFunction = MODEL.getParameterizedFunction();
final LeastSquareResults result = MINIMISER.solve(new DoubleMatrix1D(t), new DoubleMatrix1D(ytm), parameterizedFunction, initialValues);
final DoubleMatrix1D parameters = result.getFitParameters();
final FunctionalDoublesCurve curve = FunctionalDoublesCurve.from(parameterizedFunction.asFunctionOfArguments(parameters));
final YieldCurve yieldCurve = YieldCurve.from(curve);
return Sets.newHashSet(new ComputedValue(_result, yieldCurve));
}
@Override
public ComputationTargetType getTargetType() {
return ComputationTargetType.CURRENCY;
}
@SuppressWarnings("synthetic-access")
@Override
public boolean canApplyTo(final FunctionCompilationContext context, final ComputationTarget target) {
return CURRENCY.equals(target.getValue());
}
@SuppressWarnings("synthetic-access")
@Override
public Set<ValueRequirement> getRequirements(final FunctionCompilationContext context, final ComputationTarget target, final ValueRequirement desiredValue) {
if (canApplyTo(context, target)) {
final FinancialSecuritySource securitySource = context.getSecuritySource(FinancialSecuritySource.class);
final Collection<Security> allBonds = new ArrayList<Security>(securitySource.getBondsWithIssuerName("US TREASURY N/B"));
final Iterator<Security> iter = allBonds.iterator();
while (iter.hasNext()) {
final Security sec = iter.next();
if (sec instanceof BondSecurity) {
final BondSecurity bond = (BondSecurity) sec;