//using System; //using System.Collections.Generic; //using System.Linq; //using YLErp.BLL.Calculation.V2; //using YLErp.DBModels; //using YLErp.Model; //using YLErp.Modules.MarginModule; //using YLErp.Modules.VolatilityModule; //using CalculatorHelper = YLErp.BLL.Calculation.CalculatorHelperOld; //namespace YLErp.BLL.MarginCalculationBak //{ // public class MaoChuanMarginCalculation : MarginCalculationBase // { // // 定义一个静态变量来保存类的实例(单例模式) // public static readonly MaoChuanMarginCalculation Instance; // static MaoChuanMarginCalculation() // { // Instance = new MaoChuanMarginCalculation(); // } // // 定义私有构造函数,使外界不能创建该类实例 // private MaoChuanMarginCalculation() // { // } // /// // /// 计算香草期权和亚式期权保证金 // /// // private List CalculationNormalOptionMargin(int userId, string userName, List tradeList, DateTime settleDate, Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易") // { // var resultMap = new Dictionary(); // var codes = tradeList.Select(O => O.UnderlyingCode).ToHashSet(); // var mpProvider = new MarginParamProvider(new OptUserInfo(0, "系统"), settleDate) // .Initialize(codes, MarginParamTypeEnum.MarginRate); // //客户要求用AskVol计算保证金,所以先把所有交易方向改为卖出并把VolType赋值为 BidAskVol; // //Dictionary buySellDict = tradeList.ToDictionary(K => K.id, V => V.BuySell); // //tradeList.ForEach(t => t.BuySell = "卖出"); // var vols = new Dictionary(); // foreach (var t in tradeList) // { // string vType = t.VolType; // t.VolType = "报价Ask"; // var um = _underlyingDataProvider.GetUnderlying(t.UnderlyingCode); // if (um == null) // { // throw new MarginCalcException($"[保证金计算]找不到标的数据(交易编号:{t.TradeNumber},标的代码:{t.UnderlyingCode})"); // } // double vol = SingleVolService.GetSingleVol(new SingleVolReq(t, um), userId); // vols[t.id] = vol; // t.VolType = vType; // } // var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades( // userId.ToString(), // settleDate, // tradeList, // priceDict, // ValueCalculator.BASIC_PRICING, // null, // isEndOfDate: true, // volType: null, // overrideVolsForTrade: vols, // isUseTradeVol: PS.Config.IsTradeVol, // PreciseTimeMode: !isEodSettle); // //tradeList.ForEach(t => t.BuySell = buySellDict[t.id]); // foreach (var r in tradeRiskResult.Results) // { // if (!mpProvider.TryGetMarginRate(r.Trade.UnderlyingCode, out var marginRate)) // { // throw new MarginCalcException($"{r.Trade.UnderlyingCode} 保证金比率不存在"); // } // var trade = tradeList.FirstOrDefault(t => t.id == r.Trade.id); // double price = priceDict == null ? r.Trade.SpotPrice ?? 0 : priceDict[r.Trade.UnderlyingId]; // logger.Info($"茂川标准保证金计算:[{r.Trade.TradeType}_{r.Trade.BuySell}_{r.Trade.OptionType}_{r.Trade.Strike}] => Vol:{r.ValueResult.Vol};Delta:{r.ValueResult.Delta}"); // //保证金= Delta*标的期货保证金比率*标的期货价格*持仓规模 (qdp计算出的Delta已经乘过数量了,所以这里不用再乘了); // double value = Math.Abs(r.ValueResult.Delta) * marginRate * price;// * r.Trade.Notional; // if (r.Trade.BuySell == "买入") { value = Math.Abs(value); } else { value = 0 - Math.Abs(value); } // var tempTradeSpan = new trade_span // { // TradeId = r.Trade.id, // ClientId = trade.ClientId, // ValueDate = settleDate, // UnderlyingId = trade.UnderlyingId, // UnderlyingCode = trade.UnderlyingCode, // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // Spv1 = value, // Spv2 = value, // Spv3 = value, // Spv4 = value, // WorstCastClientPayable = value // }; // resultMap[r.Trade.id] = tempTradeSpan; // } // return resultMap.Values.ToList(); // } // /// // /// 计算其他期权保证金 // /// // private List CalculationOtherOptionMargin(int userId, string userName, List tradeList, DateTime settleDate, Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易") // { // var resultMap = new Dictionary(); // var tempTradeList = new List(); // var vols = new Dictionary(); // foreach (var t in tradeList) // { // string vType = t.VolType; // t.VolType = "报价Ask"; // var um = _underlyingDataProvider.GetUnderlying(t.UnderlyingCode); // double vol = SingleVolService.GetSingleVol(new SingleVolReq(t, um), userId); // vols[t.id] = vol; // t.VolType = vType; // tempTradeList.Add(t.Clone()); // } // var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades( // userId.ToString(), // userId + "_" + price.Key, // settleDate, // tempTradeList, // priceDict, // ValueCalculator.BASIC_PRICING, // null, // isEndOfDate: true, // volType: null, // overrideVolsForTrade: vols, // isUseTradeVol: PS.Config.IsTradeVol, // PreciseTimeMode: !isEodSettle); // Action changeUmIdFunction = (t) => // { // if (t.OptionType == "看跌") // { // t.UnderlyingId = t.UnderlyingId * -1; // if (t.trade_spread_option != null) // { // t.trade_spread_option.UnderlyingId1 = t.trade_spread_option.UnderlyingId1 * -1; // t.trade_spread_option.UnderlyingId2 = t.trade_spread_option.UnderlyingId2 * -1; // t.trade_spread_option.UnderlyingId3 = t.trade_spread_option.UnderlyingId3 * -1; // t.trade_spread_option.UnderlyingId4 = t.trade_spread_option.UnderlyingId4 * -1; // } // if (t.trade_rainbow_option != null) // { // t.trade_rainbow_option.UnderlyingId1 = t.trade_rainbow_option.UnderlyingId1 * -1; // t.trade_rainbow_option.UnderlyingId2 = t.trade_rainbow_option.UnderlyingId2 * -1; // } // } // }; // if (priceDict == null) // { // priceDict = new Dictionary(); // Dictionary rateDict = base.GetUpDownLimitRate(tradeList.Select(O => O.UnderlyingId).ToArray()); // tempTradeList.ForEach(t => // { // double[] arr = base.GetUpDownLimitPrice(rateDict[t.id], (t.SpotPrice ?? 0), double.Parse(rateDict[t.id][2])); // changeUmIdFunction(t); // priceDict[t.UnderlyingId] = t.OptionType == "看涨" ? arr[1] : arr[0]; // }); // } // else // { // Dictionary dict = base.GetUpDownLimitPrice(priceDict); // tempTradeList.ForEach(t => // { // changeUmIdFunction(t); // priceDict[t.UnderlyingId] = t.OptionType == "看涨" ? dict[t.id][1] : dict[t.id][0]; // }); // } // var newTradeRiskResult = CalculatorHelper.CalculateRisksForTrades( // userId.ToString(), // userId + "_" + price.Key, // settleDate, // tempTradeList, // priceDict, // ValueCalculator.BASIC_PRICING, // null, // isEndOfDate: true, // volType: null, // overrideVolsForTrade: vols, // isUseTradeVol: PS.Config.IsTradeVol, // PreciseTimeMode: !isEodSettle); // tempTradeList.ForEach(t => // { // double cPv = tradeRiskResult.Results.FirstOrDefault(O => O.ValueResult.TradeId == t.id).ValueResult.Pv; // double nPv = newTradeRiskResult.Results.FirstOrDefault(O => O.ValueResult.TradeId == t.id).ValueResult.Pv; // //保证金= 次日最大亏损; // double value = t.OptionType == "看涨" ? cPv - nPv : cPv + nPv; // logger.Info($"茂川奇异保证金计算:[{t.TradeType}_{t.BuySell}_{t.OptionType}_{t.Strike}] => 当日Pv:{cPv};次日Pv:{nPv}"); // var tempTradeSpan = new trade_span // { // TradeId = t.id, // ClientId = t.ClientId, // ValueDate = settleDate, // UnderlyingId = Math.Abs(t.UnderlyingId), // UnderlyingCode = t.UnderlyingCode, // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // Spv1 = value, // Spv2 = value, // Spv3 = value, // Spv4 = value, // WorstCastClientPayable = value // }; // resultMap[t.id] = tempTradeSpan; // }); // return resultMap.Values.ToList(); // } // private List CalculationStructureOptionMargin(int userId, string userName, List tradeList, DateTime settleDate, Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易") // { // List resultMap = new List(); // for (int i = 0; i < tradeList.Count; i++) // { // int tempId = int.MaxValue; // tradeList[i].SubTrades.ToList().ForEach(t => t.id = tempId--); // List childrenSpans = RunMarginCalculation(userId, userName, tradeList[i].SubTrades.ToList(), settleDate, null, hasOptionInfo, isEodSettle, forSingleTrade, volType); // double margin = 0; // switch (tradeList[i].StructureType) // { // case "牛市价差": // case "熊市价差": // case "亚式熊市价差": // case "三领口组合": // for (int j = 0; j < childrenSpans.Count; j++) // { // margin += childrenSpans[j].WorstCastClientPayable ?? 0; // } // break; // case "跨式组合": // case "宽跨式组合": // for (int j = 0; j < childrenSpans.Count; j++) // { // double absMargin = Math.Abs(childrenSpans[j].WorstCastClientPayable ?? 0); // if (margin < absMargin) // { // margin = absMargin; // } // } // break; // case "复制标的资产": // case "蝶式组合": // case "飞鹰式组合": // case "比例价差": // case "日历价差": // case "箱式价差": // case "风险逆转": // default: // break; // } // var tempTradeSpan = new trade_span // { // TradeId = tradeList[i].id, // ClientId = tradeList[i].ClientId, // ValueDate = settleDate, // UnderlyingId = tradeList[i].UnderlyingId, // UnderlyingCode = tradeList[i].UnderlyingCode, // OptId = userId, // OptName = userName, // OptDate = DateTime.Now, // Spv1 = margin, // Spv2 = margin, // Spv3 = margin, // Spv4 = margin, // WorstCastClientPayable = margin // }; // resultMap.Add(tempTradeSpan); // } // return resultMap; // } // 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) // { // //结果集 // List resultMap = new List(); // if (!(priceDict?.Count > 0)) // { // var codes = tradeList.Select(O => O.UnderlyingCode).ToArray(); // priceDict = base.GetSettlePrice(codes, settleDate); // } // if (priceDict.Count == 0) // { // //如果价格没有传入也没从数据库获取到,就直接返回,没必要往下运行了。但不应该报错; // return resultMap; // } // if (forOtherSide) // { // return RunMarginCalculationOtherSide(userId, userName, tradeList, settleDate, priceDict, hasOptionInfo, isEodSettle, forSingleTrade, volType); // } // List tList = tradeList.Where(O => O.TradeType == "结构化交易").ToList(); // if (tList.Count > 0) // { // resultMap.AddRange(CalculationStructureOptionMargin(userId, userName, tList, settleDate, priceDict, hasOptionInfo, isEodSettle, forSingleTrade, volType)); // } // tList = tradeList.Where(O => O.TradeType == "香草期权" || O.TradeType == "亚式期权").ToList(); // if (tList.Count > 0) // { // resultMap.AddRange(CalculationNormalOptionMargin(userId, userName, tList, settleDate, priceDict, hasOptionInfo, isEodSettle, forSingleTrade, volType)); // } // tList = tradeList.Where(O => O.TradeType != "结构化交易" && O.TradeType != "香草期权" && O.TradeType != "亚式期权").ToList(); // if (tList.Count > 0) // { // resultMap.AddRange(CalculationOtherOptionMargin(userId, userName, tList, settleDate, priceDict, hasOptionInfo, isEodSettle, forSingleTrade, volType)); // } // return resultMap; // } // public override double GetTradeMargin(trade trade, double price, bool isInitialMargin = false, bool hasOptionInfo = false) // { // if (trade.TradeType == "结构化交易" && (trade.SubTrades == null || trade.SubTrades.Count() == 0)) // { // using (YLContext db = new YLContext()) // { // 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; // } // } //}