BloombergEQVanillaOptionChain { public BloombergEQVanillaOptionChain narrowByStrike(final double referencePrice, int strikeOffset) { List<BloombergTickerParserEQVanillaOption> parsers = new ArrayList<BloombergTickerParserEQVanillaOption>(_identifiers.size()); NavigableSet<Double> strikes = new TreeSet<Double>(); for (ExternalId identifier : _identifiers) { BloombergTickerParserEQVanillaOption parser = new BloombergTickerParserEQVanillaOption(identifier); parsers.add(parser); Double strike = parser.getStrike(); strikes.add(strike); } Double targetValue = null; if (strikeOffset == 0) { double floorValue = Objects.firstNonNull(strikes.floor(referencePrice), strikes.first()); targetValue = floorValue; if (floorValue != referencePrice) { double ceilingValue = Objects.firstNonNull(strikes.ceiling(referencePrice), strikes.last()); double diff1 = Math.abs(referencePrice - floorValue); double diff2 = Math.abs(referencePrice - ceilingValue); if (diff1 < diff2) { targetValue = floorValue; } else { targetValue = ceilingValue; } } } else { targetValue = referencePrice; while (strikeOffset != 0) { Double next = null; if (strikeOffset > 0) { next = strikes.higher(targetValue); if (next != null) { targetValue = next; strikeOffset--; } else { break; } } else { next = strikes.lower(targetValue); if (next != null) { targetValue = next; strikeOffset++; } else { break; } } } } List<ExternalId> identifiers = new ArrayList<ExternalId>(); for (BloombergTickerParserEQVanillaOption parser : parsers) { if (parser.getStrike() == targetValue) { identifiers.add(parser.getIdentifier()); } } return new BloombergEQVanillaOptionChain(identifiers); } BloombergEQVanillaOptionChain(Set<ExternalId> identifiers);  BloombergEQVanillaOptionChain(List<ExternalId> identifiers);  BloombergEQVanillaOptionChain(BloombergEQVanillaOptionChain original); BloombergEQVanillaOptionChain narrowByOptionType(final OptionType optionType); BloombergEQVanillaOptionChain narrowByExpiry(int monthsFromToday); BloombergEQVanillaOptionChain narrowByExpiry(LocalDate referenceDate, final int monthsFromReferenceDate); BloombergEQVanillaOptionChain narrowByStrike(final double referencePrice, int strikeOffset); NavigableSet<ExternalId> getIdentifiers();  }
@Test(dependsOnMethods="preTest") public void testStrike_0_On () { double referencePrice = 140; int offset = 0; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(6, referencePrice, offset, chain); assertStrike(140, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Plus_1 () { double referencePrice = 141; int offset = 1; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(6, referencePrice, offset, chain); assertStrike(145, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Plus_2 () { double referencePrice = 141; int offset = 2; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(6, referencePrice, offset, chain); assertStrike(150, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Plus_5 () { double referencePrice = 141; int offset = 5; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(10, referencePrice, offset, chain); assertStrike(165, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Plus_100 () { double referencePrice = 141; int offset = 100; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(10, referencePrice, offset, chain); assertStrike(540, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Plus_1_Interval () { int offset = 1; double referencePrice = 530; for (int i = 1 ; i <= 4 ; i++) { referencePrice = referencePrice + 1.0; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(4, referencePrice, offset, chain); assertStrike(535, referencePrice, offset, chain); } }
@Test(dependsOnMethods="preTest") public void testStrike_Minus_1 () { double referencePrice = 141; int offset = -1; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(6, referencePrice, offset, chain); assertStrike(140, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Minus_2 () { double referencePrice = 141; int offset = -2; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(6, referencePrice, offset, chain); assertStrike(135, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Minus_5 () { double referencePrice = 141; int offset = -5; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(4, referencePrice, offset, chain); assertStrike(120, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Minus_100 () { double referencePrice = 141; int offset = -100; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(2, referencePrice, offset, chain); assertStrike(100, referencePrice, offset, chain); }
@Test(dependsOnMethods="preTest") public void testStrike_Minus_1_Interval () { int offset = -1; double referencePrice = 531; for (int i = 1 ; i <= 4 ; i++) { referencePrice = referencePrice + 1.0; BloombergEQVanillaOptionChain chain = s_chain.narrowByStrike(referencePrice, offset); assertCount(12, referencePrice, offset, chain); assertStrike(530, referencePrice, offset, chain); } }