203 lines
10 KiB
C#
203 lines
10 KiB
C#
//using System;
|
|
//using System.Collections.Generic;
|
|
//using System.Linq;
|
|
//using YLErp.BLL.Calculation.V2;
|
|
//using YLErp.DBModels;
|
|
//using YLErp.Model;
|
|
//using YLErp.QdpModule;
|
|
//using CalculatorHelper = YLErp.BLL.Calculation.CalculatorHelperOld;
|
|
|
|
//namespace YLErp.BLL.MarginCalculationBak
|
|
//{
|
|
// public class HongYuanMarginCalculation : MarginCalculationBase
|
|
// {
|
|
// //定义一个静态变量来保存类的实例(单例模式)
|
|
// public static readonly HongYuanMarginCalculation Instance;
|
|
|
|
// static HongYuanMarginCalculation()
|
|
// {
|
|
// Instance = new HongYuanMarginCalculation();
|
|
// }
|
|
|
|
// //定义私有构造函数,使外界不能创建该类实例
|
|
// private HongYuanMarginCalculation()
|
|
// {
|
|
|
|
// }
|
|
|
|
// private List<trade_span> CalculationSingleTrade(int userId, string userName, List<trade> tradeList, DateTime settleDate, Dictionary<int, double> priceDict, bool isInitialMargin = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易")
|
|
// {
|
|
// //结果集
|
|
// var resultMap = new Dictionary<int, trade_span>();
|
|
// var tempTradeList = new List<trade>();
|
|
// var codes = new HashSet<string>(tradeList.Select(O => O.UnderlyingCode).ToArray());
|
|
// if (!(priceDict?.Count > 0))
|
|
// {
|
|
// //结算的时候价格列表一定是空的;
|
|
// priceDict = base.GetSettlePrice(codes, settleDate);
|
|
// }
|
|
// else
|
|
// {
|
|
// //价格列表不是空的说明是计算实时保证金或期初保证金
|
|
// //宏源在计算实时保证金的时候使用前一天的收盘价这个逻辑是一开始确认的,后续对数据的时候要再次确认 --时嬴政
|
|
// var lastDate = QdpCalendarHelper.GetNonHolidayDefore(settleDate.AddDays(-1));
|
|
// priceDict = base.GetSettlePrice(codes, lastDate);
|
|
// }
|
|
// if (priceDict.Count == 0)
|
|
// {
|
|
// //如果价格没有传入也没从数据库获取到,就直接返回,没必要往下运行了。但不应该报错;
|
|
// return resultMap.Values.ToList();
|
|
// }
|
|
// var clientIds = new HashSet<int>(tradeList.Select(O => O.ClientId ?? 0).ToArray());
|
|
// var clientClassDict = new clientBLL().GetClients(clientIds.ToList()).ToDictionary(K => K.id, V => V.ProperClientClass);
|
|
// var ratioDict = new clientlevelBLL().GetClientlevels(clientIds).ToDictionary(K => K.Key, V => isInitialMargin ? (V.Value.Ratio1 ?? 1) : (V.Value.Ratio ?? 1));
|
|
// var vols = new Dictionary<int, double>();
|
|
// tradeList.ForEach(t =>
|
|
// {
|
|
// double vol = t.TradeOpenVolatility ?? 0;
|
|
// if (!clientClassDict.ContainsKey(t.ClientId ?? 0))
|
|
// {
|
|
// throw new Exception($"交易编号为{t.TradeNumber}的客户适当性类型不存在,无法计算保证金!");
|
|
// }
|
|
// if (clientClassDict[t.ClientId ?? 0].Contains("普通投资者"))
|
|
// {
|
|
// vol = vol * 1.2;
|
|
// }
|
|
// vols[t.id] = vol;
|
|
// tempTradeList.Add(t.Clone());
|
|
// if (!priceDict.ContainsKey(t.UnderlyingId))
|
|
// { priceDict[t.UnderlyingId] = t.SpotPrice ?? 0; }
|
|
// });
|
|
// Action<trade> 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;
|
|
// }
|
|
// }
|
|
// };
|
|
// Dictionary<int, double[]> dict = base.GetUpDownLimitPrice(priceDict);
|
|
// Dictionary<int, double> underlyingPrice = new Dictionary<int, double>();
|
|
// tempTradeList.ForEach(t =>
|
|
// {
|
|
// changeUmIdFunction(t);
|
|
// underlyingPrice[t.UnderlyingId] = t.OptionType == "看涨" ? dict[t.UnderlyingId][0] : dict[t.UnderlyingId][1];
|
|
// });
|
|
// var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades(
|
|
// userId.ToString(), // userId + "_" + price.Key,
|
|
// settleDate,
|
|
// tempTradeList,
|
|
// underlyingPrice,
|
|
// ValueCalculator.PV_ONLY,
|
|
// null,
|
|
// isEndOfDate: true,
|
|
// volType: volType,
|
|
// overrideVolsForTrade: vols,
|
|
// isUseTradeVol: PS.Config.IsTradeVol,
|
|
// PreciseTimeMode: !isEodSettle);
|
|
|
|
// tradeRiskResult.Results.ForEach(r =>
|
|
// {
|
|
// System.Diagnostics.Debug.WriteLine($"{r.Trade.StructureType}-{r.Trade.SpotPrice}-{r.Trade.TradeOpenVolatility}-{r.ValueResult.Pv}");
|
|
// //保证金= 次日最大亏损;
|
|
// double value = ratioDict[r.Trade.ClientId ?? 0] * r.ValueResult.Pv;
|
|
// if (r.Trade.BuySell == "买入") { value = Math.Abs(value); } else { value = 0 - Math.Abs(value); }
|
|
// var tempTradeSpan = new trade_span
|
|
// {
|
|
// TradeId = r.Trade.id,
|
|
// ClientId = r.Trade.ClientId,
|
|
// ValueDate = settleDate,
|
|
// UnderlyingId = Math.Abs(r.Trade.UnderlyingId),
|
|
// UnderlyingCode = r.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<trade_span> CalcMargin(int userId, string userName, List<trade> tradeList, DateTime settleDate, Dictionary<int, double> priceDict, bool isInitialMargin = false, bool hasOptionInfo = false, bool isEodSettle = false, bool forSingleTrade = true, string volType = "交易")
|
|
// {
|
|
// //结果集
|
|
// List<trade_span> resultMap = new List<trade_span>();
|
|
|
|
// List<trade> tList = tradeList.Where(O => O.TradeType == "结构化交易").ToList();
|
|
// if (tList.Count > 0)
|
|
// {
|
|
// for (int i = 0; i < tList.Count; i++)
|
|
// {
|
|
// int tempId = int.MaxValue;
|
|
// tList[i].SubTrades.ToList().ForEach(t => t.id = tempId--);
|
|
// List<trade_span> spans = CalculationSingleTrade(userId, userName, tList[i].SubTrades.ToList(), settleDate, priceDict, isInitialMargin, isEodSettle, forSingleTrade, volType);
|
|
// double margin = 0;
|
|
// if (tList[i].StructureType.Contains("跨式"))
|
|
// { margin = spans.Max(O => O.WorstCastClientPayable ?? 0); }
|
|
// else
|
|
// { margin = spans.Sum(O => O.WorstCastClientPayable ?? 0); }
|
|
// resultMap.Add(new trade_span
|
|
// {
|
|
// TradeId = tList[i].id,
|
|
// ClientId = tList[i].ClientId,
|
|
// ValueDate = settleDate,
|
|
// UnderlyingId = tList[i].UnderlyingId,
|
|
// UnderlyingCode = tList[i].UnderlyingCode,
|
|
// OptId = userId,
|
|
// OptName = userName,
|
|
// OptDate = DateTime.Now,
|
|
// Spv1 = margin,
|
|
// Spv2 = margin,
|
|
// Spv3 = margin,
|
|
// Spv4 = margin,
|
|
// WorstCastClientPayable = margin
|
|
// });
|
|
// }
|
|
// }
|
|
// tList = tradeList.Where(O => O.TradeType != "结构化交易").ToList();
|
|
// if (tList.Count > 0)
|
|
// {
|
|
// resultMap.AddRange(CalculationSingleTrade(userId, userName, tList, settleDate, priceDict, isInitialMargin, isEodSettle, forSingleTrade, volType));
|
|
// }
|
|
// return resultMap;
|
|
// }
|
|
|
|
// 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)
|
|
// {
|
|
// return CalcMargin(userId, userName, tradeList, settleDate, priceDict, false, hasOptionInfo, isEodSettle, forSingleTrade, volType);
|
|
// }
|
|
|
|
// 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 = CalcMargin(0, "系统", new List<trade> { trade }, isInitialMargin ? (trade.TradeDate ?? valuedateBLL.ValueDate) : valuedateBLL.ValueDate, price == 0 ? null : new Dictionary<int, double> { { trade.UnderlyingId, price } }, isInitialMargin, hasOptionInfo);
|
|
// if (null != tradeMargin)
|
|
// {
|
|
// return tradeMargin.FirstOrDefault()?.WorstCastClientPayable ?? 0.0;
|
|
// }
|
|
// return 0.0;
|
|
// }
|
|
// }
|
|
//}
|