This commit is contained in:
parent
d72e0b79c5
commit
76bf6883bb
4 changed files with 121 additions and 64 deletions
|
|
@ -12,22 +12,23 @@ import ru.clearing.classes.statics.data.security.Rates;
|
|||
import ru.clearing.platform.dictionary.CurrencyPairDictionary;
|
||||
import ru.spcex.clearing.dflt.management.model.CalculationResult;
|
||||
import ru.spcex.clearing.dflt.management.model.SwapContext;
|
||||
import ru.spcex.platform.enumeration.SettleCode;
|
||||
|
||||
@Service
|
||||
public class SwapCalculationService {
|
||||
public class OrderCalculationService {
|
||||
private static final MathContext MC = new MathContext(20, RoundingMode.HALF_UP);
|
||||
|
||||
private final ImdgQueryService imdgQueryService;
|
||||
private final TransferDateService transferDateService;
|
||||
|
||||
public SwapCalculationService(ImdgQueryService imdgQueryService,
|
||||
TransferDateService transferDateService) {
|
||||
public OrderCalculationService(ImdgQueryService imdgQueryService,
|
||||
TransferDateService transferDateService) {
|
||||
this.imdgQueryService = imdgQueryService;
|
||||
this.transferDateService = transferDateService;
|
||||
}
|
||||
|
||||
public void calcAndEnrich(OrderCurrency order,
|
||||
SwapContext context) {
|
||||
public void calcAndEnrichSwap(OrderCurrency order,
|
||||
SwapContext context) {
|
||||
Registry registry = context.getRegistry();
|
||||
Listing listing = context.getListing();
|
||||
|
||||
|
|
@ -46,6 +47,35 @@ public class SwapCalculationService {
|
|||
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) {
|
||||
BigDecimal planPosition = registry.getBalance();
|
||||
BigDecimal sum1 = planPosition.abs();
|
||||
|
|
@ -8,61 +8,66 @@ import ru.clearing.classes.statics.data.registry.Registry;
|
|||
import ru.clearing.classes.statics.data.security.RiskParameter;
|
||||
import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
|
||||
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;
|
||||
|
||||
@Service
|
||||
public class SwapOrderTemplateBuilder {
|
||||
public class OrderTemplateBuilder {
|
||||
|
||||
public OrderCurrency buildPrimaryTransfer(SwapContext context) {
|
||||
public OrderCurrency buildPrimaryTransfer(SwapContext context, SettleCode settleCode) {
|
||||
OrderCurrency orderCurrency;
|
||||
switch (context.getOrderCategory()) {
|
||||
case "PL" -> orderCurrency = buildPrimaryTransferPl(context);
|
||||
case "UK" -> orderCurrency = buildPrimaryTransferUk(context);
|
||||
default -> throw new IllegalArgumentException("Unsupported order category: " + context.getOrderCategory());
|
||||
}
|
||||
Side side = resolvePrimaryTransferSide(context.getRegistry());
|
||||
enrichOrder(orderCurrency, context, side, "overnight");
|
||||
Side side = resolvePrimaryTransferSide(context.getRegistry(), settleCode);
|
||||
enrichOrder(orderCurrency, context, side, "overnight", settleCode);
|
||||
return orderCurrency;
|
||||
}
|
||||
|
||||
public OrderCurrency buildCounterTransfer(SwapContext context) {
|
||||
public OrderCurrency buildCounterTransfer(SwapContext context, SettleCode settleCode) {
|
||||
OrderCurrency orderCurrency;
|
||||
switch (context.getOrderCategory()) {
|
||||
case "PL" -> orderCurrency = buildCounterTransferPl(context);
|
||||
case "UK" -> orderCurrency = buildCounterTransferUk(context);
|
||||
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;
|
||||
}
|
||||
|
||||
public OrderCurrency buildPrimaryBalancing(SwapContext context) {
|
||||
public OrderCurrency buildPrimaryBalancing(SwapContext context, SettleCode settleCode) {
|
||||
OrderCurrency orderCurrency;
|
||||
switch (context.getOrderCategory()) {
|
||||
case "PL" -> orderCurrency = buildPrimaryBalancingPl(context);
|
||||
case "UK" -> orderCurrency = buildPrimaryBalancingUk(context);
|
||||
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;
|
||||
}
|
||||
|
||||
public OrderCurrency buildCounterBalancing(SwapContext context) {
|
||||
public OrderCurrency buildCounterBalancing(SwapContext context, SettleCode settleCode) {
|
||||
OrderCurrency orderCurrency;
|
||||
switch (context.getOrderCategory()) {
|
||||
case "PL" -> orderCurrency = buildCounterBalancingPl(context);
|
||||
case "UK" -> orderCurrency = buildCounterBalancingUk(context);
|
||||
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;
|
||||
}
|
||||
|
||||
private OrderCurrency buildPrimaryTransferUk(SwapContext context) {
|
||||
CounterpartyInfo clcc = context.getClcc();
|
||||
CounterpartyInfo clcc = context.getClcc();
|
||||
Registry registry = context.getRegistry();
|
||||
|
||||
OrderCurrency order = new OrderCurrency();
|
||||
|
|
@ -182,9 +187,10 @@ public class SwapOrderTemplateBuilder {
|
|||
}
|
||||
|
||||
public void enrichOrder(OrderCurrency order,
|
||||
SwapContext context,
|
||||
Side side,
|
||||
String comment) {
|
||||
SwapContext context,
|
||||
Side side,
|
||||
String comment,
|
||||
SettleCode settleCode) {
|
||||
Registry registry = context.getRegistry();
|
||||
Listing listing = context.getListing();
|
||||
Market market = context.getMarket();
|
||||
|
|
@ -198,26 +204,30 @@ public class SwapOrderTemplateBuilder {
|
|||
order.setLotSize(listing.getLotSize());
|
||||
order.setRate(riskParameter.getQuoteValue());
|
||||
order.setSide(side.getKey());
|
||||
order.setSettleCode("T1");
|
||||
order.setSettleCode(settleCode.getKey());
|
||||
order.setStatus("CRET");
|
||||
order.setComment(comment);
|
||||
order.setRegistryId(registry.getId());
|
||||
}
|
||||
|
||||
private Side resolvePrimaryTransferSide(Registry registry) {
|
||||
return isRub(registry) ? Side.BUY : Side.SELL;
|
||||
private Side resolvePrimaryTransferSide(Registry registry, SettleCode settleCode) {
|
||||
Side side = T0 == settleCode ? Side.BUY : Side.SELL;
|
||||
return isRub(registry) ? side : side.revers();
|
||||
}
|
||||
|
||||
private Side resolveCounterTransferSide(Registry registry) {
|
||||
return isRub(registry) ? Side.SELL : Side.BUY;
|
||||
private Side resolveCounterTransferSide(Registry registry, SettleCode settleCode) {
|
||||
Side side = T0 == settleCode ? Side.SELL : Side.BUY;
|
||||
return isRub(registry) ? side : side.revers();
|
||||
}
|
||||
|
||||
private Side resolvePrimaryBalancingSide(Registry registry) {
|
||||
return isRub(registry) ? Side.SELL : Side.BUY;
|
||||
private Side resolvePrimaryBalancingSide(Registry registry, SettleCode settleCode) {
|
||||
Side side = T0 == settleCode ? Side.SELL : Side.BUY;
|
||||
return isRub(registry) ? side : side.revers();
|
||||
}
|
||||
|
||||
private Side resolveCounterBalancingSide(Registry registry) {
|
||||
return isRub(registry) ? Side.BUY : Side.SELL;
|
||||
private Side resolveCounterBalancingSide(Registry registry, SettleCode settleCode) {
|
||||
Side side = T0 == settleCode ? Side.BUY : Side.SELL;
|
||||
return isRub(registry) ? side : side.revers();
|
||||
}
|
||||
|
||||
private boolean isRub(Registry registry) {
|
||||
|
|
@ -12,6 +12,7 @@ import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
|
|||
import ru.spcex.clearing.dflt.management.model.SwapContext;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
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.ImdgProvider;
|
||||
|
||||
|
|
@ -21,13 +22,13 @@ public class SpotOvernightOrderService {
|
|||
|
||||
private final ImdgQueryService imdgQueryService;
|
||||
private final Imdg<OrderCurrency> orderCurrencyImdg;
|
||||
private final SwapOrderTemplateBuilder templateBuilder;
|
||||
private final SwapCalculationService swapCalculationService;
|
||||
private final OrderTemplateBuilder templateBuilder;
|
||||
private final OrderCalculationService swapCalculationService;
|
||||
|
||||
public SpotOvernightOrderService(ImdgProvider imdgProvider,
|
||||
ImdgQueryService imdgQueryService,
|
||||
SwapOrderTemplateBuilder templateBuilder,
|
||||
SwapCalculationService swapCalculationService) {
|
||||
OrderTemplateBuilder templateBuilder,
|
||||
OrderCalculationService swapCalculationService) {
|
||||
this.imdgQueryService = imdgQueryService;
|
||||
this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class);
|
||||
this.templateBuilder = templateBuilder;
|
||||
|
|
@ -55,20 +56,35 @@ public class SpotOvernightOrderService {
|
|||
.rgcm(rgcm)
|
||||
.build();
|
||||
|
||||
// OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context);
|
||||
// OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context);
|
||||
// OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context);
|
||||
// OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context);
|
||||
//
|
||||
// swapCalculationService.calcAndEnrich(primaryTransfer, context);
|
||||
// swapCalculationService.calcAndEnrich(counterTransfer, context);
|
||||
// swapCalculationService.calcAndEnrich(primaryBalancing, context);
|
||||
// swapCalculationService.calcAndEnrich(counterBalancing, context);
|
||||
//
|
||||
// orderCurrencyImdg.insert(primaryTransfer);
|
||||
// orderCurrencyImdg.insert(counterTransfer);
|
||||
// orderCurrencyImdg.insert(primaryBalancing);
|
||||
// orderCurrencyImdg.insert(counterBalancing);
|
||||
OrderCurrency primaryTransferT0 = templateBuilder.buildPrimaryTransfer(context, SettleCode.T0);
|
||||
OrderCurrency counterTransferT0 = templateBuilder.buildCounterTransfer(context, SettleCode.T0);
|
||||
OrderCurrency primaryTransferT1 = templateBuilder.buildPrimaryTransfer(context, SettleCode.T1);
|
||||
OrderCurrency counterTransferT1 = templateBuilder.buildCounterTransfer(context, SettleCode.T1);
|
||||
|
||||
OrderCurrency primaryBalancingT0 = templateBuilder.buildPrimaryBalancing(context, SettleCode.T0);
|
||||
OrderCurrency counterBalancingT0 = templateBuilder.buildCounterBalancing(context, SettleCode.T0);
|
||||
OrderCurrency primaryBalancingT1 = templateBuilder.buildPrimaryBalancing(context, SettleCode.T1);
|
||||
OrderCurrency counterBalancingT1 = templateBuilder.buildCounterBalancing(context, SettleCode.T1);
|
||||
|
||||
swapCalculationService.calcAndEnrichSpot(primaryTransferT0, context);
|
||||
swapCalculationService.calcAndEnrichSpot(counterTransferT0, context);
|
||||
swapCalculationService.calcAndEnrichSpot(primaryTransferT1, context);
|
||||
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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import ru.spcex.clearing.dflt.management.model.CounterpartyInfo;
|
|||
import ru.spcex.clearing.dflt.management.model.SwapContext;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
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.ImdgProvider;
|
||||
|
||||
|
|
@ -21,13 +22,13 @@ public class SwapOvernightOrderService {
|
|||
|
||||
private final ImdgQueryService imdgQueryService;
|
||||
private final Imdg<OrderCurrency> orderCurrencyImdg;
|
||||
private final SwapOrderTemplateBuilder templateBuilder;
|
||||
private final SwapCalculationService swapCalculationService;
|
||||
private final OrderTemplateBuilder templateBuilder;
|
||||
private final OrderCalculationService swapCalculationService;
|
||||
|
||||
public SwapOvernightOrderService(ImdgProvider imdgProvider,
|
||||
ImdgQueryService imdgQueryService,
|
||||
SwapOrderTemplateBuilder templateBuilder,
|
||||
SwapCalculationService swapCalculationService) {
|
||||
OrderTemplateBuilder templateBuilder,
|
||||
OrderCalculationService swapCalculationService) {
|
||||
this.imdgQueryService = imdgQueryService;
|
||||
this.orderCurrencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_OrderCurrency, OrderCurrency.class);
|
||||
this.templateBuilder = templateBuilder;
|
||||
|
|
@ -55,15 +56,15 @@ public class SwapOvernightOrderService {
|
|||
.rgcm(rgcm)
|
||||
.build();
|
||||
|
||||
OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context);
|
||||
OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context);
|
||||
OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context);
|
||||
OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context);
|
||||
OrderCurrency primaryTransfer = templateBuilder.buildPrimaryTransfer(context, SettleCode.T1);
|
||||
OrderCurrency counterTransfer = templateBuilder.buildCounterTransfer(context, SettleCode.T1);
|
||||
OrderCurrency primaryBalancing = templateBuilder.buildPrimaryBalancing(context, SettleCode.T1);
|
||||
OrderCurrency counterBalancing = templateBuilder.buildCounterBalancing(context, SettleCode.T1);
|
||||
|
||||
swapCalculationService.calcAndEnrich(primaryTransfer, context);
|
||||
swapCalculationService.calcAndEnrich(counterTransfer, context);
|
||||
swapCalculationService.calcAndEnrich(primaryBalancing, context);
|
||||
swapCalculationService.calcAndEnrich(counterBalancing, context);
|
||||
swapCalculationService.calcAndEnrichSwap(primaryTransfer, context);
|
||||
swapCalculationService.calcAndEnrichSwap(counterTransfer, context);
|
||||
swapCalculationService.calcAndEnrichSwap(primaryBalancing, context);
|
||||
swapCalculationService.calcAndEnrichSwap(counterBalancing, context);
|
||||
|
||||
orderCurrencyImdg.insert(primaryTransfer);
|
||||
orderCurrencyImdg.insert(counterTransfer);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue