Files
zszq-trs/YLErpUnitTest/Modules/MarginModule/MarginCalculation/UniversalMarginCalculation.cs
T
2024-05-09 14:06:26 +08:00

183 lines
9.5 KiB
C#

//using System;
//using System.Collections.Generic;
//using System.Linq;
//using YLErp.DBModels;
//using YLErp.DBModels.Consts;
//using YLErp.Model;
//using YLErp.Modules.MarginModule;
//namespace YLErp.BLL.MarginCalculationBak
//{
// public class UniversalMarginCalculation : MarginCalculationBase
// {
// // 定义一个静态变量来保存类的实例
// public static readonly UniversalMarginCalculation Instance;
// static UniversalMarginCalculation()
// {
// Instance = new UniversalMarginCalculation();
// }
// // 定义私有构造函数,使外界不能创建该类实例
// private UniversalMarginCalculation()
// {
// }
// public override List<trade_span> RunMarginCalculation(int userId, string userName, List<trade> tradeList, DateTime settleDate, Dictionary<int, double> priceDict, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易", bool forOtherSide = false)
// {
// List<trade_span> tradeSpans = new List<trade_span>();
// if (!(priceDict?.Count > 0))
// {
// var codes = tradeList.Select(O => O.UnderlyingCode).ToArray();
// priceDict = base.GetSettlePrice(codes, settleDate);
// }
// if (priceDict.Count == 0)
// {
// //如果价格没有传入也没从数据库获取到,就直接返回,没必要往下运行了。但不应该报错;
// return tradeSpans;
// }
// 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())
// {
// var futureTradeSpanlist = FutureMarginCalculation(userId, userName, tempFutureTradeList, settleDate, priceDict, hasOptionInfo, isEodSettle);
// if (futureTradeSpanlist.Count > 0)
// {
// tradeSpans.AddRange(futureTradeSpanlist);
// }
// }
// }
// return tradeSpans;
// }
// /// <summary>
// /// 股票类期权计算保证金
// /// </summary>
// public List<trade_span> StockMarginCalculation(int userId, string userName, List<trade> tradeList, DateTime settleDate, Dictionary<int, double> priceDict, bool hasOptionInfo = false, bool isEodSettle = false)
// {
// throw new Exception("未实现");
// }
// /// <summary>
// /// 商品期权计算保证金
// /// </summary>
// public List<trade_span> FutureMarginCalculation(int userId, string userName, List<trade> futureTradeList, DateTime settleDate, Dictionary<int, double> priceDict, bool hasOptionInfo = false, bool isEodSettle = false)
// {
// List<trade_span> tradeSpans = new List<trade_span>();
// if (futureTradeList != null && futureTradeList.Count > 0)
// {
// using (YLContext db = new YLContext())
// {
// 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();
// var underlyingCodes = futureTradeList.Select(O => O.UnderlyingCode).ToHashSet();
// var mpProvider = new MarginParamProvider(new OptUserInfo(0, "系统"), settleDate)
// .Initialize(underlyingCodes, MarginParamTypeEnum.MarginRate);
// if (clientList != null)
// {
// foreach (var t in futureTradeList)
// {
// priceDict.TryGetValue(t.UnderlyingId, out double price);
// mpProvider.TryGetMarginRate(t.UnderlyingCode, out double marginRate);
// var contractSize = (_underlyingDataProvider.GetUnderlying(t.UnderlyingCode)?.ContractSize) ?? 0;
// double diffPrice = 0;
// switch (t.OptionType)
// {
// case "看涨":
// diffPrice = (t.Strike ?? 0) - price;
// break;
// case "看跌":
// diffPrice = price - (t.Strike ?? 0);
// break;
// }
// double visualValue = Math.Max(diffPrice, 0) * contractSize;
// double futureMargin = price * marginRate * contractSize;
// double optionMargin1 = (t.StockEqvNotional ?? 0) + futureMargin - 0.5 * visualValue;
// double optionMargin2 = (t.StockEqvNotional ?? 0) + futureMargin * 0.5;
// var client = clientList.FirstOrDefault(c => c.client.id == t.ClientId);
// if (client != null)
// {
// var value = Math.Max(optionMargin1, optionMargin2);
// if (t.TradeType == "自定义交易")
// {
// var eodTradeRiskManual = db.eod_trade_risk_manual.Where(x => x.ValueDate <= settleDate && x.TradeId == t.id).OrderByDescending(x => x.ValueDate).FirstOrDefault();
// //收盘时如果自定义交易还活着且没有维护当日风险,并且收的时系统日期当日的盘,抛出exception
// if (isEodSettle && !ConsTrade.TradeCompleteStatus.Contains(t.TradeStatus) && settleDate == valuedateBLL.SystemDate.ValueDate)
// {
// if (eodTradeRiskManual == null || eodTradeRiskManual.ValueDate != settleDate)
// {
// var error = $"TradeNumber:{t.TradeNumber}在{settleDate:yyyy-MM-dd}需先进行交易风险维护";
// throw new Exception(error);
// }
// }
// value = eodTradeRiskManual?.Margin ?? 0;
// }
// 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
// });
// }
// }
// }
// }
// }
// return tradeSpans;
// }
// 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> { trade }, isInitialMargin ? (trade.TradeDate ?? valuedateBLL.ValueDate) : valuedateBLL.ValueDate, new Dictionary<int, double> { { trade.UnderlyingId, price } }, hasOptionInfo: hasOptionInfo);
// if (null != tradeMargin)
// {
// return tradeMargin.FirstOrDefault()?.WorstCastClientPayable ?? 0.0;
// }
// return 0.0;
// }
// }
//}