etreschenkov 2026-04-14 17:05:52 +03:00
parent d72e0b79c5
commit 76bf6883bb
4 changed files with 121 additions and 64 deletions

View file

@ -12,22 +12,23 @@ import ru.clearing.classes.statics.data.security.Rates;
import ru.clearing.platform.dictionary.CurrencyPairDictionary; import ru.clearing.platform.dictionary.CurrencyPairDictionary;
import ru.spcex.clearing.dflt.management.model.CalculationResult; import ru.spcex.clearing.dflt.management.model.CalculationResult;
import ru.spcex.clearing.dflt.management.model.SwapContext; import ru.spcex.clearing.dflt.management.model.SwapContext;
import ru.spcex.platform.enumeration.SettleCode;
@Service @Service
public class SwapCalculationService { public class OrderCalculationService {
private static final MathContext MC = new MathContext(20, RoundingMode.HALF_UP); private static final MathContext MC = new MathContext(20, RoundingMode.HALF_UP);
private final ImdgQueryService imdgQueryService; private final ImdgQueryService imdgQueryService;
private final TransferDateService transferDateService; private final TransferDateService transferDateService;
public SwapCalculationService(ImdgQueryService imdgQueryService, public OrderCalculationService(ImdgQueryService imdgQueryService,
TransferDateService transferDateService) { TransferDateService transferDateService) {
this.imdgQueryService = imdgQueryService; this.imdgQueryService = imdgQueryService;
this.transferDateService = transferDateService; this.transferDateService = transferDateService;
} }
public void calcAndEnrich(OrderCurrency order, public void calcAndEnrichSwap(OrderCurrency order,
SwapContext context) { SwapContext context) {
Registry registry = context.getRegistry(); Registry registry = context.getRegistry();
Listing listing = context.getListing(); Listing listing = context.getListing();
@ -46,6 +47,35 @@ public class SwapCalculationService {
order.setQuantityLot(quantityLot); order.setQuantityLot(quantityLot);
} }
public void calcAndEnrichSpot(OrderCurrency order,
SwapContext context) {
Registry registry = context.getRegistry();
Listing listing = context.getListing();
CurrencyPairDictionary pair = imdgQueryService.getCurrencyPairDictionaryByListing(listing);
BigDecimal s = imdgQueryService.defineTransferRate(pair.getId(), registry.getSecuritySymbol());
BigDecimal n = transferDateService.numberOfDaysOfTransfer();
CalculationResult result = isRub(registry)
? calculationSwapRubSums(registry, listing.getLotSize().intValue(), s, n)
: calculationSwapSums(registry, listing.getLotSize().intValue(), s, n);
Rates rates = imdgQueryService.getRate(pair.getBaseCurrency(), LocalDate.now());
BigDecimal quantityLot;
BigDecimal price;
if (SettleCode.T0.equalsByKey(order.getSettleCode())) {
quantityLot = result.sum3().abs();
price = rates.getValue();
} else {
quantityLot = result.sum5().abs();
BigDecimal delta = swapPriceCalc(pair.getBaseCurrency(), s, n);
price = rates.getValue().add(delta);
}
order.setPrice(price);
order.setQuantityLot(quantityLot);
}
private CalculationResult calculationSwapSums(Registry registry, Integer lotSize, BigDecimal s, BigDecimal n) { private CalculationResult calculationSwapSums(Registry registry, Integer lotSize, BigDecimal s, BigDecimal n) {
BigDecimal planPosition = registry.getBalance(); BigDecimal planPosition = registry.getBalance();
BigDecimal sum1 = planPosition.abs(); BigDecimal sum1 = planPosition.abs();

View file

@ -8,61 +8,66 @@ import ru.clearing.classes.statics.data.registry.Registry;
import ru.clearing.classes.statics.data.security.RiskParameter; import ru.clearing.classes.statics.data.security.RiskParameter;
import ru.spcex.clearing.dflt.management.model.CounterpartyInfo; import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
import ru.spcex.clearing.dflt.management.model.SwapContext; import ru.spcex.clearing.dflt.management.model.SwapContext;
import ru.spcex.platform.enumeration.SettleCode;
import static ru.spcex.platform.enumeration.SettleCode.T0;
import ru.spcex.platform.enumeration.Side; import ru.spcex.platform.enumeration.Side;
@Service @Service
public class SwapOrderTemplateBuilder { public class OrderTemplateBuilder {
public OrderCurrency buildPrimaryTransfer(SwapContext context) { public OrderCurrency buildPrimaryTransfer(SwapContext context, SettleCode settleCode) {
OrderCurrency orderCurrency; OrderCurrency orderCurrency;
switch (context.getOrderCategory()) { switch (context.getOrderCategory()) {
case "PL" -> orderCurrency = buildPrimaryTransferPl(context); case "PL" -> orderCurrency = buildPrimaryTransferPl(context);
case "UK" -> orderCurrency = buildPrimaryTransferUk(context); case "UK" -> orderCurrency = buildPrimaryTransferUk(context);
default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory()); default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory());
} }
Side side = resolvePrimaryTransferSide(context.getRegistry()); Side side = resolvePrimaryTransferSide(context.getRegistry(), settleCode);
enrichOrder(orderCurrency, context, side, "overnight"); enrichOrder(orderCurrency, context, side, "overnight", settleCode);
return orderCurrency; return orderCurrency;
} }
public OrderCurrency buildCounterTransfer(SwapContext context) { public OrderCurrency buildCounterTransfer(SwapContext context, SettleCode settleCode) {
OrderCurrency orderCurrency; OrderCurrency orderCurrency;
switch (context.getOrderCategory()) { switch (context.getOrderCategory()) {
case "PL" -> orderCurrency = buildCounterTransferPl(context); case "PL" -> orderCurrency = buildCounterTransferPl(context);
case "UK" -> orderCurrency = buildCounterTransferUk(context); case "UK" -> orderCurrency = buildCounterTransferUk(context);
default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory()); default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory());
}; }
Side side = resolveCounterTransferSide(context.getRegistry()); ;
enrichOrder(orderCurrency, context, side, "overnight"); Side side = resolveCounterTransferSide(context.getRegistry(), settleCode);
enrichOrder(orderCurrency, context, side, "overnight", settleCode);
return orderCurrency; return orderCurrency;
} }
public OrderCurrency buildPrimaryBalancing(SwapContext context) { public OrderCurrency buildPrimaryBalancing(SwapContext context, SettleCode settleCode) {
OrderCurrency orderCurrency; OrderCurrency orderCurrency;
switch (context.getOrderCategory()) { switch (context.getOrderCategory()) {
case "PL" -> orderCurrency = buildPrimaryBalancingPl(context); case "PL" -> orderCurrency = buildPrimaryBalancingPl(context);
case "UK" -> orderCurrency = buildPrimaryBalancingUk(context); case "UK" -> orderCurrency = buildPrimaryBalancingUk(context);
default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory()); default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory());
}; }
Side side = resolvePrimaryBalancingSide(context.getRegistry()); ;
enrichOrder(orderCurrency, context, side, "balance"); Side side = resolvePrimaryBalancingSide(context.getRegistry(), settleCode);
enrichOrder(orderCurrency, context, side, "balance", settleCode);
return orderCurrency; return orderCurrency;
} }
public OrderCurrency buildCounterBalancing(SwapContext context) { public OrderCurrency buildCounterBalancing(SwapContext context, SettleCode settleCode) {
OrderCurrency orderCurrency; OrderCurrency orderCurrency;
switch (context.getOrderCategory()) { switch (context.getOrderCategory()) {
case "PL" -> orderCurrency = buildCounterBalancingPl(context); case "PL" -> orderCurrency = buildCounterBalancingPl(context);
case "UK" -> orderCurrency = buildCounterBalancingUk(context); case "UK" -> orderCurrency = buildCounterBalancingUk(context);
default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory()); default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory());
}; }
Side side = resolveCounterBalancingSide(context.getRegistry()); ;
enrichOrder(orderCurrency, context, side, "balance"); Side side = resolveCounterBalancingSide(context.getRegistry(), settleCode);
enrichOrder(orderCurrency, context, side, "balance", settleCode);
return orderCurrency; return orderCurrency;
} }
private OrderCurrency buildPrimaryTransferUk(SwapContext context) { private OrderCurrency buildPrimaryTransferUk(SwapContext context) {
CounterpartyInfo clcc = context.getClcc(); CounterpartyInfo clcc = context.getClcc();
Registry registry = context.getRegistry(); Registry registry = context.getRegistry();
OrderCurrency order = new OrderCurrency(); OrderCurrency order = new OrderCurrency();
@ -182,9 +187,10 @@ public class SwapOrderTemplateBuilder {
} }
public void enrichOrder(OrderCurrency order, public void enrichOrder(OrderCurrency order,
SwapContext context, SwapContext context,
Side side, Side side,
String comment) { String comment,
SettleCode settleCode) {
Registry registry = context.getRegistry(); Registry registry = context.getRegistry();
Listing listing = context.getListing(); Listing listing = context.getListing();
Market market = context.getMarket(); Market market = context.getMarket();
@ -198,26 +204,30 @@ public class SwapOrderTemplateBuilder {
order.setLotSize(listing.getLotSize()); order.setLotSize(listing.getLotSize());
order.setRate(riskParameter.getQuoteValue()); order.setRate(riskParameter.getQuoteValue());
order.setSide(side.getKey()); order.setSide(side.getKey());
order.setSettleCode("T1"); order.setSettleCode(settleCode.getKey());
order.setStatus("CRET"); order.setStatus("CRET");
order.setComment(comment); order.setComment(comment);
order.setRegistryId(registry.getId()); order.setRegistryId(registry.getId());
} }
private Side resolvePrimaryTransferSide(Registry registry) { private Side resolvePrimaryTransferSide(Registry registry, SettleCode settleCode) {
return isRub(registry) ? Side.BUY : Side.SELL; Side side = T0 == settleCode ? Side.BUY : Side.SELL;
return isRub(registry) ? side : side.revers();
} }
private Side resolveCounterTransferSide(Registry registry) { private Side resolveCounterTransferSide(Registry registry, SettleCode settleCode) {
return isRub(registry) ? Side.SELL : Side.BUY; Side side = T0 == settleCode ? Side.SELL : Side.BUY;
return isRub(registry) ? side : side.revers();
} }
private Side resolvePrimaryBalancingSide(Registry registry) { private Side resolvePrimaryBalancingSide(Registry registry, SettleCode settleCode) {
return isRub(registry) ? Side.SELL : Side.BUY; Side side = T0 == settleCode ? Side.SELL : Side.BUY;
return isRub(registry) ? side : side.revers();
} }
private Side resolveCounterBalancingSide(Registry registry) { private Side resolveCounterBalancingSide(Registry registry, SettleCode settleCode) {
return isRub(registry) ? Side.BUY : Side.SELL; Side side = T0 == settleCode ? Side.BUY : Side.SELL;
return isRub(registry) ? side : side.revers();
} }
private boolean isRub(Registry registry) { private boolean isRub(Registry registry) {

View file

@ -12,6 +12,7 @@ import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
import ru.spcex.clearing.dflt.management.model.SwapContext; import ru.spcex.clearing.dflt.management.model.SwapContext;
import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.platform.enumeration.CompanyRole; import ru.spcex.platform.enumeration.CompanyRole;
import ru.spcex.platform.enumeration.SettleCode;
import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.imdg.api.ImdgProvider;
@ -21,13 +22,13 @@ public class SpotOvernightOrderService {
private final ImdgQueryService imdgQueryService; private final ImdgQueryService imdgQueryService;
private final Imdg<OrderCurrency> orderCurrencyImdg; private final Imdg<OrderCurrency> orderCurrencyImdg;
private final SwapOrderTemplateBuilder templateBuilder; private final OrderTemplateBuilder templateBuilder;
private final SwapCalculationService swapCalculationService; private final OrderCalculationService swapCalculationService;
public SpotOvernightOrderService(ImdgProvider imdgProvider, public SpotOvernightOrderService(ImdgProvider imdgProvider,
ImdgQueryService imdgQueryService, ImdgQueryService imdgQueryService,
SwapOrderTemplateBuilder templateBuilder, OrderTemplateBuilder templateBuilder,
SwapCalculationService swapCalculationService) { OrderCalculationService swapCalculationService) {
this.imdgQueryService = imdgQueryService; this.imdgQueryService = imdgQueryService;
this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class); this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class);
this.templateBuilder = templateBuilder; this.templateBuilder = templateBuilder;
@ -55,20 +56,35 @@ public class SpotOvernightOrderService {
.rgcm(rgcm) .rgcm(rgcm)
.build(); .build();
// OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context); OrderCurrency primaryTransferT0 = templateBuilder.buildPrimaryTransfer(context, SettleCode.T0);
// OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context); OrderCurrency counterTransferT0 = templateBuilder.buildCounterTransfer(context, SettleCode.T0);
// OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context); OrderCurrency primaryTransferT1 = templateBuilder.buildPrimaryTransfer(context, SettleCode.T1);
// OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context); OrderCurrency counterTransferT1 = templateBuilder.buildCounterTransfer(context, SettleCode.T1);
//
// swapCalculationService.calcAndEnrich(primaryTransfer, context); OrderCurrency primaryBalancingT0 = templateBuilder.buildPrimaryBalancing(context, SettleCode.T0);
// swapCalculationService.calcAndEnrich(counterTransfer, context); OrderCurrency counterBalancingT0 = templateBuilder.buildCounterBalancing(context, SettleCode.T0);
// swapCalculationService.calcAndEnrich(primaryBalancing, context); OrderCurrency primaryBalancingT1 = templateBuilder.buildPrimaryBalancing(context, SettleCode.T1);
// swapCalculationService.calcAndEnrich(counterBalancing, context); OrderCurrency counterBalancingT1 = templateBuilder.buildCounterBalancing(context, SettleCode.T1);
//
// orderCurrencyImdg.insert(primaryTransfer); swapCalculationService.calcAndEnrichSpot(primaryTransferT0, context);
// orderCurrencyImdg.insert(counterTransfer); swapCalculationService.calcAndEnrichSpot(counterTransferT0, context);
// orderCurrencyImdg.insert(primaryBalancing); swapCalculationService.calcAndEnrichSpot(primaryTransferT1, context);
// orderCurrencyImdg.insert(counterBalancing); swapCalculationService.calcAndEnrichSpot(counterTransferT1, context);
swapCalculationService.calcAndEnrichSpot(primaryBalancingT0, context);
swapCalculationService.calcAndEnrichSpot(counterBalancingT0, context);
swapCalculationService.calcAndEnrichSpot(primaryBalancingT1, context);
swapCalculationService.calcAndEnrichSpot(counterBalancingT1, context);
orderCurrencyImdg.insert(primaryTransferT0);
orderCurrencyImdg.insert(counterTransferT0);
orderCurrencyImdg.insert(primaryTransferT1);
orderCurrencyImdg.insert(counterTransferT1);
orderCurrencyImdg.insert(primaryBalancingT0);
orderCurrencyImdg.insert(counterBalancingT0);
orderCurrencyImdg.insert(primaryBalancingT1);
orderCurrencyImdg.insert(counterBalancingT1);
log.info("Spot orders created for registryId={}", registry.getId()); log.info("Spot orders created for registryId={}", registry.getId());
} }

View file

@ -12,6 +12,7 @@ import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
import ru.spcex.clearing.dflt.management.model.SwapContext; import ru.spcex.clearing.dflt.management.model.SwapContext;
import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.platform.enumeration.CompanyRole; import ru.spcex.platform.enumeration.CompanyRole;
import ru.spcex.platform.enumeration.SettleCode;
import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.imdg.api.ImdgProvider;
@ -21,13 +22,13 @@ public class SwapOvernightOrderService {
private final ImdgQueryService imdgQueryService; private final ImdgQueryService imdgQueryService;
private final Imdg<OrderCurrency> orderCurrencyImdg; private final Imdg<OrderCurrency> orderCurrencyImdg;
private final SwapOrderTemplateBuilder templateBuilder; private final OrderTemplateBuilder templateBuilder;
private final SwapCalculationService swapCalculationService; private final OrderCalculationService swapCalculationService;
public SwapOvernightOrderService(ImdgProvider imdgProvider, public SwapOvernightOrderService(ImdgProvider imdgProvider,
ImdgQueryService imdgQueryService, ImdgQueryService imdgQueryService,
SwapOrderTemplateBuilder templateBuilder, OrderTemplateBuilder templateBuilder,
SwapCalculationService swapCalculationService) { OrderCalculationService swapCalculationService) {
this.imdgQueryService = imdgQueryService; this.imdgQueryService = imdgQueryService;
this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class); this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class);
this.templateBuilder = templateBuilder; this.templateBuilder = templateBuilder;
@ -55,15 +56,15 @@ public class SwapOvernightOrderService {
.rgcm(rgcm) .rgcm(rgcm)
.build(); .build();
OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context); OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context, SettleCode.T1);
OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context); OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context, SettleCode.T1);
OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context); OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context, SettleCode.T1);
OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context); OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context, SettleCode.T1);
swapCalculationService.calcAndEnrich(primaryTransfer, context); swapCalculationService.calcAndEnrichSwap(primaryTransfer, context);
swapCalculationService.calcAndEnrich(counterTransfer, context); swapCalculationService.calcAndEnrichSwap(counterTransfer, context);
swapCalculationService.calcAndEnrich(primaryBalancing, context); swapCalculationService.calcAndEnrichSwap(primaryBalancing, context);
swapCalculationService.calcAndEnrich(counterBalancing, context); swapCalculationService.calcAndEnrichSwap(counterBalancing, context);
orderCurrencyImdg.insert(primaryTransfer); orderCurrencyImdg.insert(primaryTransfer);
orderCurrencyImdg.insert(counterTransfer); orderCurrencyImdg.insert(counterTransfer);