不同金融工具的SWIFT报文类型
引言
随着金融行业的发展,各种金融工具的种类也越来越多。为了实现不同金融机构之间的交互,SWIFT(Society for Worldwide Interbank Financial Telecommunication)提供了一种标准化的报文格式。本文将介绍几种常见的金融工具及其对应的SWIFT报文类型,并通过代码示例来演示如何构建和解析这些报文。
类图
classDiagram
class FinancialInstrument
class EquityInstrument
class DebtInstrument
class DerivativeInstrument
class SwiftMessage
FinancialInstrument <|-- EquityInstrument
FinancialInstrument <|-- DebtInstrument
FinancialInstrument <|-- DerivativeInstrument
SwiftMessage "1" *-- "1..*" FinancialInstrument
状态图
stateDiagram
[*] --> Ready
Ready --> ConstructingMessage
ConstructingMessage --> Sending
Sending --> Parsing
Parsing --> [*]
1. 股票(Equity)
股票是一种代表公司所有权的金融工具。在SWIFT中,股票的信息可以通过MT568消息进行交换。
代码示例:
class EquityInstrument extends FinancialInstrument {
private String symbol;
private String exchange;
// ...
public SwiftMessage toSwiftMessage() {
SwiftMessage message = new SwiftMessage();
message.setType("MT568");
// 构建MT568报文内容
message.setContent("SYMBOL:" + symbol + "\n" +
"EXCHANGE:" + exchange + "\n" +
// ...
"ENDOFMESSAGE");
return message;
}
public static EquityInstrument fromSwiftMessage(SwiftMessage message) {
if (!message.getType().equals("MT568")) {
throw new IllegalArgumentException("Invalid message type");
}
EquityInstrument instrument = new EquityInstrument();
String content = message.getContent();
// 解析MT568报文内容
String[] lines = content.split("\n");
for (String line : lines) {
String[] parts = line.split(":");
String field = parts[0];
String value = parts[1];
switch (field) {
case "SYMBOL":
instrument.setSymbol(value);
break;
case "EXCHANGE":
instrument.setExchange(value);
break;
// ...
}
}
return instrument;
}
}
2. 债券(Debt)
债券是一种借贷关系的金融工具。在SWIFT中,债券的信息可以通过MT559消息进行交换。
代码示例:
class DebtInstrument extends FinancialInstrument {
private String issuer;
private LocalDate maturityDate;
// ...
public SwiftMessage toSwiftMessage() {
SwiftMessage message = new SwiftMessage();
message.setType("MT559");
// 构建MT559报文内容
message.setContent("ISSUER:" + issuer + "\n" +
"MATURITYDATE:" + maturityDate + "\n" +
// ...
"ENDOFMESSAGE");
return message;
}
public static DebtInstrument fromSwiftMessage(SwiftMessage message) {
if (!message.getType().equals("MT559")) {
throw new IllegalArgumentException("Invalid message type");
}
DebtInstrument instrument = new DebtInstrument();
String content = message.getContent();
// 解析MT559报文内容
String[] lines = content.split("\n");
for (String line : lines) {
String[] parts = line.split(":");
String field = parts[0];
String value = parts[1];
switch (field) {
case "ISSUER":
instrument.setIssuer(value);
break;
case "MATURITYDATE":
instrument.setMaturityDate(LocalDate.parse(value));
break;
// ...
}
}
return instrument;
}
}
3. 衍生品(Derivative)
衍生品是一种基于其他金融工具的金融合约。在SWIFT中,衍生品的信息可以通过MT566消息进行交换。
代码示例:
class DerivativeInstrument extends FinancialInstrument {
private String underlyingAsset;
private String contractType;
// ...
public SwiftMessage toSwiftMessage() {
SwiftMessage message = new SwiftMessage();
message.setType("MT566");
// 构建MT566报文内容
message.setContent("UNDERLYINGASSET:" + underlyingAsset + "\n" +
"CONTRACTTYPE:" + contractType + "\n" +
// ...
"ENDOFMESSAGE");
return message;
}
public static DerivativeInstrument fromSwiftMessage(SwiftMessage message) {