Skip to content

Commit

Permalink
added "uniqueTradeIdentifierField", consolidated XMLs
Browse files Browse the repository at this point in the history
  • Loading branch information
pekola committed Oct 23, 2023
1 parent 83c8a7c commit 99a81ba
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 2,419 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void main(String[] args) throws Exception {

final String marketDataStart = new String(ValuationClient.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.client/md_testset1.json").readAllBytes(), StandardCharsets.UTF_8);
final String marketDataEnd = new String(ValuationClient.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.client/md_testset2.json").readAllBytes(), StandardCharsets.UTF_8);
final String product = new String(ValuationClient.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.client/smartderivativecontract-sample-swap.xml").readAllBytes(), StandardCharsets.UTF_8);
final String product = new String(ValuationClient.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/smartderivativecontract.xml").readAllBytes(), StandardCharsets.UTF_8);

final LegacyMarginRequest marginRequest = new LegacyMarginRequest().marketDataStart(marketDataStart).marketDataEnd(marketDataEnd).tradeData(product).valuationDate(LocalDateTime.now().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
public class SmartDerivativeContractDescriptor {

private final String uniqueTradeIdentifier;
private final LocalDateTime tradeDate;
private final List<Party> counterparties;
private final Map<String, Double> marginAccountInitialByPartyID;
Expand Down Expand Up @@ -68,7 +69,8 @@ public String toString() {
}
}

public SmartDerivativeContractDescriptor(LocalDateTime tradeDate, List<Party> counterparties, Map<String, Double> marginAccountInitialByPartyID, Map<String, Double> penaltyFeeInitialByPartyID, String recervicePartyID, Node underlying, List<CalibrationDataItem.Spec> marketdataItems) {
public SmartDerivativeContractDescriptor(String uniqueTradeIdentifier, LocalDateTime tradeDate, List<Party> counterparties, Map<String, Double> marginAccountInitialByPartyID, Map<String, Double> penaltyFeeInitialByPartyID, String recervicePartyID, Node underlying, List<CalibrationDataItem.Spec> marketdataItems) {
this.uniqueTradeIdentifier = uniqueTradeIdentifier;
this.tradeDate = tradeDate;
this.counterparties = counterparties;
this.marginAccountInitialByPartyID = marginAccountInitialByPartyID;
Expand All @@ -83,6 +85,10 @@ public SmartDerivativeContractDescriptor(LocalDateTime tradeDate, List<Party> co
Validate.notNull(underlying, "Underlying must not be null.");
}

public String getUniqueTradeIdentifier() {
return uniqueTradeIdentifier;
}

public LocalDateTime getTradeDate() {
return tradeDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public PlainSwapEditorHandler(final PlainSwapOperationRequest plainSwapOperation

// create new SDCmL file as object
smartDerivativeContract = new Smartderivativecontract();
smartDerivativeContract.setUniqueTradeIdentifier("test123");
final Smartderivativecontract templateContract;
try {
ClassPathResource templateXmlResource = new ClassPathResource(templatePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private SDCXMLParser() {

public static SmartDerivativeContractDescriptor parse(String sdcxml) throws ParserConfigurationException, IOException, SAXException {



LocalDateTime settlementDateInitial;

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(sdcxml.getBytes(StandardCharsets.UTF_8)));
Expand All @@ -38,6 +40,8 @@ public static SmartDerivativeContractDescriptor parse(String sdcxml) throws Pars
String tradeDateString = document.getElementsByTagName("settlementDateInitial").item(0).getTextContent();
settlementDateInitial = LocalDateTime.parse(tradeDateString.trim());

String uniqueTradeIdentifier = document.getElementsByTagName("uniqueTradeIdentifier").item(0).getTextContent();

/*
Market Data
*/
Expand Down Expand Up @@ -87,7 +91,7 @@ public static SmartDerivativeContractDescriptor parse(String sdcxml) throws Pars

// TODO Support multiple underlyings
Node underlying = document.getElementsByTagName("underlying").item(0).getFirstChild().getNextSibling();
return new SmartDerivativeContractDescriptor(settlementDateInitial, parties, marginAccountInitialByPartyID, penaltyFeeInitialByPartyID, receiverPartyID, underlying,marketdataItems );
return new SmartDerivativeContractDescriptor(uniqueTradeIdentifier,settlementDateInitial, parties, marginAccountInitialByPartyID, penaltyFeeInitialByPartyID, receiverPartyID, underlying,marketdataItems );
}

/*
Expand Down
67 changes: 46 additions & 21 deletions src/main/java/net/finmath/smartcontract/reactive/DemoLauncher.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package net.finmath.smartcontract.reactive;


import com.neovisionaries.ws.client.WebSocket;
import io.reactivex.rxjava3.core.Observable;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import net.finmath.smartcontract.marketdata.adapters.LiveFeedAdapter;
import net.finmath.smartcontract.marketdata.adapters.MarketDataRandomFeedAdapter;
import net.finmath.smartcontract.marketdata.adapters.MarketDataWebSocketAdapter;
import net.finmath.smartcontract.marketdata.adapters.WebSocketConnector;
import net.finmath.smartcontract.marketdata.adapters.*;
import net.finmath.smartcontract.marketdata.curvecalibration.CalibrationDataItem;
import net.finmath.smartcontract.marketdata.curvecalibration.CalibrationDataset;
import net.finmath.smartcontract.marketdata.curvecalibration.CalibrationParser;
import net.finmath.smartcontract.marketdata.curvecalibration.CalibrationParserDataItems;
import net.finmath.smartcontract.model.ValueResult;
import net.finmath.smartcontract.model.MarketDataSet;
import net.finmath.smartcontract.product.SmartDerivativeContractDescriptor;
import net.finmath.smartcontract.product.xml.SDCXMLParser;
import net.finmath.smartcontract.valuation.MarginCalculator;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.*;
import java.util.concurrent.TimeUnit;


Expand All @@ -37,24 +34,32 @@ public class DemoLauncher {

public static void main(String[] args) throws Exception {

String sdcXML = new String(DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/sdc2.xml").readAllBytes(), StandardCharsets.UTF_8);
String sdcXML = new String(DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/smartderivativecontract.xml").readAllBytes(), StandardCharsets.UTF_8);
SmartDerivativeContractDescriptor sdc = SDCXMLParser.parse(sdcXML);
List<CalibrationDataItem.Spec> mdItemList = sdc.getMarketdataItemList();

/* Load connection properties*/
String connectionPropertiesFile = "Q:\\refinitiv_connect.properties";
Properties properties = new Properties();
properties.load(new FileInputStream(connectionPropertiesFile));
InputStream marketDataMessageStream = DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.client/nf_md_20230711-123529.json");
final String marketDataMessageContents = new String(Objects.requireNonNull(marketDataMessageStream).readAllBytes(), StandardCharsets.UTF_8);
MarketDataSet marketData = new ObjectMapper().registerModule(new JavaTimeModule())
.readValue(marketDataMessageContents, MarketDataSet.class);


/* Init Websockect Connection*/
final WebSocketConnector connector = new WebSocketConnector(properties);
final WebSocket socket = connector.getWebSocket();
//final WebSocketConnector connector = new WebSocketConnector(properties);
//final WebSocket socket = connector.getWebSocket();


/* Market Data Adapter */
//final LiveFeedAdapter<CalibrationDataset> emitter = new MarketDataWebSocketAdapter(connector.getAuthJson(), connector.getPosition(), mdItemList);
final LiveFeedAdapter<CalibrationDataset> emitter = new MarketDataRandomFeedAdapter(Period.ofDays(1),new String(DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.client/md_testset1.json").readAllBytes(), StandardCharsets.UTF_8));
socket.addListener(emitter);
socket.connect();
//final LiveFeedAdapter<MarketDataSet> emitter2 = new MarketDataRandomFeedAdapter2(LocalDateTime.now(),1,marketData);

//socket.addListener(emitter);
//socket.connect();

/* Write Market Data to File */
final Consumer<CalibrationDataset> marketDataWriter = new Consumer<CalibrationDataset>() {
Expand All @@ -67,7 +72,27 @@ public void accept(CalibrationDataset s) throws Throwable {
Files.write(path, json.getBytes());
}
};
emitter.asObservable().throttleLast(5,TimeUnit.SECONDS).subscribe(s->emitter.writeDataset("C:\\Temp\\marketdata\\",s,false));

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.INDENT_OUTPUT, true)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
final Consumer<MarketDataSet> marketDataWriter2 = new Consumer<MarketDataSet>() {
@Override
public void accept(MarketDataSet s) throws Throwable {
//s.values(s.getValues().stream().map(x -> this.overnightFixingPostProcessing(x, isOvernightFixing)).toList());
File targetFile = new File("C:\\Temp\\marketdata\\x.json");
mapper.writerFor(MarketDataSet.class).writeValue(targetFile, s);

//Path path = Paths.get("C:\\Temp\\marketdata\\md_" + timeStamp + ".json");
//Files.write(path, json.getBytes());
}
};


Disposable d = emitter.asObservable().throttleLast(5,TimeUnit.SECONDS).subscribe(marketDataWriter);//.subscribe(s->emitter.writeDataset("C:\\Temp\\marketdata\\",s,false));
d.dispose();
//emitter.writeDataset("C:\\Temp\\marketdata\\",emitter.asObservable().blockingFirst(),false);
Path dir = Paths.get("C:\\Temp\\marketdata\\"); // specify your directory

Expand Down Expand Up @@ -97,13 +122,13 @@ public void accept(CalibrationDataset calibrationDataSet) throws Throwable {
//emitter.asObservable().throttleFirst(60,TimeUnit.MINUTES).subscribe(fixingHistoryCollector);

/* Print Market Values */
Consumer<ValueResult> printValues = (ValueResult s) -> System.out.println("Consumer ValuationPrint: " + s.getValue().doubleValue());
/*Consumer<ValueResult> printValues = (ValueResult s) -> System.out.println("Consumer ValuationPrint: " + s.getValue().doubleValue());
final Observable observableValuation = emitter.asObservable().throttleLast(5, TimeUnit.SECONDS).map(marketData -> {
MarginCalculator calculator = new MarginCalculator();
return calculator.getValue(marketData.serializeToJson(), sdcXML);
});
observableValuation.subscribe(printValues);
observableValuation.subscribe(printValues);*/

/* Conditional Settlements */
/*BigDecimal settlementTriggerValue = BigDecimal.valueOf(100.);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DemoStarter {

public static void main(String[] args) throws Exception {

String sdcXML = new String(DemoStarter.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/sdc2.xml").readAllBytes(), StandardCharsets.UTF_8);
String sdcXML = new String(DemoStarter.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/smartderivativecontract.xml").readAllBytes(), StandardCharsets.UTF_8);
WebSocketClientEndpoint client = new WebSocketClientEndpoint(new URI("ws://localhost:443/valuationfeed"), "user1", "password1");
long timeout = client.getUserSession().getMaxIdleTimeout();
client.sendTextMessage(sdcXML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
else {
throw new IllegalStateException("Unexpected WebSocket message type: " + message);
}
String sdcXML = payload;//new String(DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/sdc2.xml").readAllBytes(), StandardCharsets.UTF_8);
String sdcXML = payload;//new String(DemoLauncher.class.getClassLoader().getResourceAsStream("net.finmath.smartcontract.product.xml/smartderivativecontract2.xml").readAllBytes(), StandardCharsets.UTF_8);
SmartDerivativeContractDescriptor sdc = SDCXMLParser.parse(sdcXML);
List<CalibrationDataItem.Spec> mdItemList = sdc.getMarketdataItemList();

Expand Down
Loading

0 comments on commit 99a81ba

Please sign in to comment.