375 lines
17 KiB
C#
375 lines
17 KiB
C#
using YLErp.Abstract.DataProviders;
|
|
using YLErp.BLL.Calculation;
|
|
using YLErp.Enums;
|
|
using YLErp.Helpers;
|
|
using YLErp.Model.Enum;
|
|
using YLErp.Modules;
|
|
using YLErp.Modules.CalculationModule;
|
|
using YLErp.Modules.DataProviderModule;
|
|
using YLErp.Modules.TradeModule;
|
|
|
|
namespace YLErp.BLL.MarginCalculation
|
|
{
|
|
/// <summary>
|
|
/// 预付金计算
|
|
/// </summary>
|
|
public partial class MarginCalculationBase
|
|
{
|
|
protected static readonly IYcLogger logger = LogFactory.GetLogger("预付金计算");
|
|
|
|
protected readonly UnderlyingDataProvider _underlyingDataProvider;
|
|
|
|
protected MarginCalculationBase()
|
|
{
|
|
_underlyingDataProvider = new UnderlyingDataProvider();
|
|
}
|
|
|
|
public virtual List<trade_span> CalcClientMargin(CalcClientMarginReq req)
|
|
{
|
|
var clientSpanNews = new List<ClientSpan>();
|
|
|
|
using (var db = new YLContext())
|
|
{
|
|
if (req.tradeSpans != null && req.tradeSpans.Count > 0)
|
|
{
|
|
var tradeIds = req.tradeSpans.Select(t => t.TradeId).ToList();
|
|
var tradeList = db.trade.AsNoTracking().Where(t => tradeIds.Contains(t.id)).ToList();
|
|
var tradeSpanInfo = (from tradeSpan in req.tradeSpans
|
|
join trade in tradeList on tradeSpan.TradeId equals trade.id
|
|
where tradeSpan.ValueDate == req.settleDate
|
|
select new { trade, tradeSpan }).ToList();
|
|
|
|
var clientGroups = tradeSpanInfo.GroupBy(t => t.trade.ClientId);
|
|
foreach (var clientGroup in clientGroups)
|
|
{
|
|
var client = DataCacheProvider.GetClientDataSource().GetData(clientGroup.Key);
|
|
var underlyingGroup = clientGroup.GroupBy(t => t.trade.UnderlyingId).Select(t => new ClientSpan
|
|
{
|
|
UnderlyingId = t.Key,
|
|
ClientId = clientGroup.Key,
|
|
ValueDate = req.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),
|
|
SwapWorstCastClientPayable = t.Where(g => g.trade.TradeType == "收益互换").Sum(g => g.tradeSpan.WorstCastClientPayable) * (-1),
|
|
OptId = req.userId,
|
|
OptName = req.userName,
|
|
OptDate = DateTime.Now,
|
|
SpanType = req.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 tradeIdList = clientGroup.Select(x => x.tradeSpan.TradeId);
|
|
var tradeSpansUpdate = db.trade_span.Where(x => tradeIdList.Contains(x.TradeId) && x.ClientId == item.ClientId && x.UnderlyingId == item.UnderlyingId && x.ValueDate == req.settleDate).ToList();
|
|
var tradeSpansReq = req.tradeSpans.Where(x => tradeIdList.Contains(x.TradeId) && x.ClientId == item.ClientId && x.UnderlyingId == item.UnderlyingId && x.ValueDate == req.settleDate).ToList();
|
|
|
|
if (item.WorstCastClientPayable == item.Spv1)
|
|
{
|
|
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv1);
|
|
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv1);
|
|
}
|
|
else if (item.WorstCastClientPayable == item.Spv2)
|
|
{
|
|
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv2);
|
|
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv2);
|
|
}
|
|
else if (item.WorstCastClientPayable == item.Spv3)
|
|
{
|
|
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv3);
|
|
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv3);
|
|
}
|
|
else if (item.WorstCastClientPayable == item.Spv4)
|
|
{
|
|
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv4);
|
|
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv4);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
var clientSpan = new ClientSpan
|
|
{
|
|
ClientId = clientGroup.Key,
|
|
ValueDate = req.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 = underlyingGroup.Sum(g => g.WorstCastClientPayable),
|
|
SwapWorstCastClientPayable = underlyingGroup.Sum(g => g.SwapWorstCastClientPayable),
|
|
MySideMargin = underlyingGroup.Sum(g => g.WorstCastClientPayable),
|
|
TwoSideMargin = underlyingGroup.Sum(g => g.TwoSideMargin),
|
|
OptId = req.userId,
|
|
OptName = req.userName,
|
|
OptDate = DateTime.Now,
|
|
SpanType = req.SpanType,
|
|
AdditionalWorstCastClientPayable = req.clientAdditionalMarginDic != null
|
|
&& req.clientAdditionalMarginDic.TryGetValue(clientGroup.Key, out var dd) ? dd : 0
|
|
};
|
|
|
|
if (client.MarginOptionType == (int)MarginOptionEnum.单向追保)
|
|
{
|
|
clientSpan.WorstCastClientPayable = Math.Min(clientSpan.WorstCastClientPayable.Value, 0);
|
|
}
|
|
else if (client.MarginOptionType == (int)MarginOptionEnum.对手方单向追保)
|
|
{
|
|
clientSpan.WorstCastClientPayable = Math.Max(clientSpan.WorstCastClientPayable.Value, 0);
|
|
}
|
|
|
|
clientSpanNews.Add(clientSpan);
|
|
}
|
|
}
|
|
|
|
//span类型为实时删除所有实时计算的交易的预付金信息
|
|
if (req.SpanType == ClientSpan.SpanType_RealTime)
|
|
{
|
|
if (req.RefreshClientIds != null)
|
|
{
|
|
db.BulkDelete<ClientSpan>($"{nameof(ClientSpan.ClientId)} in @ids", new { ids = req.RefreshClientIds });
|
|
}
|
|
else
|
|
{
|
|
db.BulkDelete<ClientSpan>($"{nameof(ClientSpan.SpanType)}={req.SpanType}");
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
if (req.ClientIds != null)
|
|
{
|
|
var sql = $"{nameof(ClientSpan.ClientId)} in @ids and {nameof(ClientSpan.ValueDate)}='{req.settleDate.ToSqlDate()}' and {nameof(ClientSpan.SpanType)}={req.SpanType} and {nameof(ClientSpan.ModifiedFlag)}=0";
|
|
db.BulkDelete<ClientSpan>(sql, new { ids = req.ClientIds });
|
|
}
|
|
else
|
|
{
|
|
var sql = $"{nameof(ClientSpan.ValueDate)}='{req.settleDate.ToSqlDate()}' and {nameof(ClientSpan.SpanType)}={req.SpanType} and {nameof(ClientSpan.ModifiedFlag)}=0";
|
|
db.BulkDelete<ClientSpan>(sql);
|
|
}
|
|
|
|
var clientSpanOldsWithFlag = db.client_span.Where(t => t.ValueDate == req.settleDate && t.SpanType == req.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();
|
|
}
|
|
if (clientSpanNews.Count > 0)
|
|
{
|
|
//MySqlBulkExtensions.BulkInsert(db, clientSpanNews);
|
|
db.client_span.AddRange(clientSpanNews);
|
|
}
|
|
|
|
db.SaveChanges();
|
|
|
|
return req.tradeSpans;
|
|
}
|
|
}
|
|
|
|
public virtual List<trade_span> RunMarginCalculation(RunMarginCalculationReq req)
|
|
{
|
|
if (req.tradeList == null || !req.tradeList.Any())
|
|
{
|
|
return new List<trade_span>();
|
|
}
|
|
|
|
var tradeSpanList = new List<trade_span>();
|
|
|
|
//收益互换交易单独计算trade_span
|
|
if (req.tradeList.Any(t => t.TradeType == "收益互换"))
|
|
{
|
|
var tradeList = req.tradeList.Where(t => t.TradeType == "收益互换").ToList();
|
|
tradeSpanList.AddRange(SwapTradeMarginCalculation(req.Clone(tradeList)));
|
|
}
|
|
|
|
//剔除收益互换交易
|
|
req.tradeList = req.tradeList.Where(t => t.TradeType != "收益互换").ToList();
|
|
|
|
var helper = new RunMarginCalculationHelper(req, _underlyingDataProvider);
|
|
|
|
var resultMap = new Dictionary<int, trade_span>();
|
|
|
|
//为了算客户角度的一个预付金数值
|
|
helper.ReverseTradeSide();
|
|
|
|
helper.SetFieldsByTradeType();
|
|
|
|
helper.GetUpDownLimitPrices(out var upLimitPrices, out var downLimitPrices);
|
|
|
|
helper.GetTradVolRateDic(out var tradeVolRateDic);
|
|
|
|
var vols = new[] { null, tradeVolRateDic };
|
|
var prices = new (string, IPriceProvider)[] { ("up", upLimitPrices), ("down", downLimitPrices) };
|
|
var loops = prices.SelectMany(n => vols.Select(m => new
|
|
{
|
|
pricekey = n.Item1,
|
|
priceProvider = n.Item2,
|
|
addVolRateDic = m
|
|
})).ToArray();
|
|
|
|
foreach (var loop in loops)
|
|
{
|
|
var calcReq = helper.GetCalculateRisksForTradesReq(priceProvider: loop.priceProvider, addVolRateDic: loop.addVolRateDic, overrideVols: null);
|
|
|
|
var tradeRiskResult = CalculatorHelper.CalculateRisksForTrades(calcReq);
|
|
|
|
if (tradeRiskResult.Results == null || tradeRiskResult.Results.Count < 1)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = $"{loop.pricekey}_{(loop.addVolRateDic == null ? 0 : 1)}";
|
|
|
|
foreach (var item in tradeRiskResult.Results)
|
|
{
|
|
var pv = item.ValueResult.Pv;
|
|
|
|
if (!helper.GetSpecialMargin(item.Trade, pv, out var value))
|
|
{
|
|
var clientRatio = helper.GetClient(item.Trade)?.Ratio ?? 1.0;
|
|
|
|
value = double.IsNaN(pv) ? 0 : pv * clientRatio;
|
|
}
|
|
|
|
var contains = resultMap.TryGetValue(item.Trade.id, out var tempTradeSpan);
|
|
|
|
if (!contains)
|
|
{
|
|
resultMap[item.Trade.id] = tempTradeSpan = helper.CreateTradeSpan(item.Trade);
|
|
}
|
|
|
|
switch (key)
|
|
{
|
|
case "up_0":
|
|
tempTradeSpan.Spv1 = value; break;
|
|
case "up_1":
|
|
tempTradeSpan.Spv2 = value; break;
|
|
case "down_0":
|
|
tempTradeSpan.Spv3 = value; break;
|
|
case "down_1":
|
|
tempTradeSpan.Spv4 = value; break;
|
|
}
|
|
|
|
if (contains)
|
|
{
|
|
tempTradeSpan.SetWorstCastClientPayable();
|
|
}
|
|
}
|
|
}
|
|
|
|
tradeSpanList.AddRange(resultMap.Values.ToList());
|
|
|
|
return tradeSpanList;
|
|
}
|
|
|
|
public virtual double GetTradeMargin(GetTradeMarginReq req)
|
|
{
|
|
using (var db = new YLContext())
|
|
{
|
|
var marginReq = req.GetRunMarginCalculationReq();
|
|
if (req.trade.IsGroup == 1)
|
|
{
|
|
marginReq.tradeList = db.trade.Where(x => x.ParentTradeId == req.realTradeId).ToList();
|
|
if (marginReq.CalcMarginType == CalcMarginTypeEnum.InitialMargin)
|
|
{
|
|
foreach (var item in marginReq.tradeList)
|
|
{
|
|
item.id = 0;
|
|
}
|
|
}
|
|
}
|
|
var tradeMargin = RunMarginCalculation(marginReq);
|
|
if (null != tradeMargin)
|
|
{
|
|
var margin = req.trade.IsGroup == 1 ? tradeMargin.Sum(x => x.WorstCastClientPayable ?? 0) : (tradeMargin.FirstOrDefault()?.WorstCastClientPayable ?? 0);
|
|
return margin;
|
|
}
|
|
return 0.0;
|
|
}
|
|
}
|
|
|
|
//为了算客户角度的一个预付金数值
|
|
//RunMarginCalculation时forOtherSide为true时调用
|
|
protected List<trade_span> RunMarginCalculationOtherSide(RunMarginCalculationReq req)
|
|
{
|
|
void RevertBuySell()
|
|
{
|
|
foreach (var x in req.tradeList)
|
|
{
|
|
x.BuySell = x.BuySell == "买入" ? "卖出" : "买入";
|
|
if (x.SubTrades != null && x.SubTrades.Any())
|
|
{
|
|
foreach (var xs in x.SubTrades)
|
|
{
|
|
xs.BuySell = xs.BuySell == "买入" ? "卖出" : "买入";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
RevertBuySell();
|
|
|
|
req.forOtherSide = false;
|
|
var results = RunMarginCalculation(req);
|
|
RevertBuySell();
|
|
return results;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否客户有双向预付金
|
|
/// </summary>
|
|
protected bool HasTwoSideMargin(int clientId)
|
|
{
|
|
if (PS.Config.ErpElement.TwoSideMargin)
|
|
{
|
|
var client = DataCacheProvider.GetClientDataSource().GetData(clientId);
|
|
return client?.MarginOptionType == (int)MarginOptionEnum.双向追保;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取客户信息
|
|
/// </summary>
|
|
protected InnerClient GetClientInfo(int clientId)
|
|
{
|
|
if (clientId < 1)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using (var db = DbContextFactory.GetClientDbContext(null))
|
|
{
|
|
var query = from c in db.client
|
|
join cl in db.clientlevel on c.LevelId equals cl.id into t_cl
|
|
from cl in t_cl.DefaultIfEmpty()
|
|
where clientId == c.id
|
|
select new InnerClient
|
|
{
|
|
ClientId = c.id,
|
|
Ratio = cl == null ? null : cl.Ratio,
|
|
Ratio1 = cl == null ? null : cl.Ratio1,
|
|
AddRatio = cl == null ? null : cl.AddRatio,
|
|
MarginOptionType = c.MarginOptionType,
|
|
ProperClientClass = c.ProperClientClass,
|
|
QuestionnaireScore = c.QuestionnaireScore,
|
|
RuleT0orT1 = c.RuleT0orT1
|
|
};
|
|
|
|
return query.FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
//收益互换预付金计算
|
|
private static List<trade_span> SwapTradeMarginCalculation(RunMarginCalculationReq req)
|
|
{
|
|
var result = new List<trade_span>();
|
|
return result;
|
|
}
|
|
}
|
|
}
|