//using System; //using System.Collections.Generic; //using System.Linq; //using YLErp.BLL.Calculation.V2; //using YLErp.Commons; //using YLErp.DBModels; //using YLErp.DBModels.Consts; //using YLErp.Model; //using YLErp.QdpModule; //using CalculatorHelper = YLErp.BLL.Calculation.CalculatorHelperOld; //namespace YLErp.BLL.MarginCalculationBak //{ // /// // /// 国泰君安保证金计算 // /// // public class GTJAMarginCalculation : MarginCalculationBase // { // // 定义一个静态变量来保存类的实例 // public static readonly GTJAMarginCalculation Instance; // static GTJAMarginCalculation() // { // Instance = new GTJAMarginCalculation(); // } // // 定义私有构造函数,使外界不能创建该类实例 // protected GTJAMarginCalculation() // { // } // public override List RunMarginCalculation(int userId, string userName, List tradeList, DateTime settleDate, // Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易", bool forOtherSide = false) // { // //结果集 // Dictionary resultMap = new Dictionary(); // using (YLContext db = new YLContext()) // { // //为了算客户角度的一个保证金数值 // if (forOtherSide) // { // tradeList.ForEach(x => x.BuySell = x.BuySell == "买入" ? "卖出" : "买入"); // } // if (!(priceDict?.Count > 0)) // { // var codes = tradeList.Select(O => O.UnderlyingCode).ToArray(); // priceDict = GetSettlePrice(codes, settleDate, "收盘价"); // } // if (priceDict.Count == 0) // { // //如果价格没有传入也没从数据库获取到,就直接返回,没必要往下运行了。但不应该报错; // return resultMap.Values.ToList(); // } // var underlyingIds = priceDict.Keys.ToList(); // //获取标的涨跌幅限制 // var umDatas = (from um in db.underlying_manager // join variety in db.variety on um.UnderlyingTypeId equals variety.id into t_variety // from variety in t_variety.DefaultIfEmpty() // where underlyingIds.Contains(um.id) // select new // { // um.id, // um.VolatilityRate, // um.UpDownLimit, // defUpLimit = variety == null ? null : variety.UpLimit, // defDownLimit = variety == null ? null : variety.DownLimit, // defVolatilityRate = variety == null ? null : variety.VolatilityRate // }).ToList(); // var UpLimitDict = new Dictionary(); // var DownLimitDict = new Dictionary(); // var umVolatilityRateDic = new Dictionary(); // if (umDatas != null && umDatas.Count > 0) // { // umDatas.ForEach(t => // { // //OTC-8856 Start // //1.波动率变化 // if (DataConvert.TryParsePercentValue(t.VolatilityRate, out double pvalue) && (Math.Abs(pvalue) >= 1e-5)) // { // umVolatilityRateDic[t.id] = pvalue; // } // else if (DataConvert.TryParsePercentValue(t.defVolatilityRate, out pvalue) && (Math.Abs(pvalue) >= 1e-5)) // { // umVolatilityRateDic[t.id] = pvalue; // } // //2.涨跌停板幅度 // if (!string.IsNullOrWhiteSpace(t.UpDownLimit)) // { // UpLimitDict[t.id] = t.UpDownLimit;//百分比或绝对值 // DownLimitDict[t.id] = t.UpDownLimit; // } // else // { // UpLimitDict[t.id] = t.defUpLimit?.ToString() ?? "5%"; // DownLimitDict[t.id] = t.defDownLimit?.ToString() ?? "5%"; // } // }); // } // if (!hasOptionInfo) // { // tradeBLL.SetFieldsByTradeType(tradeList); // } // //判断当日结算价是否已经入库 // //if (priceDict == null || priceDict.Count == 0) // //{ // // throw new Exception("当日结算价还未同步,请等待结算价自动同步完成或手动同步后再执行收盘操作!"); // //} // var tradeVolatilityRateDic = tradeList.ToDictionary(t => t.id, t => umVolatilityRateDic.ContainsKey(t.UnderlyingId) ? umVolatilityRateDic[t.UnderlyingId] : 0) // .Where(d => d.Value > 0).ToDictionary(d => d.Key, d => d.Value); // Dictionary> eodPriceDict = new Dictionary>(); // var upLimitPrices = new Dictionary(); // var downLimitPrices = new Dictionary(); // double tempDouble; // //根据涨跌幅限制以及当日结算价计算涨停价以及跌停价 // if (priceDict != null && priceDict.Count > 0) // { // foreach (var t in priceDict) // { // //OTC-8856 Start // //UpLimit // if (UpLimitDict.ContainsKey(t.Key)) // { // var tempVaue = UpLimitDict[t.Key]; // if (tempVaue.Contains("%")) // { // //百分比 // double.TryParse(tempVaue.Replace("%", ""), out tempDouble); // upLimitPrices[t.Key] = t.Value + Math.Abs(t.Value * tempDouble * 0.01); // } // else // { // //绝对值 // double.TryParse(tempVaue, out double tempAbs); // upLimitPrices[t.Key] = t.Value + Math.Abs(tempAbs); // } // } // else // { // upLimitPrices[t.Key] = t.Value * 1.05; // } // //DownLimit // if (DownLimitDict.ContainsKey(t.Key)) // { // var tempVaue = DownLimitDict[t.Key]; // if (tempVaue.Contains("%")) // { // //百分比 // double.TryParse(tempVaue.Replace("%", ""), out tempDouble); // downLimitPrices[t.Key] = t.Value - Math.Abs(t.Value * tempDouble * 0.01); // } // else // { // //绝对值 // double.TryParse(tempVaue, out double tempAbs); // downLimitPrices[t.Key] = t.Value - Math.Abs(tempAbs); // } // } // else // { // downLimitPrices[t.Key] = t.Value * 0.95; // } // //End // } // eodPriceDict["up"] = upLimitPrices; // eodPriceDict["down"] = downLimitPrices; // } // //波动率变化 // var addVolRateList = new List> { null, tradeVolatilityRateDic }; // //交易对应客户信息 // var clientIds = tradeList.Select(t => t.ClientId).ToList(); // var clientList = (from client in db.client // join clientlevel in db.clientlevel // on client.LevelId equals clientlevel.id into tempClientlevel // from clientlevelTT in tempClientlevel.DefaultIfEmpty() // where clientIds.Contains(client.id) // select new // { // client, // clientlevel = clientlevelTT // }).ToList(); // var userIdNew = UniqueTimeId.Get().ToString(); // try // { // foreach (var price in eodPriceDict) // { // addVolRateList.ForEach(addVolRateDic => // { // var key = $"{price.Key}_{(addVolRateDic == null ? 0 : 1)}"; // var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades( // userIdNew, // userId + "_" + price.Key, // settleDate, // tradeList, // price.Value, // ValueCalculator.PV_ONLY, // addVolRateDic, // isEodSettle, // volType, // isUseTradeVol: PS.Config.IsTradeVol, // PreciseTimeMode: !isEodSettle, // isAddVolPercent: false); // if (tradeRiskResult.Results != null && tradeRiskResult.Results.Count > 0) // { // foreach (var item in tradeRiskResult.Results) // { // var client = clientList.FirstOrDefault(c => c.client.id == item.Trade.ClientId); // var clientRatio = client == null ? 1.0 : (client.clientlevel == null ? 1.0 : (client.clientlevel.Ratio ?? 1.0)); // double value = 0; // if (item.Trade.TradeType == "自定义交易") // { // var eodTradeRiskManual = db.eod_trade_risk_manual.Where(x => x.ValueDate <= settleDate && x.TradeId == item.Trade.id).OrderByDescending(x => x.ValueDate).FirstOrDefault(); // //收盘时如果自定义交易还活着且没有维护当日风险,并且收的时系统日期当日的盘,抛出exception // if (isEodSettle && !ConsTrade.TradeCompleteStatus.Contains(item.Trade.TradeStatus) && settleDate == valuedateBLL.SystemDate.ValueDate) // { // if (eodTradeRiskManual == null || eodTradeRiskManual.ValueDate != settleDate) // { // var error = $"TradeNumber:{item.Trade.TradeNumber}在{settleDate.ToString("yyyy-MM-dd")}需先进行交易风险维护"; // throw new Exception(error); // } // } // value = eodTradeRiskManual?.Margin ?? 0; // } // else if (item.Trade.TradeType == "权益互换") // { // value = (item.Trade.trade_swap.GetMarginRate ?? 0) * (item.Trade.StockEqvNotional ?? 0) + Math.Max((double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv), 0); // } // if (resultMap.ContainsKey(item.Trade.id)) // { // switch (key) // { // case "up_0": // resultMap[item.Trade.id].Spv1 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "up_1": // resultMap[item.Trade.id].Spv2 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "down_0": // resultMap[item.Trade.id].Spv3 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "down_1": // resultMap[item.Trade.id].Spv4 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // default: // break; // } // resultMap[item.Trade.id].setWorstCastClientPayable4(); // } // else // { // var trade = tradeList.FirstOrDefault(t => t.id == item.Trade.id); // var tempTradeSpan = new trade_span // { // TradeId = item.Trade.id, // ClientId = trade.ClientId, // ValueDate = settleDate, // UnderlyingId = trade.UnderlyingId, // UnderlyingCode = trade.UnderlyingCode, // OptId = userId, // OptName = userName, // OptDate = DateTime.Now // }; // switch (key) // { // case "up_0": // tempTradeSpan.Spv1 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "up_1": // tempTradeSpan.Spv2 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "down_0": // tempTradeSpan.Spv3 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // case "down_1": // tempTradeSpan.Spv4 = item.Trade.TradeType == "自定义交易" || item.Trade.TradeType == "权益互换" ? value : (double.IsNaN(item.ValueResult.Pv) ? 0 : item.ValueResult.Pv) * clientRatio; // break; // default: // break; // } // resultMap[item.Trade.id] = tempTradeSpan; // } // } // } // }); // } // } // finally // { // //上面的计算用到静态生成market,需要清除 // QdpMarketManager.Instance.RemovePrebuiltMarketProxy(userIdNew); // } // //为了保持原有交易买卖方向不变 // if (forOtherSide) // { // tradeList.ForEach(x => x.BuySell = x.BuySell == "买入" ? "卖出" : "买入"); // } // return resultMap.Values.ToList(); // } // } // public override bool CalcClientMargin(int userId, string userName, DateTime settleDate, List tradeSpans, List tradeSpansOtherSide, int SpanType = 0, List RefreshClientIds = null, bool OnlyBuyer = false, Dictionary clientAdditionalMarginDic = null) // { // using (YLContext db = new YLContext()) // { // //删除 // if (tradeSpans != null && tradeSpans.Count > 0) // { // var tradeIds = tradeSpans.Select(t => t.TradeId).ToList(); // var tradeList = db.trade.AsNoTracking().Where(t => tradeIds.Contains(t.id)).ToList(); // var clientIds = tradeSpans.Select(t => t.ClientId).Distinct().ToList(); // var clientList = db.client.Where(x => clientIds.Contains(x.id)).ToList(); // var tradeSpanInfo = (from tradeSpan in tradeSpans // join // trade in tradeList on tradeSpan.TradeId equals trade.id // where tradeSpan.ValueDate == settleDate // select new { trade, tradeSpan }).ToList(); // var tradeSpanInfoOtherSide = (from tradeSpan in tradeSpansOtherSide // join // trade in tradeList on tradeSpan.TradeId equals trade.id // where tradeSpan.ValueDate == settleDate // select new { trade, tradeSpan }).ToList(); // var clientSpanNews = new List(); // var clientGroups = tradeSpanInfo.GroupBy(t => t.trade.ClientId); // foreach (var clientGroup in clientGroups) // { // var underlyingGroup = clientGroup.GroupBy(t => t.trade.UnderlyingId).Select(t => new client_span // { // UnderlyingId = t.Key, // ClientId = clientGroup.Key, // ValueDate = settleDate, // Spv1 = t.Sum(g => g.tradeSpan.Spv1) * (-1), // Spv2 = t.Sum(g => g.tradeSpan.Spv2) * (-1), // Spv3 = t.Sum(g => g.tradeSpan.Spv3) * (-1), // Spv4 = t.Sum(g => g.tradeSpan.Spv4) * (-1), // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // SpanType = SpanType // }).ToList(); // foreach (var item in underlyingGroup) // { // item.WorstCastClientPayable = Math.Min(Math.Min(Math.Min(item.Spv1 ?? 0, item.Spv2 ?? 0), item.Spv3 ?? 0), item.Spv4 ?? 0); // item.TwoSideMargin = Math.Min(Math.Min(Math.Min(item.Spv1 ?? 0, item.Spv2 ?? 0), item.Spv3 ?? 0), item.Spv4 ?? 0); // #region 更新tradeSpan,使得每笔交易的持仓保证金和客户保证金计算用的Spv组保持一致 // var tradeSpansUpdate = db.trade_span.Where(x => x.ClientId == item.ClientId && x.UnderlyingId == item.UnderlyingId && x.ValueDate == settleDate).ToList(); // if (item.WorstCastClientPayable == item.Spv1) // { // tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv1); // } // else if (item.WorstCastClientPayable == item.Spv2) // { // tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv2); // } // else if (item.WorstCastClientPayable == item.Spv3) // { // tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv3); // } // else // { // tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv4); // } // #endregion // if (!PS.Config.ErpElement.NonInterBankMarginNetting) // { // item.WorstCastClientPayable = Math.Min(item.WorstCastClientPayable.Value, 0); // } // } // var clientSpan = new client_span // { // ClientId = clientGroup.Key, // ValueDate = settleDate, // Spv1 = underlyingGroup.Sum(g => g.Spv1), // Spv2 = underlyingGroup.Sum(g => g.Spv2), // Spv3 = underlyingGroup.Sum(g => g.Spv3), // Spv4 = underlyingGroup.Sum(g => g.Spv4), // //负数代表客户应缴保证金,正数代表客户应收保证金 // WorstCastClientPayable = (PS.Config.ErpElement.TwoSideMargin && clientList.FirstOrDefault(x => x.id == clientGroup.Key) != null && clientList.FirstOrDefault(x => x.id == clientGroup.Key).HasTwoSideMargin == 1) ? underlyingGroup.Sum(g => g.WorstCastClientPayable) : underlyingGroup.Sum(g => g.WorstCastClientPayable) < 0 ? underlyingGroup.Sum(g => g.WorstCastClientPayable) : 0, // TwoSideMargin = underlyingGroup.Sum(g => g.TwoSideMargin), // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // SpanType = SpanType, // AdditionalWorstCastClientPayable = clientAdditionalMarginDic == null ? 0 : (clientAdditionalMarginDic.ContainsKey(clientGroup.Key ?? 0) ? clientAdditionalMarginDic[clientGroup.Key ?? 0] : 0) // }; // clientSpanNews.Add(clientSpan); // } // //处理从客户角度的保证金计算(将交易买卖方向反向处理) // var clientGroupsOtherSide = tradeSpanInfoOtherSide.GroupBy(t => t.trade.ClientId); // foreach (var clientGroup in clientGroupsOtherSide) // { // var underlyingGroup = clientGroup.GroupBy(t => t.trade.UnderlyingId).Select(t => new client_span // { // ClientId = clientGroup.Key, // ValueDate = settleDate, // Spv1 = t.Sum(g => g.tradeSpan.Spv1) * (-1), // Spv2 = t.Sum(g => g.tradeSpan.Spv2) * (-1), // Spv3 = t.Sum(g => g.tradeSpan.Spv3) * (-1), // Spv4 = t.Sum(g => g.tradeSpan.Spv4) * (-1), // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // SpanType = SpanType // }).ToList(); // foreach (var item in underlyingGroup) // { // if (PS.Config.ErpElement.NonInterBankMarginNetting) // { // item.WorstCastClientPayable = Math.Min(Math.Min(Math.Min(item.Spv1 ?? 0, item.Spv2 ?? 0), item.Spv3 ?? 0), item.Spv4 ?? 0); // } // else // { // item.WorstCastClientPayable = Math.Min(Math.Min(Math.Min(Math.Min(item.Spv1 ?? 0, item.Spv2 ?? 0), item.Spv3 ?? 0), item.Spv4 ?? 0), 0); // } // } // var clientSpan = clientSpanNews.FirstOrDefault(x => x.ClientId == clientGroup.Key && x.ValueDate == settleDate); // clientSpan.OtherSideMargin = (PS.Config.ErpElement.TwoSideMargin && clientList.FirstOrDefault(x => x.id == clientGroup.Key) != null && clientList.FirstOrDefault(x => x.id == clientGroup.Key).HasTwoSideMargin == 1) ? underlyingGroup.Sum(g => g.WorstCastClientPayable) : underlyingGroup.Sum(g => g.WorstCastClientPayable) < 0 ? underlyingGroup.Sum(g => g.WorstCastClientPayable) : 0; // } // //span类型为实时删除所有实时计算的交易的保证金信息 // if (SpanType == client_span.SpanType_RealTime) // { // if (RefreshClientIds != null) // { // db.BulkDelete($"{nameof(client_span.ClientId)} in @ids", new { ids = RefreshClientIds }); // } // else // { // db.BulkDelete($"{nameof(client_span.SpanType)}=@SpanType", new { SpanType }); // } // MySqlBulkExtensions.BulkInsert(db, clientSpanNews); // } // else // { // db.BulkDelete($"{nameof(client_span.ValueDate)}=@settleDate and {nameof(client_span.SpanType)}=@SpanType and {nameof(client_span.ModifiedFlag)}=0", new { settleDate, SpanType }); // var clientSpanOldsWithFlag = db.client_span.Where(t => t.ValueDate == settleDate && t.SpanType == SpanType && t.ModifiedFlag) // .Select(n => new { n.ValueDate, n.ClientId }).ToList(); // //筛选出可以修改的clientSpan // clientSpanNews = clientSpanNews.Where(c => !clientSpanOldsWithFlag.Any(t => t.ValueDate == c.ValueDate && t.ClientId == c.ClientId)).ToList(); // MySqlBulkExtensions.BulkInsert(db, clientSpanNews); // } // db.SaveChanges(); // } // return true; // } // } // public override double GetTradeMargin(trade trade, double price, bool isInitialMargin = false, bool hasOptionInfo = false) // { // using (YLContext db = new YLContext()) // { // if (trade.TradeType == "结构化交易") // { // trade.SubTrades = db.trade.Where(x => x.ParentTradeId == trade.id).ToList(); // } // } // var tradeMargin = RunMarginCalculation(0, "系统", new List { trade }, isInitialMargin ? (trade.TradeDate ?? valuedateBLL.ValueDate) : valuedateBLL.ValueDate, new Dictionary { { trade.UnderlyingId, price } }, hasOptionInfo: hasOptionInfo); // if (null != tradeMargin) // { // return tradeMargin.FirstOrDefault()?.WorstCastClientPayable ?? 0.0; // } // return 0.0; // } // } //}