Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDC-539, create initial/regular settlement with provided marketData #111

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class SettlementService {
private static final Logger logger = LoggerFactory.getLogger(SettlementService.class);
public static final String NEW_MARKET_DATA_STRING = "{} - newMarketDataString: {}";
public static final String SETTLEMENT_REQUEST_INFO = "{} - sdc trade id: {}, product marketdata provider: {}, valuation service marketdata provider: {}";

private final MarginCalculator marginCalculator = new MarginCalculator();

Expand All @@ -45,13 +51,24 @@ public SettlementService(RefinitivConfig refinitivConfig, ValuationConfig valuat
public RegularSettlementResult generateRegularSettlementResult(RegularSettlementRequest regularSettlementRequest) {
logger.info("Generating regular settlement result, liveData: {}, now parsing trade data", valuationConfig.isLiveMarketData());
SmartDerivativeContractDescriptor sdc = parseProductData(regularSettlementRequest.getTradeData());
logger.info("generateRegularSettlementResult - sdc trade id: {}, product marketdata provider: {}, valuation service marketdata provider: {}", sdc.getDltTradeId(), sdc.getMarketDataProvider(), valuationConfig.getLiveMarketDataProvider());
MarketDataList newMarketDataList = retrieveMarketData(sdc);
includeFixingsOfLastSettlement(regularSettlementRequest, newMarketDataList);
String newMarketDataString = SDCXMLParser.marshalClassToXMLString(newMarketDataList);
logger.info(SETTLEMENT_REQUEST_INFO, "generateRegularSettlementResult", sdc.getDltTradeId(), sdc.getMarketDataProvider(), valuationConfig.getLiveMarketDataProvider());

String newMarketDataString;

if (regularSettlementRequest.getNewProvidedMarketData() == null) {
MarketDataList newMarketDataList = retrieveMarketData(sdc);
includeFixingsOfLastSettlement(regularSettlementRequest, newMarketDataList);
newMarketDataString = SDCXMLParser.marshalClassToXMLString(newMarketDataList);
} else {
logger.info("provided custom marketData per string in regularSettlementRequest");
newMarketDataString = regularSettlementRequest.getNewProvidedMarketData();
checkMarketDataString(newMarketDataString);
logger.info("provided marketData in regularSettlementRequest passed the marketData check");
}
logger.info(NEW_MARKET_DATA_STRING, "generateRegularSettlementResult", newMarketDataString);

Settlement settlementLast = SDCXMLParser.unmarshalXml(regularSettlementRequest.getSettlementLast(), Settlement.class);
String marketDataLastString = SDCXMLParser.marshalClassToXMLString(settlementLast.getMarketData());
logger.info("generateRegularSettlementResult - newMarketDataString: {}", newMarketDataString);

// TODO Using now here is a bit strange in the unit test. Results will vary.
ZonedDateTime settlementTimeNext = ZonedDateTime.now().plusDays(1);
Expand Down Expand Up @@ -85,10 +102,19 @@ public RegularSettlementResult generateRegularSettlementResult(RegularSettlement
public InitialSettlementResult generateInitialSettlementResult(InitialSettlementRequest initialSettlementRequest) {
logger.info("Generating initial settlement result, liveData: {}, now parsing trade data", valuationConfig.isLiveMarketData());
SmartDerivativeContractDescriptor sdc = parseProductData(initialSettlementRequest.getTradeData());
logger.info("generateInitialSettlementResult- sdc trade id: {}, product marketdata provider: {}, valuation service marketdata provider: {}", sdc.getDltTradeId(), sdc.getMarketDataProvider(), valuationConfig.getLiveMarketDataProvider());
MarketDataList newMarketDataList = retrieveMarketData(sdc);
String newMarketDataString = SDCXMLParser.marshalClassToXMLString(newMarketDataList);
logger.debug("generateInitialSettlementResult- newMarketDataString: {}", newMarketDataString);
logger.info(SETTLEMENT_REQUEST_INFO, "generateInitialSettlementResult", sdc.getDltTradeId(), sdc.getMarketDataProvider(), valuationConfig.getLiveMarketDataProvider());
String newMarketDataString;

if (initialSettlementRequest.getNewProvidedMarketData() == null) {
MarketDataList newMarketDataList = retrieveMarketData(sdc);
newMarketDataString = SDCXMLParser.marshalClassToXMLString(newMarketDataList);
} else {
logger.info("provided custom marketData per string in initialSettlementRequest");
newMarketDataString = initialSettlementRequest.getNewProvidedMarketData();
checkMarketDataString(newMarketDataString);
logger.info("provided marketData in initialSettlementRequest passed the marketData check");
}
logger.info(NEW_MARKET_DATA_STRING, "generateInitialSettlementResult", newMarketDataString);

ZonedDateTime settlementTimeNext = ZonedDateTime.now().plusDays(1);

Expand All @@ -115,6 +141,21 @@ public InitialSettlementResult generateInitialSettlementResult(InitialSettlement
.valuationDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss")));
}

/**
* checks the format of a providedMarketDataString, if not valid it will throw an exception
* @param newMarketDataString XML marketDataList as string
*/
private void checkMarketDataString(String newMarketDataString) {
try {
SDCXMLParser.unmarshalXml(newMarketDataString, MarketDataList.class);
} catch (SDCException e){
logger.error("MarketDataString is invalid, error while unmarshalling string: " + newMarketDataString, e);
throw new SDCException(ExceptionId.SDC_XML_PARSE_ERROR, "provided MarketDataString is invalid, please check input", 400);
} catch (Exception e) {
logger.error("unknown error recognized while unmarshalling string: " + newMarketDataString, e);
throw e;
}
}

private static SmartDerivativeContractDescriptor parseProductData(String tradeData) {
try {
Expand Down Expand Up @@ -205,7 +246,7 @@ private void includeFixingsOfLastSettlement(RegularSettlementRequest regularSett
.findAny();
List<String> symbols;

if(symbolsOptional.isPresent()) {symbols = symbolsOptional.get().getSymbol();}
if (symbolsOptional.isPresent()) {symbols = symbolsOptional.get().getSymbol();}
else {
logger.warn("no Fixings found in SDC product data XML, marketDataList not changed");
return;
Expand Down
Loading