//using System; //using System.Collections.Generic; //using System.Linq; //using YLErp.BLL.Calculation.V2; //using YLErp.DBModels; //using YLErp.Model; //using CalculatorHelper = YLErp.BLL.Calculation.CalculatorHelperOld; //namespace YLErp.BLL.MarginCalculationBak //{ // /// // /// 安粮 // /// // public class ALQHMarginCalculation : MarginCalculationBase // { // // 定义一个静态变量来保存类的实例 // public static readonly ALQHMarginCalculation Instance; // static ALQHMarginCalculation() // { // Instance = new ALQHMarginCalculation(); // } // // 定义私有构造函数,使外界不能创建该类实例 // private ALQHMarginCalculation() // { // } // 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 tradeSpans = new List(); // if (tradeList != null && tradeList.Count > 0) // { // var tempStockTradeList = tradeList.Where(t => t.UnderlyingInstrumentType == ConsGlobal.InstrumentType.Stock).ToList(); // if (tempStockTradeList.Any()) // { // var stockTradeSpanlist = StockMarginCalculation(userId, userName, tempStockTradeList, settleDate, priceDict, hasOptionInfo, isEodSettle); // if (stockTradeSpanlist.Count > 0) // { // tradeSpans.AddRange(stockTradeSpanlist); // } // } // var tempFutureTradeList = tradeList.Where(t => t.UnderlyingInstrumentType == ConsGlobal.InstrumentType.CommodityFutures).ToList(); // if (tempFutureTradeList.Any()) // { // if (!(priceDict?.Count > 0)) // { // var codes = tradeList.Select(O => O.UnderlyingCode).ToArray(); // priceDict = base.GetSettlePrice(codes, settleDate); // } // if (priceDict.Count > 0) // { // var futureTradeSpanlist = FutureMarginCalculation(userId, userName, tempFutureTradeList, settleDate, priceDict, hasOptionInfo, isEodSettle, volType); // if (futureTradeSpanlist.Count > 0) // { // tradeSpans.AddRange(futureTradeSpanlist); // } // } // } // } // return tradeSpans; // } // /// // /// 股票类期权计算保证金 // /// // public List StockMarginCalculation(int userId, string userName, List tradeList, DateTime settleDate, Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false) // { // List tradeSpans = new List(); // if (tradeList != null && tradeList.Count > 0) // { // using (YLContext db = new YLContext()) // { // var marginRation = valuedateBLL.SystemDate.MarginRatio ?? 0.15; // 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(); // if (clientList != null) // { // tradeList.ForEach(t => // { // var client = clientList.FirstOrDefault(c => c.client.id == t.ClientId); // if (client != null) // { // //未设置相关保证金系数默认为1.0 // var clientRatio = client.clientlevel == null ? 1.0 : (client.clientlevel.Ratio ?? 1.0); // //如果是股票去名义本金,如果是期货取:份额 * 即期价格 // var value = (t.StockEqvNotional ?? (t.Notional * t.SpotPrice ?? 0.0)) * marginRation * (t.BuySell == "买入" ? 1 : (PS.Config.ErpElement.TwoSideMargin && client.client.HasTwoSideMargin == 1 ? -1 : 0)) * clientRatio; // var twoSideMargin = (t.StockEqvNotional ?? (t.Notional * t.SpotPrice ?? 0.0)) * marginRation * (t.BuySell == "买入" ? 1 : -1) * clientRatio; // tradeSpans.Add(new trade_span // { // TradeId = t.id, // OptDate = DateTime.Now, // OptId = userId, // OptName = userName, // ClientId = client.client.id, // UnderlyingId = t.UnderlyingId, // UnderlyingCode = t.UnderlyingCode, // ValueDate = settleDate, // Spv1 = value, // Spv2 = value, // Spv3 = value, // Spv4 = value, // WorstCastClientPayable = value, // TwoSideMargin = twoSideMargin // }); // } // }); // } // } // } // return tradeSpans; // } // /// // /// 商品期权计算保证金 // /// // public List FutureMarginCalculation(int userId, string userName, List futureTradeList, DateTime settleDate, Dictionary priceDict, bool hasOptionInfo = false, bool isEodSettle = false, string volType = "交易") // { // var tradeSpans = new List(); // using (YLContext db = new YLContext()) // { // if (!hasOptionInfo) // { // tradeBLL.SetFieldsByTradeType(futureTradeList); // } // var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades(userId + "", settleDate, futureTradeList, priceDict, ValueCalculator.BASIC_GREEKS, null, isEodSettle, volType, isUseTradeVol: PS.Config.IsTradeVol, PreciseTimeMode: !isEodSettle); // var deltaCoefficient = valuedateBLL.SystemDate.FutureMarginDeltaCoefficient ?? 1; // var vegaCoefficient = valuedateBLL.SystemDate.FutureMarginVegaCoefficient ?? 3; // var omegaCoefficient = valuedateBLL.SystemDate.FutureMarginOmegaCoefficient ?? 2.5; // var alphaRate = valuedateBLL.SystemDate.FutureMarginAlphaRate ?? 1; // //客户对应等级系数 // var clientIds = futureTradeList.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(); // foreach (var item in tradeRiskResult.Results) // { // var tempTrade = futureTradeList.FirstOrDefault(t => t.id == item.Trade.id); // var optionValue = item.ValueResult; // var tempVariety = _underlyingDataProvider.GetVariety(tempTrade.UnderlyingId); // var closePrice = priceDict.ContainsKey(tempTrade.UnderlyingId) ? priceDict[tempTrade.UnderlyingId] : 0.0; // var client = clientList.FirstOrDefault(c => c.client.id == item.Trade.ClientId); // if (tempTrade != null && item.ValueResult != null && tempVariety != null) // { // //未设置相关保证金系数默认为1.0 // var clientRatio = client == null ? 1.0 : (client.clientlevel == null ? 1.0 : (client.clientlevel.Ratio ?? 1.0)); // //保证金 = ( a * DeltaCash + b* GammaCash * PricingVol / 16) * 当前标的保证金率 + c * Vega // var value = ((optionValue.DeltaCash * deltaCoefficient // + optionValue.GammaCash * 0.01 * (optionValue.Vol / 16) * omegaCoefficient) * tempVariety.Margin // + optionValue.Vega * vegaCoefficient // ) * alphaRate * (tempTrade.BuySell == "买入" ? 1 : (PS.Config.ErpElement.TwoSideMargin && client.client.HasTwoSideMargin == 1 ? 1 : 0)) * clientRatio; // var twoSideMargin = ((optionValue.DeltaCash * deltaCoefficient // + optionValue.GammaCash * 0.01 * (optionValue.Vol / 16) * omegaCoefficient) * tempVariety.Margin // + optionValue.Vega * vegaCoefficient // ) * alphaRate * (tempTrade.BuySell == "买入" ? 1 : -1) * clientRatio; // tradeSpans.Add(new trade_span // { // TradeId = tempTrade.id, // OptDate = DateTime.Now, // OptId = userId, // OptName = userName, // ClientId = tempTrade.ClientId, // UnderlyingId = tempTrade.UnderlyingId, // UnderlyingCode = tempTrade.UnderlyingCode, // ValueDate = settleDate, // Spv1 = value, // Spv2 = value, // Spv3 = value, // Spv4 = value, // WorstCastClientPayable = value, // TwoSideMargin = twoSideMargin // }); // } // } // return tradeSpans; // } // } // public override double GetTradeMargin(trade trade, double price, bool isInitialMargin = false, bool hasOptionInfo = false) // { // 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; // } // } //}