FixedStrikeLookbackOptionModel extends AnalyticOptionModel<FixedStrikeLookbackOptionDefinition, StandardOptionWithSpotTimeSeriesDataBundle> { @Override public Function1D<StandardOptionWithSpotTimeSeriesDataBundle, Double> getPricingFunction(final FixedStrikeLookbackOptionDefinition definition) { Validate.notNull(definition, "definition"); return new Function1D<StandardOptionWithSpotTimeSeriesDataBundle, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final StandardOptionWithSpotTimeSeriesDataBundle data) { Validate.notNull(data, "data"); final DoubleTimeSeries<?> ts = data.getSpotTimeSeries(); final double s = data.getSpot(); final ZonedDateTime date = data.getDate(); final double t = definition.getTimeToExpiry(date); final boolean isCall = definition.isCall(); final double k = definition.getStrike(); final double sCritical = isCall ? ts.maxValue() : ts.minValue(); final double sigma = data.getVolatility(t, k); final double r = data.getInterestRate(t); final double b = data.getCostOfCarry(); final double df1 = Math.exp(t * (b - r)); final double df2 = Math.exp(-r * t); double x = k; if ((isCall && x <= sCritical) || (!isCall && x >= sCritical)) { x = sCritical; } final int sign = isCall ? 1 : -1; final double d1 = getD1(s, x, t, sigma, b); final double d2 = getD2(d1, sigma, t); final double cdf1 = NORMAL.getCDF(sign * d1); return sign * (df2 * (x - k) + s * df1 * cdf1 - x * df2 * NORMAL.getCDF(sign * d2) - s * df2 * sigma * sigma * (Math.pow(s / x, -2 * b / sigma / sigma) * NORMAL.getCDF(sign * (d1 - 2 * b * Math.sqrt(t) / sigma)) - Math.exp(b * t) * cdf1) / 2 / b); } }; }  @Override Function1D<StandardOptionWithSpotTimeSeriesDataBundle, Double> getPricingFunction(final FixedStrikeLookbackOptionDefinition definition);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullDefinition() { MODEL.getPricingFunction(null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullData() { MODEL.getPricingFunction(new FixedStrikeLookbackOptionDefinition(100, EXPIRY, true)).evaluate((StandardOptionWithSpotTimeSeriesDataBundle) null); }
@Test public void test() { FixedStrikeLookbackOptionDefinition option = new FixedStrikeLookbackOptionDefinition(95, EXPIRY, false); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 4.4448, EPS); option = new FixedStrikeLookbackOptionDefinition(100, EXPIRY, false); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 8.3177, EPS); option = new FixedStrikeLookbackOptionDefinition(105, EXPIRY, false); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 13.0739, EPS); option = new FixedStrikeLookbackOptionDefinition(95, EXPIRY, true); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 18.9263, EPS); option = new FixedStrikeLookbackOptionDefinition(100, EXPIRY, true); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 14.1702, EPS); option = new FixedStrikeLookbackOptionDefinition(105, EXPIRY, true); assertEquals(MODEL.getPricingFunction(option).evaluate(DATA), 9.8905, EPS); }