Skip to content

Commit

Permalink
Merge pull request #86 from finmath/feature/627_new_endpoint_for_eval…
Browse files Browse the repository at this point in the history
…uationtimes

SDC-627, Create new endpoint to enable product valuation at evaluationtimes different from marketdatatime
  • Loading branch information
Julius278 authored Sep 2, 2024
2 parents 3429d5c + e830782 commit af91da3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ public ValueResult getValue(String marketData, String productData) throws Except
return new ValueResult().value(BigDecimal.valueOf(value)).currency(currency).valuationDate(valuationDate.toString());
}

public ValueResult getValueAtEvaluationTime(String marketData, String productData, LocalDateTime evaluationTime) throws Exception {
SmartDerivativeContractDescriptor productDescriptor = SDCXMLParser.parse(productData);

String ownerPartyID = productDescriptor.getUnderlyingReceiverPartyID();
InterestRateSwapProductDescriptor underlying = (InterestRateSwapProductDescriptor) new FPMLParser(ownerPartyID, FORWARD_EUR_6M, DISCOUNT_EUR_OIS).getProductDescriptor(productDescriptor.getUnderlying());


CalibrationDataset set = CalibrationParserDataItems.getCalibrationDataSetFromXML(marketData,productDescriptor.getMarketdataItemList());
double value = calculateValueAtTime(List.of(set),evaluationTime,set.getDate(),productDescriptor,underlying);

String currency = "EUR";

return new ValueResult().value(BigDecimal.valueOf(value)).currency(currency).valuationDate(evaluationTime.toString());
}

public ValueResult getValue(MarketDataList marketData, String productData) throws Exception {
SmartDerivativeContractDescriptor productDescriptor = SDCXMLParser.parse(productData);

Expand Down Expand Up @@ -217,4 +232,33 @@ private double calculateMargin(List<CalibrationDataset> marketDataList, LocalDat

return marginCall;
}

/**
* Calculates values at different valuation times
*
* @param marketDataList list of market data scenarios.
* @param evaluationTime the evaluation time
* @param marketDataTime the marketdata time
* @param productDescriptor The product descriptor (wrapper to the product XML)
* @param underlying The underlying descriptor (wrapper to the underlying XML)
*/
private double calculateValueAtTime(List<CalibrationDataset> marketDataList, LocalDateTime evaluationTime, LocalDateTime marketDataTime, SmartDerivativeContractDescriptor productDescriptor, InterestRateSwapProductDescriptor underlying) {

// Build product
LocalDate referenceDate = productDescriptor.getTradeDate().toLocalDate();
InterestRateSwapLegProductDescriptor legReceiver = (InterestRateSwapLegProductDescriptor) underlying.getLegReceiver();
InterestRateSwapLegProductDescriptor legPayer = (InterestRateSwapLegProductDescriptor) underlying.getLegPayer();
InterestRateAnalyticProductFactory productFactory = new InterestRateAnalyticProductFactory(referenceDate);
DescribedProduct<? extends ProductDescriptor> legReceiverProduct = productFactory.getProductFromDescriptor(legReceiver);
DescribedProduct<? extends ProductDescriptor> legPayerProduct = productFactory.getProductFromDescriptor(legPayer);

Swap swap = new Swap((SwapLeg) legReceiverProduct, (SwapLeg) legPayerProduct);

// Build valuation oracle with given market data.
final ValuationOraclePlainSwap oracle = new ValuationOraclePlainSwap(swap, 1.0, marketDataList);

double value = oracle.getValue(evaluationTime,marketDataTime);

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.DoubleUnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -101,5 +103,4 @@ public Double getValue(final LocalDateTime evaluationDate, final LocalDateTime m
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.xml.sax.SAXException;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Controller for the settlement valuation REST service.
Expand Down Expand Up @@ -127,6 +129,25 @@ public ResponseEntity<ValueResult> value(ValueRequest valueRequest) {
}
}

@Override
public ResponseEntity<ValueResult> valueAtTime(ValueRequest valueRequest) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add(RESPONDED, "valueAtTime");
ValueResult value = null;
try {
MarginCalculator marginCalculator = new MarginCalculator();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss");
LocalDateTime evaluationTime = LocalDateTime.parse(valueRequest.getValuationDate(), formatter);
value = marginCalculator.getValueAtEvaluationTime(valueRequest.getMarketData(), valueRequest.getTradeData(),evaluationTime);
logger.info(value.toString());
return ResponseEntity.ok(value);
} catch (Exception e) {
logger.error(FAILED_CALCULATION, e);
throw new SDCException(ExceptionId.SDC_MARGIN_CALCULATION_ERROR, e.getMessage());
}
}


@Override
public ResponseEntity<ValueResult> testProductValue(MultipartFile tradeData) {
HttpHeaders responseHeaders = new HttpHeaders();
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ paths:
schema:
$ref: "#/components/schemas/Error"

/valuation/valueAtTime:
post:
summary: Request mapping for the value at specific evaluationTime
operationId: valueAtTime
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/ValueRequest"
responses:
"200":
description: Value was calculated
content:
application/json:
schema:
$ref: "#/components/schemas/ValueResult"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

/valuation/margin:
post:
summary: Request mapping for the value
Expand Down

0 comments on commit af91da3

Please sign in to comment.