Skip to content

Commit 7acdcea

Browse files
cjkentakshaishahjodastephen
authored
Multiple CCPs in MarginClientExample (#174)
* Calc for multiple CCPs * Fix URL * Trigger build Co-authored-by: Akshai Shah <akshai@opengamma.com> Co-authored-by: Stephen Colebourne <stephen@opengamma.com>
1 parent a58df4b commit 7acdcea

3 files changed

Lines changed: 71 additions & 26 deletions

File tree

examples/src/main/java/com/opengamma/sdk/example/MarginClientExample.java

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import java.nio.file.Path;
99
import java.nio.file.Paths;
1010
import java.time.LocalDate;
11+
import java.util.ArrayList;
1112
import java.util.Collections;
13+
import java.util.HashMap;
1214
import java.util.List;
15+
import java.util.Map;
1316

1417
import org.joda.beans.ser.JodaBeanSer;
1518

@@ -33,46 +36,84 @@ public class MarginClientExample {
3336
private static final String DEV_ID = "PLEASE-CONTACT-OPENGAMMA";
3437
private static final String DEV_SECRET = "123";
3538
private static final Credentials CREDENTIALS = Credentials.ofApiKey(DEV_ID, DEV_SECRET);
39+
private static final Map<Ccp, Path> CCP_FILES;
3640

37-
// the file to upload
38-
private static final Path LCH_FILE = Paths.get("src/main/resources/com/opengamma/sdk/example/lch-trades.txt");
41+
static {
42+
Map<Ccp, Path> ccpMap = new HashMap<>();
43+
ccpMap.put(Ccp.LCH, Paths.get("src/main/resources/com/opengamma/sdk/example/lch-trades.txt"));
44+
ccpMap.put(Ccp.CME, Paths.get("src/main/resources/com/opengamma/sdk/example/cme.csv"));
45+
ccpMap.put(Ccp.ICE_SPAN, Paths.get("src/main/resources/com/opengamma/sdk/example/ice_span.csv"));
46+
CCP_FILES = Collections.unmodifiableMap(ccpMap);
47+
}
48+
49+
/**
50+
* Invoke without any arguments to calculate for LCH.
51+
* <p>
52+
* Provide CCPs as arguments to run calculations for those CCPs. Valid CCPs:
53+
* <ul>
54+
* <li>LCH</li>
55+
* <li>CME</li>
56+
* <li>ICE_SPAN</li>
57+
* </ul>
58+
*
59+
* @param args empty to calculate for LCH, otherwise list of CCPs for which to run calculations.
60+
*/
61+
public static void main(String[] args) {
62+
List<Ccp> ccps = new ArrayList<>();
63+
64+
if (args.length == 0) {
65+
ccps.add(Ccp.LCH);
66+
} else {
67+
for (String arg : args) {
68+
Ccp ccp = Ccp.of(arg);
69+
ccps.add(ccp);
70+
}
71+
}
3972

40-
// example code - invoke with no arguments
41-
public static void main(String[] args) throws InterruptedException {
4273
// create the invoker specifying the URL and credentials
4374
try (ServiceInvoker invoker = ServiceInvoker.of(CREDENTIALS)) {
4475
// Creating the margin client
4576
MarginClient client = MarginClient.of(invoker);
4677

4778
// Listing the CCPs that are available
48-
CcpsResult ccps = client.listCcps();
79+
CcpsResult ccpsResult = client.listCcps();
4980

50-
// Optional step: Checking if we are permissioned to the specific CCP calculation engine
51-
Ccp chosenCCP = Ccp.LCH;
52-
if (!ccps.isCcpAvailable(chosenCCP)) {
53-
throw new IllegalStateException("Margin Calculator not available for " + chosenCCP.name());
81+
for (Ccp ccp : ccps) {
82+
if (!CCP_FILES.containsKey(ccp)) {
83+
throw new IllegalStateException("Example portfolio data not available for " + ccp.name());
84+
}
85+
if (!ccpsResult.isCcpAvailable(ccp)) {
86+
throw new IllegalStateException("Margin Calculator not available for " + ccp.name());
87+
}
88+
calculate(client, ccp);
5489
}
90+
}
91+
}
5592

56-
//Retrieve specific information about the CCP calculation engine: valuation dates and available currencies
57-
CcpInfo lch = client.getCcpInfo(chosenCCP);
58-
LocalDate valuationDate = lch.getLatestValuationDate();
59-
String currency = lch.getDefaultCurrency();
93+
private static void calculate(MarginClient client, Ccp ccp) {
94+
System.out.println("Calculating for " + ccp.name());
95+
//Retrieve specific information about the CCP calculation engine: valuation dates and available currencies
96+
CcpInfo ccpInfo = client.getCcpInfo(ccp);
97+
LocalDate valuationDate = ccpInfo.getLatestValuationDate();
98+
String currency = ccpInfo.getDefaultCurrency();
6099

61-
// choose the file to upload
62-
List<PortfolioDataFile> files = Collections.singletonList(PortfolioDataFile.of(LCH_FILE));
100+
// choose the file to upload
101+
Path portfolioFile = CCP_FILES.get(ccp);
102+
List<PortfolioDataFile> files = Collections.singletonList(PortfolioDataFile.of(portfolioFile));
63103

64-
// create the request
65-
MarginCalcRequest request = MarginCalcRequest.of(valuationDate, currency, files);
104+
// create the request
105+
MarginCalcRequest request = MarginCalcRequest.of(valuationDate, currency, files);
66106

67-
// make the call and view the result
68-
MarginCalcResult result = client.calculate(chosenCCP, request);
69-
System.out.println(JodaBeanSer.PRETTY.simpleJsonWriter().write(result));
107+
// make the call and view the result
108+
MarginCalcResult result = client.calculate(ccp, request);
109+
System.out.println("Results for " + ccp.name());
110+
System.out.println(JodaBeanSer.PRETTY.simpleJsonWriter().write(result));
70111

71-
// make the what-if call and view the result (the difference in margin numbers)
72-
MarginWhatIfCalcResult whatIfResult =
73-
client.calculateWhatIf(Ccp.LCH, request, Collections.singletonList(PortfolioDataFile.of(LCH_FILE)));
74-
System.out.println(JodaBeanSer.PRETTY.simpleJsonWriter().write(whatIfResult));
75-
}
112+
// make the what-if call and view the result (the difference in margin numbers)
113+
System.out.println("Calculating what-if for " + ccp.name());
114+
MarginWhatIfCalcResult whatIfResult =
115+
client.calculateWhatIf(ccp, request, Collections.singletonList(PortfolioDataFile.of(portfolioFile)));
116+
System.out.println("What-if results for " + ccp.name());
117+
System.out.println(JodaBeanSer.PRETTY.simpleJsonWriter().write(whatIfResult));
76118
}
77-
78119
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Strata Trade Type,Id Scheme,Id,CCP,Counterparty,Trade Date,Buy Sell,Direction,Currency,Notional,Start Date,End Date,Day Count,Fixed Rate,Index,Interpolated Index,Date Convention,Date Calendar,Payment Date,Payment Date Convention,Payment Date Calendar,Netting Fund,Fund,Original Item ID,Sub Strategy,Broker,Business Unit,Desk,Strategy,Team,Product Type,Book,OTC Cleared Trade Id,Sub Account,Client Instrument Code,Portfolio Margin Model,Seniority,Doc Clause,Red Code,Bilateral-Margin Calculation Methodology,FxPB Grid,FX Option Delta Adjust,Leg 1 Direction,Leg 1 Start Date,Leg 1 End Date,Leg 1 Frequency,Leg 1 Roll Convention,Leg 1 Stub Convention,Leg 1 First Regular Start Date,Leg 1 Last Regular End Date,Leg 1 Date Convention,Leg 1 Date Calendar,Leg 1 Payment Frequency,Leg 1 Payment Relative To,Leg 1 Payment Offset Days,Leg 1 Payment Offset Calendar,Leg 1 Payment Offset Adjustment Convention,Leg 1 Payment Offset Adjustment Calendar,Leg 1 Compounding Method,Leg 1 Payment First Regular Start Date,Leg 1 Payment Last Regular End Date,Leg 1 Currency,Leg 1 Notional,Leg 1 Notional Initial Exchange,Leg 1 Notional Intermediate Exchange,Leg 1 Notional Final Exchange,Leg 1 Day Count,Leg 1 Fixed Rate,Leg 1 Future Value Notional,Leg 1 Index,Leg 1 Negative Rate Method,Leg 1 Gearing,Leg 1 Spread,Leg 1 Fixing Relative To,Leg 1 Fixing Offset Days,Leg 1 Fixing Offset Calendar,Leg 1 Fixing Offset Adjustment Convention,Leg 1 Fixing Offset Adjustment Calendar,Leg 1 Accrual Method,Leg 1 Rate Cut Off Days,Leg 1 Inflation Lag,Leg 1 Inflation Method,Leg 1 Inflation First Index Value,Leg 2 Direction,Leg 2 Start Date,Leg 2 End Date,Leg 2 Frequency,Leg 2 Roll Convention,Leg 2 Stub Convention,Leg 2 First Regular Start Date,Leg 2 Last Regular End Date,Leg 2 Date Convention,Leg 2 Date Calendar,Leg 2 Payment Frequency,Leg 2 Payment Relative To,Leg 2 Payment Offset Days,Leg 2 Payment Offset Calendar,Leg 2 Payment Offset Adjustment Convention,Leg 2 Payment Offset Adjustment Calendar,Leg 2 Compounding Method,Leg 2 Payment First Regular Start Date,Leg 2 Payment Last Regular End Date,Leg 2 Currency,Leg 2 Notional,Leg 2 Notional Initial Exchange,Leg 2 Notional Intermediate Exchange,Leg 2 Notional Final Exchange,Leg 2 Day Count,Leg 2 Fixed Rate,Leg 2 Future Value Notional,Leg 2 Index,Leg 2 Negative Rate Method,Leg 2 Gearing,Leg 2 Spread,Leg 2 Fixing Relative To,Leg 2 Fixing Offset Days,Leg 2 Fixing Offset Calendar,Leg 2 Fixing Offset Adjustment Convention,Leg 2 Fixing Offset Adjustment Calendar,Leg 2 Accrual Method,Leg 2 Rate Cut Off Days,Leg 2 Inflation Lag,Leg 2 Inflation Method,Leg 2 Inflation First Index Value,CDS Index Id Scheme,CDS Index Id,Legal Entity Id Scheme,Legal Entity Id,Payment On Default,Protection Start,Start Date Convention,Start Date Calendar,End Date Convention,End Date Calendar,Frequency,Roll Convention,Stub Convention,First Regular Start Date,Last Regular End Date,Leg 1 Payment Date,Leg 2 Payment Date,Long Short,Expiry Date,Expiry Time,Expiry Zone,Premium Date,Premium Date Convention,Premium Date Calendar,Premium Direction,Premium Currency,Premium Amount
2+
Swap,OG-Trade,41788094,CME,,18/03/2021,,,MXN,,,,,,,,,,,,,XPMF,XPMF,OG-Trade~41788094,Rates,BARC,FI_PMs,FOORD,N/A,FOORD,OTC_CLEARED,g10fly,41788094,Rates,SW IR MXN 28dTIIE 6/16/21-6/10/26 5.08,Drim,,,,,,,Pay,16/06/2021,10/06/2026,P4W,None,SmartInitial,,,NoAdjust,MXMC,P4W,PeriodEnd,0,MXMC,NoAdjust,MXMC,Straight,,,MXN,825000000,FALSE,FALSE,FALSE,Act/360,,,MXN-TIIE-4W,AllowNegative,,0,PeriodStart,-1,MXMC,NoAdjust,NoHolidays,,,,,,Receive,16/06/2021,10/06/2026,P4W,None,SmartInitial,,,NoAdjust,MXMC,P4W,PeriodEnd,0,MXMC,NoAdjust,MXMC,Straight,,,MXN,825000000,FALSE,FALSE,FALSE,Act/360,5.08,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Strata Position Type,Id Scheme,Id,CCP,Broker,Fund,Exchange,Contract Code,Contract Code Type,Long Quantity,Short Quantity,Expiry,Expiry Day,Put Call,Exercise Price,Book,Desk,Portfolio Margin Model,Strategy,Sub Strategy
2+
FUT,OG-ETD,E13_ETD_Trades.csv_35,ICE,BARC,XPMF,IFUS,MME,EXG,65,0,2021-06,,,,X-ASSETS,ANDRE,,N/A,Equity Stat Arb

0 commit comments

Comments
 (0)