Merge branch 'glms/feature/dotnumber' into glms/feature/1.4.2

# Conflicts:
#	YLErpWeb/Views/SwapTrade2/TradeEdit.cshtml
#	YLErpWeb/wwwroot/Scripts/app/swaptrade/swapTradeEdit.js
This commit is contained in:
张名锐
2026-07-23 17:53:37 +08:00
22 changed files with 319 additions and 109 deletions
@@ -137,7 +137,13 @@ namespace YLErp.Modules.SwapModule
swapFlow.OptTime = result.OptTime;
swapFlow.SettleDate = result.SettleDate;
swapFlow.TradingAmount = result.TradingAmount;
swapFlow.TradingAmountAvg = result.TradingAmountAvg;
var underlying = string.IsNullOrEmpty(result.UnderlyingCode)
? null
: DataCacheProvider.GetUnderlyingDataSource().GetData(result.UnderlyingCode);
var storagePriceRound = underlying?.IsBond() == true
? ConsGlobal.PriceRound
: ConsGlobal.SwapDeliveryPriceRound;
swapFlow.TradingAmountAvg = Math.Round(result.TradingAmountAvg, storagePriceRound, MidpointRounding.AwayFromZero);
swapFlow.TradingAmountFeeAvg = result.TradingAmountFeeAvg;
swapFlow.TradingAmountNet = result.TradingAmountNet;
swapFlow.TradingAmountNetFee = result.TradingAmountNetFee;
+66 -4
View File
@@ -36,9 +36,61 @@ namespace YLErp.Modules.SwapModule
/// 原 private 改 protected virtual,使测试 stub 可整体 override,规避内部 new SwapEventService 连库。</summary>
protected virtual long SaveSwapDeal(UnwindData unwindData, int eventType, int clientCashId, string eventResason = "", bool approve = false)
{
NormalizeNotionalValues(unwindData);
NormalizeDeliveryPrices(unwindData.FlowEvents);
return SaveSwapDealInternal(unwindData, eventType, clientCashId, eventResason, approve);
}
private static void NormalizeNotionalValues(UnwindData unwindData)
{
unwindData.NotionalValue = Math.Round(unwindData.NotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
unwindData.PosiNotionalValue = Math.Round(unwindData.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
unwindData.CloseNotionalValue = Math.Round(unwindData.CloseNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
}
private static int GetStorageDeliveryPriceRound(swap_flow_event flowEvent)
{
if (ConsGlobal.InstrumentType.IsBond(flowEvent?.UnderlyingInstrumentType))
{
return ConsGlobal.PriceRound;
}
if (string.IsNullOrEmpty(flowEvent?.UnderlyingCode))
{
return ConsGlobal.SwapDeliveryPriceRound;
}
var underlying = DataCacheProvider.GetUnderlyingDataSource().GetData(flowEvent?.UnderlyingCode);
return underlying?.IsBond() == true ? ConsGlobal.PriceRound : ConsGlobal.SwapDeliveryPriceRound;
}
private static void ValidateDeliveryPrices(UnwindData unwindData)
{
if (unwindData.FlowEvents == null)
{
return;
}
foreach (var item in unwindData.FlowEvents.Where(x => !string.IsNullOrEmpty(x.UnderlyingCode)))
{
var roundedPrice = Math.Round(item.TradingAmountAvg, GetStorageDeliveryPriceRound(item), MidpointRounding.AwayFromZero);
if (item.TradingAmountAvg != roundedPrice)
{
throw new ServiceException($"期末交割价最多保留{ConsGlobal.SwapDeliveryPriceRound}位小数");
}
item.TradingAmountAvg = roundedPrice;
}
}
private static void NormalizeDeliveryPrices(IEnumerable<swap_flow_event> flowEvents)
{
if (flowEvents == null)
{
return;
}
foreach (var item in flowEvents.Where(x => !string.IsNullOrEmpty(x.UnderlyingCode)))
{
item.TradingAmountAvg = Math.Round(item.TradingAmountAvg, GetStorageDeliveryPriceRound(item), MidpointRounding.AwayFromZero);
}
}
/// <summary>保存所有变更(生产: DbContext.SaveChanges;测试: 空操作)</summary>
protected virtual void SaveAllChanges()
{
@@ -1222,6 +1274,8 @@ namespace YLErp.Modules.SwapModule
{
throw new ServiceException("未找到交易信息");
}
ValidateDeliveryPrices(unwindData);
NormalizeNotionalValues(unwindData);
//CheckLastEod(unwindData.ValueDate, td.StartDate.Value, unwindData.SwapTradeId); //去掉平仓收盘限制
ValidateFrontendPnL(unwindData, isIncome: false); // 只读校验告警,不阻断交易
// 前端按"占期初(original)"语义传 ClosePercent(A);后端全链路按"占剩余(remaining)"语义(B)消费。
@@ -1252,7 +1306,7 @@ namespace YLErp.Modules.SwapModule
td.HasPartialUnWind = 1;
}
td.UnWindDate = unwindData.UnwindDate;
td.StockEqvNotional -= Convert.ToDouble(unwindData.CloseNotionalValue);
td.StockEqvNotional = Math.Round(td.StockEqvNotional - Convert.ToDouble(unwindData.CloseNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
td.TradeAmount -= Convert.ToDouble(unwindData.CloseQty);
SaveAllChanges();
cofirm = true;
@@ -1278,6 +1332,10 @@ namespace YLErp.Modules.SwapModule
var tradeExtend = DbContext.trade_extend.FirstOrDefault(x => x.TradeId == td.id);
td.trade_extend = tradeExtend;
var position = positions.Where(x => !string.IsNullOrEmpty(x.UnderlyingCode) && !x.IsInitial).FirstOrDefault();
var storagePriceRound = ConsGlobal.InstrumentType.IsBond(position?.UnderlyingInstrumentType)
? ConsGlobal.PriceRound
: ConsGlobal.SwapDeliveryPriceRound;
unwindPrice = Math.Round(unwindPrice, storagePriceRound, MidpointRounding.AwayFromZero);
var preDealDate = GetPreDealDate(td.id, dealDate, eventTypes);
swap_flow_event floatEvent = new swap_flow_event();
UnwindData unwindData = new UnwindData();
@@ -1568,7 +1626,7 @@ namespace YLErp.Modules.SwapModule
td.HasPartialUnWind = 1;
}
td.UnWindDate = unwindData.UnwindDate;
td.StockEqvNotional -= Convert.ToDouble(unwindData.CloseNotionalValue);
td.StockEqvNotional = Math.Round(td.StockEqvNotional - Convert.ToDouble(unwindData.CloseNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
td.TradeAmount -= Convert.ToDouble(unwindData.CloseQty);
td.Notional = td.TradeAmount;
td.OptDate = DateTime.Now;
@@ -1701,6 +1759,7 @@ namespace YLErp.Modules.SwapModule
{
throw new ServiceException("未找到交易信息");
}
ValidateDeliveryPrices(unwindData);
NormalizeIncomeUnwindDate(unwindData);
ValidateIncomeValueDate(unwindData, td);
//CheckLastEod(unwindData.ValueDate, td.StartDate.Value, unwindData.SwapTradeId); //去掉平仓收盘限制
@@ -1741,12 +1800,14 @@ namespace YLErp.Modules.SwapModule
throw new Exception("该笔交易状态为平仓待复核,未找到相关记录,请检查该笔交易是否有效");
}
swapEvent.unwindData = JsonConvert.DeserializeObject<UnwindData>(swapEvent.EventData);
NormalizeDeliveryPrices(swapEvent.unwindData.FlowEvents);
if (eventType == (int)SwapEventTypeEnum.)
{
NormalizeIncomeUnwindDate(swapEvent.unwindData);
ValidateIncomeValueDate(swapEvent.unwindData, td);
}
var flowList = FindFlowEventsByEventId(swapEvent.id);
NormalizeDeliveryPrices(flowList);
string action = eventType == (int)SwapEventTypeEnum. ? ClientCashInCashOut._互换 : ClientCashInCashOut._平仓费;
int clientCashId = AddClientCash(td, Convert.ToDouble(-swapEvent.unwindData.SwapRealizedPnL), action, swapEvent.unwindData.ValueDate);
if (swapEvent.unwindData.SwapMarginAmount != 0)
@@ -1767,7 +1828,7 @@ namespace YLErp.Modules.SwapModule
td.UnWindDate = swapEvent.unwindData.UnwindDate;
if (eventType != (int)SwapEventTypeEnum.)
{
td.StockEqvNotional -= Convert.ToDouble(swapEvent.unwindData.CloseNotionalValue);
td.StockEqvNotional = Math.Round(td.StockEqvNotional - Convert.ToDouble(swapEvent.unwindData.CloseNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
td.TradeAmount -= Convert.ToDouble(swapEvent.unwindData.CloseQty);
}
@@ -1789,6 +1850,7 @@ namespace YLErp.Modules.SwapModule
{
throw new ServiceException("未找到交易信息");
}
ValidateDeliveryPrices(unwindData);
if (eventType == (int)SwapEventTypeEnum.)
{
NormalizeIncomeUnwindDate(unwindData);
@@ -1933,7 +1995,7 @@ namespace YLErp.Modules.SwapModule
{
// 平仓时才扣减持仓
position.PosiQuantity -= unwindData.CloseQty;
position.PosiNotionalValue -= unwindData.CloseNotionalValue;
position.PosiNotionalValue = Math.Round(position.PosiNotionalValue - unwindData.CloseNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
position.PosiTradingFee -= position.PosiTradingFee * unwindData.ClosePercent;
position.PosiTradingFeePending -= position.PosiTradingFeePending * unwindData.ClosePercent;
}
@@ -32,11 +32,30 @@ namespace YLErp.Modules.SwapModule
}
private int GetStorageDeliveryPriceRound(string underlyingInstrumentType, string underlyingCode)
{
if (ConsGlobal.InstrumentType.IsBond(underlyingInstrumentType))
{
return ConsGlobal.PriceRound;
}
if (string.IsNullOrEmpty(underlyingCode))
{
return ConsGlobal.SwapDeliveryPriceRound;
}
return GetUnderlyingData(underlyingCode)?.IsBond() == true
? ConsGlobal.PriceRound
: ConsGlobal.SwapDeliveryPriceRound;
}
#region Seamsoverride DB/
/// <summary>持久化 eod 持仓记录(生产: DbContext.Add;测试: 收集到列表)</summary>
protected virtual void PersistEodSwapPosition(eod_swap_position position)
{
var storagePriceRound = GetStorageDeliveryPriceRound(position.UnderlyingInstrumentType, position.UnderlyingCode);
position.PosiGrossPrice = Math.Round(position.PosiGrossPrice, storagePriceRound, MidpointRounding.AwayFromZero);
position.UnderlyingPrice = Math.Round(position.UnderlyingPrice, storagePriceRound, MidpointRounding.AwayFromZero);
position.PosiNotionalValue = Math.Round(position.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
if (position.id == 0)
{
DbContext.eod_swap_position.Add(position);
@@ -156,34 +175,18 @@ namespace YLErp.Modules.SwapModule
return UnderlyingCodePrice(code, settleDate, out vobp);
}
/// <summary>
/// 获取用于互换浮动腿盯市的标的价格。
///
/// 普通债券类收益互换的新录入页面将全价按小数保存,例如页面录入 20% 后
/// PosiGrossPrice 为 0.2;而历史交易中仍可能存在直接保存为 20 的展示态价格。
/// 中债估值正常经 EodPriceQueryService 转换后应为小数价格,但手工维护的历史
/// 行情可能仍以展示态进入该服务,例如 2000 经一次转换后得到 20。若将 20
/// 与 0.2 直接相减,会把 20% 的价格差误算成 1,980,000 的浮动损益。
///
/// 因此仅当交易期初价已经是小数口径、且当前债券价明显仍处于展示态时,再做
/// 一次展示态到存储态转换。期初价本身是历史展示态口径的存量交易保持原价格,
/// 避免修改日终估值链路后改变其既有损益。
/// </summary>
private decimal GetSwapValuationPrice(string code, decimal posiGrossPrice, DateTime settleDate, out decimal vobp)
/// <summary>获取用于互换浮动腿盯市的标的价格。</summary>
private decimal GetSwapValuationPrice(string code, DateTime settleDate, out decimal vobp)
{
var price = GetUnderlyingPrice(code, settleDate, out vobp);
var underlying = GetUnderlyingData(code);
var usesStoragePrice = Math.Abs(posiGrossPrice) < 2m;
var usesDisplayPrice = Math.Abs(price) >= 10m;
if (underlying?.IsBond() == true && usesStoragePrice && usesDisplayPrice)
if (underlying?.IsBond() == true)
{
var normalizedPrice = BondPriceConverter.ToStorage(price);
Log.Error($"互换债券日终价格按展示态返回,已转换为存储态: UnderlyingCode={code}, ValueDate={settleDate:yyyy-MM-dd}, PosiGrossPrice={posiGrossPrice}, SourcePrice={price}, NormalizedPrice={normalizedPrice}");
return normalizedPrice;
return Math.Round(price, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
}
return price;
return Math.Round(price, ConsGlobal.SwapDeliveryPriceRound, MidpointRounding.AwayFromZero);
}
/// <summary>获取标的缓存数据(生产: DataCacheProvider;测试: 返回内存对象)</summary>
@@ -1472,7 +1475,10 @@ namespace YLErp.Modules.SwapModule
newEodPayPosition.ContractSize = eventFlow.ContractSize;
newEodPayPosition.CountRatio = eventFlow.CountRatio;
newEodPayPosition.PosiNetPrice = netPrice;
newEodPayPosition.PosiGrossPrice = grossPrice;
newEodPayPosition.PosiGrossPrice = Math.Round(
grossPrice,
GetStorageDeliveryPriceRound(eventFlow.UnderlyingInstrumentType, eventFlow.UnderlyingCode),
MidpointRounding.AwayFromZero);
newEodPayPosition.PosiNetFeePrice = netFeePrice;
newEodPayPosition.PosiNetNoFeePrice = netNoFeePrice;
newEodPayPosition.PosiQuantity = payQty;
@@ -1557,7 +1563,7 @@ namespace YLErp.Modules.SwapModule
int shortRatio = eod.PositionType == (int)PositionTypeFlag.Long ? 1 : -1;
int directionRatio = eod.PosiDirection == (int)SwapDirectionEnum. ? 1 : -1;
curretEod.PosiStatus = curretEod.PosiQuantity == 0 ? 1 : 0;
var price = GetSwapValuationPrice(eod.UnderlyingCode, eod.PosiGrossPrice, dealDate, out decimal vobp);
var price = GetSwapValuationPrice(eod.UnderlyingCode, dealDate, out decimal vobp);
curretEod.dv01 = Dv01Helper.CalcDv01(eod.UnderlyingCode, curretEod.PosiQuantity, eod.PosiDirection, eod.PositionType, vobp);
decimal tax = um.ValueAddedTax ?? 0;
if (valueDate > td.StartDate.Value && curretEod.PosiQuantity > 0)
@@ -1649,7 +1655,7 @@ namespace YLErp.Modules.SwapModule
var dealDate = curretEod.ValueDate;
int shortRatio = eod.PositionType == (int)PositionTypeFlag.Long ? 1 : -1;
int directionRatio = eod.PosiDirection == (int)SwapDirectionEnum. ? 1 : -1;
var price = GetSwapValuationPrice(eod.UnderlyingCode, eod.PosiGrossPrice, dealDate, out decimal vobp);
var price = GetSwapValuationPrice(eod.UnderlyingCode, dealDate, out decimal vobp);
var todayConsumedDividend = CalcConsumedDividend(curretEod, unwindEvents);
var originNotional = (decimal)td.OriginalStockEqvNotional / swapPosition.PosiNetPrice;
decimal totalPayment = CalcBondPayment(curretEod.UnderlyingCode, td.StartDate.Value, valueDate, (decimal)originNotional, shortRatio, directionRatio);
@@ -1771,7 +1777,10 @@ namespace YLErp.Modules.SwapModule
posiQty = 0;
}
curretEod.PosiGrossPrice = (eod.PosiGrossPrice * eod.PosiQuantity + openFlowEvents.Sum(a => a.Quantity * a.TradingAmountAvg)) / (eod.PosiQuantity + openQty);
curretEod.PosiGrossPrice = Math.Round(curretEod.PosiGrossPrice, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
curretEod.PosiGrossPrice = Math.Round(
curretEod.PosiGrossPrice,
GetStorageDeliveryPriceRound(curretEod.UnderlyingInstrumentType, curretEod.UnderlyingCode),
MidpointRounding.AwayFromZero);
curretEod.PosiNetPrice = (eod.PosiNetPrice * eod.PosiQuantity + openFlowEvents.Sum(a => a.Quantity * a.TradingAmountFeeAvg)) / (eod.PosiQuantity + openQty);
curretEod.PosiNetPrice = Math.Round(curretEod.PosiNetPrice, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
curretEod.PosiNetNoFeePrice = (eod.PosiNetNoFeePrice * eod.PosiQuantity + openFlowEvents.Sum(a => a.Quantity * a.TradingAmountNetAvg)) / (eod.PosiQuantity + openQty);
@@ -1835,7 +1844,7 @@ namespace YLErp.Modules.SwapModule
curretEod.ContractSize = position.ContractSize;
curretEod.CountRatio = position.CountRatio;
curretEod.PosiTradingFee = position.PosiTradingFee;
curretEod.UnderlyingPrice = GetSwapValuationPrice(position.UnderlyingCode, position.PosiGrossPrice, dealDate, out decimal vobp);
curretEod.UnderlyingPrice = GetSwapValuationPrice(position.UnderlyingCode, dealDate, out decimal vobp);
SetPriceInfoByFlowEvent(eod, curretEod, unwindEvents, position);
curretEod.dv01 = Dv01Helper.CalcDv01(curretEod.UnderlyingCode, curretEod.PosiQuantity, curretEod.PosiDirection, curretEod.PositionType, vobp);
//if (settleDate == td.TradeDate)
@@ -1897,14 +1906,14 @@ namespace YLErp.Modules.SwapModule
}
if (data.IsBond())
{
return BondPrice(data, settleDate, out vobp);
return Math.Round(BondPrice(data, settleDate, out vobp), ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
}
var price = data.Price ?? 0;
if (EodPriceQueryService.TryGetEodPrice(settleDate, code, out var eodPrice))
{
price = eodPrice.GetPrice(SettlementTypeEnum.ClosePrice);
}
return Convert.ToDecimal(price);
return Math.Round(Convert.ToDecimal(price), ConsGlobal.SwapDeliveryPriceRound, MidpointRounding.AwayFromZero);
}
/// <summary>
/// 获取债券收盘价格
@@ -1949,9 +1958,9 @@ namespace YLErp.Modules.SwapModule
var positions = eodSwapPositions.Where(x => !string.IsNullOrEmpty(x.UnderlyingCode)).ToList();//持仓腿
// 框架合约的方向约定:多头为正、空头为负;总名义本金取交易原始规模,
// 不能直接用多空腿相加,否则会把对冲方向误当成合约规模变化。
eod_Swap.NotionalValueLong = positions.Where(x => x.PositionType == (int)PositionTypeFlag.Long).Sum(s => s.PosiNotionalValue);
eod_Swap.NotionalValueShort = -Math.Abs(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Short).Sum(s => s.PosiNotionalValue));
eod_Swap.NotionalValue = Convert.ToDecimal(td.OriginalStockEqvNotional ?? td.StockEqvNotional);
eod_Swap.NotionalValueLong = Math.Round(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Long).Sum(s => s.PosiNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.NotionalValueShort = Math.Round(-Math.Abs(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Short).Sum(s => s.PosiNotionalValue)), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.NotionalValue = Math.Round(Convert.ToDecimal(td.OriginalStockEqvNotional ?? td.StockEqvNotional), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.SwapTradeId = td.id;
eod_Swap.SwapTradeNo = td.TradeNumber;
eod_Swap.ClientId = td.ClientId;
@@ -2026,9 +2035,9 @@ namespace YLErp.Modules.SwapModule
var eodSwapPositions = DbContext.eod_swap_position.Where(x => x.SwapTradeId == td.id && x.ValueDate == settleDate && !x.Invalid).ToList();
var interestPositions = eodSwapPositions.Where(x => string.IsNullOrEmpty(x.UnderlyingCode)).ToList();//利息腿
var positions = eodSwapPositions.Where(x => !string.IsNullOrEmpty(x.UnderlyingCode)).ToList();//持仓腿
eod_Swap.NotionalValue = Convert.ToDecimal(td.OriginalStockEqvNotional ?? td.StockEqvNotional);
eod_Swap.NotionalValueLong = positions.Where(x => x.PositionType == (int)PositionTypeFlag.Long).Sum(s => s.PosiNotionalValue);
eod_Swap.NotionalValueShort = -Math.Abs(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Short).Sum(s => s.PosiNotionalValue));
eod_Swap.NotionalValue = Math.Round(Convert.ToDecimal(td.OriginalStockEqvNotional ?? td.StockEqvNotional), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.NotionalValueLong = Math.Round(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Long).Sum(s => s.PosiNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.NotionalValueShort = Math.Round(-Math.Abs(positions.Where(x => x.PositionType == (int)PositionTypeFlag.Short).Sum(s => s.PosiNotionalValue)), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
eod_Swap.MarketValueLong = positions.Where(x => x.PositionType == (int)PositionTypeFlag.Long).Sum(s => s.UnderlyingMarketValue);
eod_Swap.MarketValueShort = positions.Where(x => x.PositionType == (int)PositionTypeFlag.Short).Sum(s => s.UnderlyingMarketValue);
eod_Swap.FloatingPnL = positions.Sum(s => s.PosiProfitSum);
@@ -2318,24 +2327,23 @@ namespace YLErp.Modules.SwapModule
item.PeriodPaymentValuation = item.FloatingUnrealizedPnl + item.position.InterestPnL;
}
// eod_swap 的保证金本金来自 trade_span;缺少 span 数据时会被保存为 0。
// 本风险页改按日终保证金腿展示,且该页面保证金本金采用原始本金的 1/10 口径
// 利息仍使用原始本金累积值,不能同步缩放,否则会破坏保证金利息金额。
// 本风险页改按日终保证金腿的实际本金展示。
item.position.InitMarginGain = marginLegs
.Where(x => x.InterestMode == (int)InterestModeEnum.
&& x.InterestDirection == (int)SwapDirectionEnum.)
.Sum(x => Math.Abs(x.InterestPrincipalFix)) / 10m;
.Sum(x => Math.Abs(x.InterestPrincipalFix));
item.position.InitMarginLoss = marginLegs
.Where(x => x.InterestMode == (int)InterestModeEnum.
&& x.InterestDirection == (int)SwapDirectionEnum.)
.Sum(x => Math.Abs(x.InterestPrincipalFix)) / 10m;
.Sum(x => Math.Abs(x.InterestPrincipalFix));
item.position.PostionMarginGain = marginLegs
.Where(x => x.InterestMode == (int)InterestModeEnum.
&& x.InterestDirection == (int)SwapDirectionEnum.)
.Sum(x => Math.Abs(x.InterestPrincipalFix)) / 10m;
.Sum(x => Math.Abs(x.InterestPrincipalFix));
item.position.PostionMarginLoss = marginLegs
.Where(x => x.InterestMode == (int)InterestModeEnum.
&& x.InterestDirection == (int)SwapDirectionEnum.)
.Sum(x => Math.Abs(x.InterestPrincipalFix)) / 10m;
.Sum(x => Math.Abs(x.InterestPrincipalFix));
// 保证金本金方向与我方的利息现金流方向相反:原始“收取”保证金
// 表示我方占用客户资金,应向客户支付利息;支付金额按负数展示。
@@ -51,6 +51,21 @@ namespace YLErp.Modules.SwapModule
protected virtual underlying_manager GetUnderlying(string underlyingCode)
=> DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode);
private int GetStorageDeliveryPriceRound(string underlyingInstrumentType, string underlyingCode)
{
if (ConsGlobal.InstrumentType.IsBond(underlyingInstrumentType))
{
return ConsGlobal.PriceRound;
}
if (string.IsNullOrEmpty(underlyingCode))
{
return ConsGlobal.SwapDeliveryPriceRound;
}
return GetUnderlying(underlyingCode)?.IsBond() == true
? ConsGlobal.PriceRound
: ConsGlobal.SwapDeliveryPriceRound;
}
protected virtual DateTime GetNextBusinessDay(DateTime date)
=> QdpCalendarHelper.GetNonHoliday(date);
@@ -61,6 +76,13 @@ namespace YLErp.Modules.SwapModule
{
foreach (var evt in events)
{
if (!string.IsNullOrEmpty(evt.UnderlyingCode))
{
evt.TradingAmountAvg = Math.Round(
evt.TradingAmountAvg,
GetStorageDeliveryPriceRound(evt.UnderlyingInstrumentType, evt.UnderlyingCode),
MidpointRounding.AwayFromZero);
}
DbContext.swap_flow_event.Add(evt);
}
DbContext.SaveChanges();
@@ -321,7 +343,7 @@ namespace YLErp.Modules.SwapModule
DataState = 1,
EventDate = flow_merge.OccurTime,
UnwindDate = QdpCalendarHelper.GetNonHoliday(flow_merge.OccurTime.AddDays(1)),
TradingAmountAvg = TradingAmountAvg,
TradingAmountAvg = Math.Round(TradingAmountAvg, GetStorageDeliveryPriceRound(underlyingInstrumentType, flow_merge.UnderlyingCode), MidpointRounding.AwayFromZero),
TradingAmountFeeAvg = TradingAmountFeeAvg,
TradingAmountNetFeeAvg = TradingAmountNetFeeAvg,
TradingAmountNetAvg = flow_merge.TradingAmountNetAvg,
@@ -375,7 +397,10 @@ namespace YLErp.Modules.SwapModule
DataState = 1,
EventDate = flow_merge.OccurTime,
UnwindDate = QdpCalendarHelper.GetNonHoliday(flow_merge.OccurTime.AddDays(1)),
TradingAmountAvg = flow_merge.TradingAmountAvg,
TradingAmountAvg = Math.Round(
flow_merge.TradingAmountAvg,
GetStorageDeliveryPriceRound(null, flow_merge.UnderlyingCode),
MidpointRounding.AwayFromZero),
TradingAmountFeeAvg = flow_merge.TradingAmountFeeAvg,
TradingFeePending = flow_merge.TradingFeePending,
ClientId = flow_merge.ClientId
@@ -409,7 +434,10 @@ namespace YLErp.Modules.SwapModule
DataState = (int)SwapFlowDateStateEnum.,
EventDate = td.TradeDate.Value,
UnwindDate = td.StartDate.Value,
TradingAmountAvg = position.PosiGrossPrice,
TradingAmountAvg = Math.Round(
position.PosiGrossPrice,
GetStorageDeliveryPriceRound(position.UnderlyingInstrumentType, position.UnderlyingCode),
MidpointRounding.AwayFromZero),
TradingAmountFeeAvg = position.PosiNetPrice,
TradingAmountNetFeeAvg = position.PosiNetFeePrice,
TradingAmountNetAvg = position.PosiNetNoFeePrice,
@@ -462,7 +490,7 @@ namespace YLErp.Modules.SwapModule
DataState = 100,
EventDate = td.TradeDate.Value,
UnwindDate = QdpCalendarHelper.GetNonHoliday(td.TradeDate.Value.AddDays(1)),
TradingAmountAvg = flowMerge.TradingAmountAvg,
TradingAmountAvg = Math.Round(flowMerge.TradingAmountAvg, GetStorageDeliveryPriceRound(underlyingInstrumentType, flowMerge.UnderlyingCode), MidpointRounding.AwayFromZero),
TradingAmountFeeAvg = flowMerge.TradingAmountFeeAvg,
TradingAmountNetFeeAvg = flowMerge.TradingAmountNetFeeAvg,
TradingAmountNetAvg = flowMerge.TradingAmountNetAvg,
@@ -118,7 +118,8 @@ namespace YLErp.Modules.SwapModule
swap_flow.ytm = reader.GetDecimalOrPercent("成交收益率",false,true) ?? 0;
swap_flow.TradingAmountNet = reader.GetDecimal("成交净价") ?? 0;
swap_flow.TradingAmountNetFee = TradeFeeHelper.CalcPriceWithFee(swap_flow.TradingFee, swap_flow.TradingAmountNet??0, swap_flow.TradingQty, swap_flow.BsType);
if (underlying != null && underlying.IsBond())
var isBond = underlying != null && underlying.IsBond();
if (isBond)
{
// 成交流水债券报价(×100形式)转入库小数(×0.01),统一走 BondPriceConverter
swap_flow.TradingAmountAvg = BondPriceConverter.ToStorage(swap_flow.TradingAmountAvg);
@@ -129,6 +130,10 @@ namespace YLErp.Modules.SwapModule
if (swap_flow.TradingAmountNetFee.HasValue)
swap_flow.TradingAmountNetFee = BondPriceConverter.ToStorage(swap_flow.TradingAmountNetFee.Value);
}
swap_flow.TradingAmountAvg = Math.Round(
swap_flow.TradingAmountAvg,
isBond ? ConsGlobal.PriceRound : ConsGlobal.SwapDeliveryPriceRound,
MidpointRounding.AwayFromZero);
if (!string.IsNullOrEmpty(clientName))
{
var client = DataCacheProvider.GetClientDataSource().AsQueryable(x=>x.Name== clientName).FirstOrDefault();
+12 -2
View File
@@ -695,7 +695,7 @@ namespace YLErp.Modules.SwapModule
TradingFee = gourpItem.Sum(s => s.TradingFee),
DataState = (int)SwapFlowDateStateEnum.,
TradingAmountFeeAvg = gourpItem.Average(s => s.TradingAmountFeeAvg),
TradingAmountAvg = gourpItem.Average(s => s.TradingAmountAvg),
TradingAmountAvg = Math.Round(gourpItem.Average(s => s.TradingAmountAvg), ConsGlobal.PriceRound, MidpointRounding.AwayFromZero),
ContractSize = swapflow.ContractSize
};
UpdateDbOption(swap_flow_summary);
@@ -747,12 +747,18 @@ namespace YLErp.Modules.SwapModule
private void CheckValid(swap_flow req)
{
CheckRequired(req);
var roundedPrice = Math.Round(req.TradingAmountAvg, ConsGlobal.SwapDeliveryPriceRound, MidpointRounding.AwayFromZero);
if (req.TradingAmountAvg != roundedPrice)
{
throw new ServiceException($"成交全价最多保留{ConsGlobal.SwapDeliveryPriceRound}位小数");
}
var underlying = DataCacheProvider.GetUnderlyingDataSource().GetData(req.UnderlyingCode);
if (underlying == null)
{
throw new ServiceException("没有找到标的信息:" + req.UnderlyingCode);
}
if (underlying != null && underlying.IsBond())
var isBond = underlying.IsBond();
if (isBond)
{
// 债券报价(×100)转入库小数(×0.01),价格字段统一走 BondPriceConverter
req.TradingAmountAvg = BondPriceConverter.ToStorage(req.TradingAmountAvg);
@@ -764,6 +770,10 @@ namespace YLErp.Modules.SwapModule
// 数量×100(万手→手),与价格维度无关,保留常量
req.TradingQty *= ConsGlobal.bondShowPriceMultiple;
}
req.TradingAmountAvg = Math.Round(
req.TradingAmountAvg,
isBond ? ConsGlobal.PriceRound : ConsGlobal.SwapDeliveryPriceRound,
MidpointRounding.AwayFromZero);
}
@@ -154,7 +154,7 @@ namespace YLErp.Modules.SwapModule
swapFlow.DataState = (int)SwapFlowDateStateEnum.;
}
// 债券报价(×100)转入库小数(×0.01),统一走 BondPriceConverter
swapFlow.TradingAmountAvg = BondPriceConverter.ToStorage(item.deal_full_price ?? 0);
swapFlow.TradingAmountAvg = Math.Round(BondPriceConverter.ToStorage(item.deal_full_price ?? 0), ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
swapFlow.TradingAmountFeeAvg = BondPriceConverter.ToStorage(item.deal_full_price_include_fee ?? 0);
swapFlow.TradingAmount = swapFlow.TradingQty * swapFlow.ContractSize * swapFlow.TradingAmountAvg;
swapFlow.ClientId = Convert.ToInt32(item.client_id ?? 0);
@@ -508,6 +508,7 @@ namespace YLErp.Modules.SwapModule
swap_flow_summary.SettleDate = gourpItem.Max(s => s.SettleDate);
swap_flow_summary.TradingAmount = swap_flow_summary.TradingQty * swap_flow_summary.ContractSize;
swap_flow_summary.TradingAmountAvg = swap_flow_summary.TradingQty == 0 ? 0 : gourpItem.Sum(s => s.FullPrice * s.TradingQty) / swap_flow_summary.TradingQty;
swap_flow_summary.TradingAmountAvg = Math.Round(swap_flow_summary.TradingAmountAvg, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
swap_flow_summary.TradingAmountFeeAvg = swap_flow_summary.TradingQty == 0 ? swap_flow_summary.TradingAmountAvg : swap_flow_summary.TradingAmountAvg + swap_flow_summary.TradingFeePending * tradeSide / swap_flow_summary.TradingQty;
swap_flow_summary.TradingAmountNetAvg = swap_flow_summary.TradingQty == 0 ? 0 : gourpItem.Sum(s => s.NetPrice * s.TradingQty) / swap_flow_summary.TradingQty;
swap_flow_summary.TradingAmountNetFeeAvg = swap_flow_summary.TradingQty == 0 ? swap_flow_summary.TradingAmountNetAvg : swap_flow_summary.TradingAmountNetAvg + swap_flow_summary.TradingFeePending * tradeSide / swap_flow_summary.TradingQty;
@@ -1081,7 +1082,7 @@ namespace YLErp.Modules.SwapModule
var ratio = flowMergeClone.BsType == 1 ? 1 : -1;
var oriRatio = flowMergeClone.BsType == 1 ? -1 : 1;
flowMergeClone.TradingFeePending = flowMergeClone.TradingQty / origin.TradingQty * origin.TradingFeePending;
flowMergeClone.TradingAmountAvg = origin.TradingAmountAvg + oriRatio * origin.TradingFeePending * 2 / origin.TradingQty;
flowMergeClone.TradingAmountAvg = Math.Round(origin.TradingAmountAvg + oriRatio * origin.TradingFeePending * 2 / origin.TradingQty, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
flowMergeClone.TradingAmountNetAvg = origin.TradingAmountNetAvg + oriRatio * origin.TradingFeePending * 2 / origin.TradingQty;
flowMergeClone.TradingAmountFeeAvg = flowMergeClone.TradingAmountAvg + ratio * flowMergeClone.TradingFeePending / flowMergeClone.TradingQty;
@@ -62,7 +62,7 @@ namespace YLErp.Modules.SwapModule
position.PosiGrossPrice = eodPayPosition.PosiGrossPrice;
position.PosiNetFeePrice = eodPayPosition.PosiNetFeePrice;
position.PosiNetNoFeePrice = eodPayPosition.PosiNetNoFeePrice;
position.PosiNotionalValue = eodPayPosition.PosiNotionalValue;
position.PosiNotionalValue = Math.Round(eodPayPosition.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
position.PosiQuantity = eodPayPosition.PosiQuantity;
position.PosiStartDate = eodPayPosition.PosiStartDate;
position.OptTime = DateTime.Now;
@@ -85,7 +85,7 @@ namespace YLErp.Modules.SwapModule
position.PosiGrossPrice = eodPayPosition.PosiGrossPrice;
position.PosiNetFeePrice = eodPayPosition.PosiNetFeePrice;
position.PosiNetNoFeePrice = eodPayPosition.PosiNetNoFeePrice;
position.PosiNotionalValue = eodPayPosition.PosiNotionalValue;
position.PosiNotionalValue = Math.Round(eodPayPosition.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
position.PosiTradingFeePending = eodPayPosition.PosiFeePending;
position.PosiQuantity = eodPayPosition.PosiQuantity;
position.PosiDirection = eodPayPosition.PosiDirection;
+56 -21
View File
@@ -51,6 +51,24 @@ namespace YLErp.Modules.SwapModule
{
}
private static decimal ValidateDeliveryPrice(decimal price, string fieldName)
{
var roundedPrice = Math.Round(price, ConsGlobal.SwapDeliveryPriceRound, MidpointRounding.AwayFromZero);
if (price != roundedPrice)
{
throw new ServiceException($"{fieldName}最多保留{ConsGlobal.SwapDeliveryPriceRound}位小数");
}
return roundedPrice;
}
private static decimal? RoundSwapBondNetPriceAndYtm(decimal? value)
{
return value.HasValue
? Math.Round(value.Value, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero)
: null;
}
#region
/// <summary>
/// 新版收益互换预付金校验
@@ -334,7 +352,7 @@ namespace YLErp.Modules.SwapModule
AssetBookName = asset.Name,
Notional = Convert.ToDouble(flowMerge.TradingQtyAbs),
TradeAmount = Convert.ToDouble(flowMerge.TradingQtyAbs),
StockEqvNotional = Convert.ToDouble(flowMerge.TradingAmount),
StockEqvNotional = Math.Round(Convert.ToDouble(flowMerge.TradingAmount), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero),
IsAutoGenerate = true,
};
if (flowMerge.SettleDate.HasValue)
@@ -352,7 +370,7 @@ namespace YLErp.Modules.SwapModule
td.ValidState = "Valid";
td.TradeSource = "系统交易";
td.TradeStatus = ConsTrade.;
td.InitYtm = flowMerge.InitYtm ?? 0;
td.InitYtm = RoundSwapBondNetPriceAndYtm(flowMerge.InitYtm) ?? 0;
return td;
}
/// <summary>
@@ -372,11 +390,14 @@ namespace YLErp.Modules.SwapModule
CountRatio = underlying.CountRatio,
ContractSize = Convert.ToDecimal(underlying.ContractSize),
PosiNetPrice = flowMerge.TradingAmountFeeAvgAbs,
PosiGrossPrice = flowMerge.TradingAmountAvg,
PosiGrossPrice = Math.Round(
flowMerge.TradingAmountAvg,
underlying.IsBond() ? ConsGlobal.PriceRound : ConsGlobal.SwapDeliveryPriceRound,
MidpointRounding.AwayFromZero),
PosiNetFeePrice = flowMerge.TradingAmountNetFeeAvg ?? 0,
PosiNetNoFeePrice = flowMerge.TradingAmountNetAvg ?? 0,
PosiQuantity = flowMerge.TradingQtyAbs,
PosiNotionalValue = flowMerge.TradingAmount,
PosiNotionalValue = Math.Round(flowMerge.TradingAmount, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero),
PosiTradingFeePending = flowMerge.TradingFeePending,
PosiTradingFee = 0,
PosiTradingFeeUnit = 0,
@@ -388,7 +409,7 @@ namespace YLErp.Modules.SwapModule
OptId = UserInfo.UserId,
OptName = UserInfo.UserName,
UnderlyingInstrumentType = underlying.UnderlyingInstrumentType,
InitYtm = flowMerge.InitYtm
InitYtm = RoundSwapBondNetPriceAndYtm(flowMerge.InitYtm)
};
td.swap_positions.Add(floatPosition);
swap_position interestPosition = new swap_position()
@@ -581,7 +602,7 @@ namespace YLErp.Modules.SwapModule
dbTrade.trade_extend = req.trade_extend;
dbTrade.swap_positions = req.swap_positions;
dbTrade.MetaDic = req.MetaDic;
dbTrade.InitYtm = req.swap_positions.FirstOrDefault(p => p.InitYtm != null)?.InitYtm;
dbTrade.InitYtm = RoundSwapBondNetPriceAndYtm(req.swap_positions.FirstOrDefault(p => p.InitYtm != null)?.InitYtm);
InnerSaveTrade(false, dbTrade, changsStr, changeConfirmStatus);
return dbTrade;
@@ -675,11 +696,7 @@ namespace YLErp.Modules.SwapModule
private bool PrepareTrade(trade req, TradeSourceEnum dataSource, underlying_manager um)
{
bool tradeNumberGenerated = false;
req.InitialMargin = Convert.ToDouble(req.trade_Initial_Margin.MarginValue);
if (req.trade_Initial_Margin.MarginType == 0)
{
req.InitialMargin = req.StockEqvNotional == 0 ? 0 : Convert.ToDouble(req.trade_Initial_Margin.MarginValue) * req.StockEqvNotional;
};
PrepareInitialMargin(req);
var isAddNew = req.id == 0;
if (isAddNew)
{
@@ -722,6 +739,17 @@ namespace YLErp.Modules.SwapModule
return tradeNumberGenerated;
}
private static void PrepareInitialMargin(trade req)
{
// 初始预付金依赖最终入库的名义本金,须先统一金额精度,避免两者无法勾稽。
req.StockEqvNotional = Math.Round(req.StockEqvNotional, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
req.InitialMargin = Convert.ToDouble(req.trade_Initial_Margin.MarginValue);
if (req.trade_Initial_Margin.MarginType == 0)
{
req.InitialMargin = req.StockEqvNotional == 0 ? 0 : Convert.ToDouble(req.trade_Initial_Margin.MarginValue) * req.StockEqvNotional;
}
}
//准备单个交易
private trade PrepareSingleTrade(trade req, TradeSourceEnum dataSource, bool isAddNew, underlying_manager um)
{
@@ -764,6 +792,7 @@ namespace YLErp.Modules.SwapModule
req.SpotPrice = Convert.ToDouble(swapPosition.PosiNetPrice);
}
req.Strike = null;
req.StockEqvNotional = Math.Round(req.StockEqvNotional, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
req.OriginalStockEqvNotional = req.StockEqvNotional;
req.StockEqvNotionalReal = req.StockEqvNotional;
@@ -1351,13 +1380,19 @@ namespace YLErp.Modules.SwapModule
position.UnderlyingCode = swap.UnderlyingCode;
position.UnderlyingInstrumentType = swap.UnderlyingInstrumentType;
position.PosiDirection = swap.PosiDirection;
position.PosiGrossPrice = swap.PosiGrossPrice;
position.PosiNetPrice = swap.PosiQuantity == 0 ? 0 : (swap.PosiGrossPrice + (position.PosiTradingFeePending / swap.PosiQuantity) * ratio);
// position.PosiGrossPrice = string.IsNullOrEmpty(swap.UnderlyingCode)
// ? Math.Round(swap.PosiGrossPrice, ConsGlobal.SwapDeliveryPriceRound, MidpointRounding.AwayFromZero)
// : ValidateDeliveryPrice(swap.PosiGrossPrice, "期初交割价");
var storagePriceRound = ConsGlobal.InstrumentType.IsBond(swap.UnderlyingInstrumentType)
? ConsGlobal.PriceRound
: ConsGlobal.SwapDeliveryPriceRound;
position.PosiGrossPrice = Math.Round(swap.PosiGrossPrice, storagePriceRound, MidpointRounding.AwayFromZero);
position.PosiNetPrice = swap.PosiQuantity == 0 ? 0 : (position.PosiGrossPrice + (position.PosiTradingFeePending / swap.PosiQuantity) * ratio);
position.PosiNetPrice = Math.Round(position.PosiNetPrice, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
position.PosiNetNoFeePrice = swap.PosiNetNoFeePrice;
position.PosiNetNoFeePrice = RoundSwapBondNetPriceAndYtm(swap.PosiNetNoFeePrice);
position.PosiNetFeePrice = swap.PosiQuantity == 0 ? 0 : (swap.PosiNetNoFeePrice + (position.PosiTradingFeePending / swap.PosiQuantity) * ratio);
position.PosiNetFeePrice = Math.Round(position.PosiNetFeePrice??0, ConsGlobal.PriceRound, MidpointRounding.AwayFromZero);
position.PosiNotionalValue = swap.PosiNotionalValue;
position.PosiNotionalValue = Math.Round(swap.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
position.PosiQuantity = swap.PosiQuantity;
position.InterestDirection = swap.InterestDirection;
position.InterestMode = swap.InterestMode;
@@ -1379,10 +1414,10 @@ namespace YLErp.Modules.SwapModule
position.FloatRateUnderlyingCode = swap.FloatRateUnderlyingCode;
position.interest_rest_days = swap.interest_rest_days;
position.interest_rule = swap.interest_rule;
position.InitYtm = swap.InitYtm;
if (swap.InitYtm != null && swap.InitYtm > 0)
position.InitYtm = RoundSwapBondNetPriceAndYtm(swap.InitYtm);
if (position.InitYtm != null && position.InitYtm > 0)
{
td.InitYtm = swap.InitYtm;
td.InitYtm = position.InitYtm;
}
if (position.id == 0)
@@ -1481,7 +1516,7 @@ namespace YLErp.Modules.SwapModule
td.ProcessStatus = null;
if (backToBegin)
{
td.StockEqvNotional = td.OriginalStockEqvNotional ?? 0;
td.StockEqvNotional = Math.Round(td.OriginalStockEqvNotional ?? 0, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
td.UnWindDate = null;
td.HasPartialUnWind = null;
SingleTradeBackToBegin(td, swapPositions);
@@ -1643,10 +1678,10 @@ namespace YLErp.Modules.SwapModule
posi.PosiGrossPrice = eodPosi.PosiGrossPrice;
posi.PosiNetFeePrice = eodPosi.PosiNetFeePrice;
posi.PosiNetNoFeePrice = eodPosi.PosiNetNoFeePrice;
posi.PosiNotionalValue = eodPosi.PosiNotionalValue;
posi.PosiNotionalValue = Math.Round(eodPosi.PosiNotionalValue, ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
if (posi.PosiDirection > 0)
{
td.StockEqvNotional = Convert.ToDouble(posi.PosiNotionalValue);
td.StockEqvNotional = Math.Round(Convert.ToDouble(posi.PosiNotionalValue), ConsGlobal.MoneyRound, MidpointRounding.AwayFromZero);
td.TradeAmount = Convert.ToDouble(posi.PosiQuantity);
}
}