Skip to content

Commit

Permalink
PROD-7690: Add methods required for non vanilla FX options and refact…
Browse files Browse the repository at this point in the history
…or. (#2325)
  • Loading branch information
santorog authored Jul 8, 2021
1 parent 141ffb2 commit 3e02a39
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
package com.opengamma.strata.loader.csv;

import static com.opengamma.strata.collect.Guavate.toImmutableMap;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.BARRIER_LEVEL_FIELD;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.BARRIER_TYPE_FIELD;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.KNOCK_TYPE_FIELD;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.PREMIUM_AMOUNT_FIELD;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.PREMIUM_CURRENCY_FIELD;
import static com.opengamma.strata.loader.csv.CsvLoaderColumns.PREMIUM_DATE_CAL_FIELD;
Expand Down Expand Up @@ -42,6 +45,10 @@
import com.opengamma.strata.product.etd.EtdSettlementType;
import com.opengamma.strata.product.etd.EtdType;
import com.opengamma.strata.product.etd.EtdVariant;
import com.opengamma.strata.product.option.Barrier;
import com.opengamma.strata.product.option.BarrierType;
import com.opengamma.strata.product.option.KnockType;
import com.opengamma.strata.product.option.SimpleConstantContinuousBarrier;

/**
* CSV information resolver helper.
Expand Down Expand Up @@ -375,6 +382,38 @@ public static AdjustablePayment parseAdjustablePayment(
return AdjustablePayment.of(ccyAmount, adjustableDate);
}

/**
* Parses a barrier from the csv row.
*
* @param row the CSV row to parse
* @param barrierTypeField the barrier type field
* @param knockTypeField the knock type field
* @param barrierLevelField the barrier level field
* @return the barrier
*/
public static Barrier parseBarrier(
CsvRow row,
String barrierTypeField,
String knockTypeField,
String barrierLevelField) {

BarrierType barrierType = row.getValue(barrierTypeField, LoaderUtils::parseBarrierType);
KnockType knockType = row.getValue(knockTypeField, LoaderUtils::parseKnockType);
double barrierLevel = row.getValue(barrierLevelField, LoaderUtils::parseDouble);

return SimpleConstantContinuousBarrier.of(barrierType, knockType, barrierLevel);
}

/**
* Parses a barrier using the default barrier fields.
*
* @param row the CSV row to parse
* @return the barrier
*/
public static Barrier parseBarrierFromDefaultFields(CsvRow row) {
return parseBarrier(row, BARRIER_TYPE_FIELD, KNOCK_TYPE_FIELD, BARRIER_LEVEL_FIELD);
}

/**
* Parses the premium using the default premium fields.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,45 @@ private CsvWriterUtils() {
* @param premium the premium
*/
public static void writePremiumFields(CsvOutput.CsvRowOutputWithHeaders csv, AdjustablePayment premium) {
csv.writeCell(PREMIUM_DATE_FIELD, premium.getDate().getUnadjusted());
csv.writeCell(PREMIUM_DATE_CNV_FIELD, premium.getDate().getAdjustment().getConvention());
csv.writeCell(PREMIUM_DATE_CAL_FIELD, premium.getDate().getAdjustment().getCalendar());
csv.writeCell(PREMIUM_DIRECTION_FIELD, PayReceive.ofSignedAmount(premium.getAmount()));
csv.writeCell(PREMIUM_CURRENCY_FIELD, premium.getCurrency());
csv.writeCell(PREMIUM_AMOUNT_FIELD, premium.getAmount());
writeAdjustablePayment(
csv,
premium,
PREMIUM_AMOUNT_FIELD,
PREMIUM_CURRENCY_FIELD,
PREMIUM_DIRECTION_FIELD,
PREMIUM_DATE_FIELD,
PREMIUM_DATE_CNV_FIELD,
PREMIUM_DATE_CAL_FIELD);
}

/**
* Writes an AdjustablePayment object to CSV
*
* @param csv the csv row
* @param adjustablePayment the adjustable payment
* @param amountField the amount field
* @param currencyField the currency field
* @param directionField the direction field
* @param dateField the date field
* @param dateConventionField the date convention field
* @param dateCalendarField the date calendar field
*/
public static void writeAdjustablePayment(
CsvOutput.CsvRowOutputWithHeaders csv,
AdjustablePayment adjustablePayment,
String amountField,
String currencyField,
String directionField,
String dateField,
String dateConventionField,
String dateCalendarField) {

csv.writeCell(amountField, adjustablePayment.getAmount());
csv.writeCell(currencyField, adjustablePayment.getCurrency());
csv.writeCell(directionField, PayReceive.ofSignedAmount(adjustablePayment.getAmount()));
csv.writeCell(dateField, adjustablePayment.getDate().getUnadjusted());
csv.writeCell(dateConventionField, adjustablePayment.getDate().getAdjustment().getConvention());
csv.writeCell(dateCalendarField, adjustablePayment.getDate().getAdjustment().getCalendar());
}

/**
Expand Down Expand Up @@ -87,7 +120,7 @@ public static void writeCurrencyAmount(
* @param barrier the barrier
* @param obsDate the barrier observation date
*/
public static void writeBarrierFields(CsvOutput.CsvRowOutputWithHeaders csv, Barrier barrier, LocalDate obsDate) {
public static void writeBarrier(CsvOutput.CsvRowOutputWithHeaders csv, Barrier barrier, LocalDate obsDate) {
csv.writeCell(BARRIER_TYPE_FIELD, barrier.getBarrierType());
csv.writeCell(KNOCK_TYPE_FIELD, barrier.getKnockType());
csv.writeCell(BARRIER_LEVEL_FIELD, barrier.getBarrierLevel(obsDate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@
import com.opengamma.strata.basics.currency.AdjustablePayment;
import com.opengamma.strata.collect.io.CsvOutput;
import com.opengamma.strata.collect.io.CsvRow;
import com.opengamma.strata.loader.LoaderUtils;
import com.opengamma.strata.product.Trade;
import com.opengamma.strata.product.TradeInfo;
import com.opengamma.strata.product.fxopt.FxSingleBarrierOption;
import com.opengamma.strata.product.fxopt.FxSingleBarrierOptionTrade;
import com.opengamma.strata.product.fxopt.FxVanillaOptionTrade;
import com.opengamma.strata.product.option.BarrierType;
import com.opengamma.strata.product.option.KnockType;
import com.opengamma.strata.product.option.SimpleConstantContinuousBarrier;
import com.opengamma.strata.product.option.Barrier;

/**
* Handles the CSV files format for FX Single Barrier Option trades.
Expand Down Expand Up @@ -84,15 +81,12 @@ public Optional<Trade> parseTrade(

private FxSingleBarrierOptionTrade parse(CsvRow row, TradeInfo info, TradeCsvInfoResolver resolver) {
FxVanillaOptionTrade vanillaOptionTrade = resolver.parseFxVanillaOptionTrade(row, info);

BarrierType barrierType = row.getValue(BARRIER_TYPE_FIELD, LoaderUtils::parseBarrierType);
KnockType knockType = row.getValue(KNOCK_TYPE_FIELD, LoaderUtils::parseKnockType);
double barrierLevel = row.getValue(BARRIER_LEVEL_FIELD, LoaderUtils::parseDouble);
Barrier barrier = CsvLoaderUtils.parseBarrierFromDefaultFields(row);
AdjustablePayment premium = CsvLoaderUtils.parsePremiumFromDefaultFields(row);

FxSingleBarrierOption.Builder productBuilder = FxSingleBarrierOption.builder()
.underlyingOption(vanillaOptionTrade.getProduct())
.barrier(SimpleConstantContinuousBarrier.of(barrierType, knockType, barrierLevel));
.barrier(barrier);

CsvLoaderUtils.tryParseCurrencyAmountWithDirection(
row,
Expand Down Expand Up @@ -133,7 +127,7 @@ public void writeCsv(CsvOutput.CsvRowOutputWithHeaders csv, FxSingleBarrierOptio

void writeSingleBarrierOption(CsvOutput.CsvRowOutputWithHeaders csv, FxSingleBarrierOption product) {
CsvWriterUtils.writeFxVanillaOption(csv, product.getUnderlyingOption());
CsvWriterUtils.writeBarrierFields(csv, product.getBarrier(), LocalDate.now(ZoneId.systemDefault()));
CsvWriterUtils.writeBarrier(csv, product.getBarrier(), LocalDate.now(ZoneId.systemDefault()));
product.getRebate().ifPresent(ccyAmount -> CsvWriterUtils.writeCurrencyAmount(
csv,
ccyAmount,
Expand Down

0 comments on commit 3e02a39

Please sign in to comment.