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

168 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CsvHelper;
using CsvHelper.Configuration;
using Org.BouncyCastle.Ocsp;
using System.Globalization;
using YLErp.Model.Enum;
using static NPOI.HSSF.Util.HSSFColor;
namespace YLErp.Modules.MarginModule
{
[TestClass]
public class HaiTongMarginTest
{
[TestMethod]
public void TestMethod1()
{
var csvFile = Path.Combine(AppContext.BaseDirectory, "Resources\\MarginModule\\tradespans.csv");
var config = new CsvConfiguration(CultureInfo.InvariantCulture) { HeaderValidated = null, MissingFieldFound=null };
using var reader = new StreamReader(csvFile);
using var csv = new CsvReader(reader, config);
var tradeSpans = csv.GetRecords<trade_span>().Where(n => n.ValueDate.Day == 10).ToList();
var clientSpanNews = new List<ClientSpan>();
if (tradeSpans != null && tradeSpans.Count > 0)
{
var tradeIds = tradeSpans.Select(t => t.TradeId).ToList();
//海通预付金保底收益率-用来计算名义本金
double GuaranteedIncome = 0.01;
var clientGroups = tradeSpans.GroupBy(t => t.ClientId);
foreach (var clientGroup in clientGroups)
{
var clientRatio = 1.1;
var underlyingGroup = clientGroup.Where(x => x.IsSingleMargin != true).GroupBy(t => t.UnderlyingId).Select(t => new ClientSpan
{
UnderlyingId = t.Key,
ClientId = clientGroup.Key ?? 0,
ValueDate = DateTime.Today,
//负数代表客户应缴预付金,正数代表客户应收预付金
Spv1 = -t.Sum(g => g.Spv1),
Spv2 = -t.Sum(g => g.Spv2),
Spv3 = -t.Sum(g => g.Spv3),
Spv4 = -t.Sum(g => g.Spv4),
Spv5 = -t.Sum(g => g.Spv5),
Spv6 = -t.Sum(g => g.Spv6),
Spv7 = -t.Sum(g => g.Spv7),
Spv8 = -t.Sum(g => g.Spv8),
WorstCastClientPayable = -t.Sum(g => g.WorstCastClientPayable),
OptId = 0,
OptName = "ddd",
OptDate = DateTime.Now,
SpanType = 1
}).ToList();
foreach (var item in underlyingGroup)
{
if (clientGroup.Count(m => m.UnderlyingId == item.UnderlyingId && m.ClientId == item.ClientId) > 1)
{
item.SetWorstCastClientPayableMin();
#region 更新tradeSpan,使得每笔交易的持仓预付金和客户预付金计算用的Spv组保持一致
var tradeIdList = clientGroup.Select(x => x.TradeId);
var tradeSpansUpdate = new List<trade_span>();
var tradeSpansReq = new List<trade_span>();
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);
}
else if (item.WorstCastClientPayable == item.Spv5)
{
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv5);
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv5);
}
else if (item.WorstCastClientPayable == item.Spv6)
{
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv6);
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv6);
}
else if (item.WorstCastClientPayable == item.Spv7)
{
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv7);
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv7);
}
else if (item.WorstCastClientPayable == item.Spv8)
{
tradeSpansUpdate.ForEach(x => x.WorstCastClientPayable = x.Spv8);
tradeSpansReq.ForEach(x => x.WorstCastClientPayable = x.Spv8);
}
#endregion
}
else
{
var tradeSpansUpdate = new List<trade_span>();
tradeSpansUpdate.ForEach(x => x.Comment = "单笔计算");
}
//客户信息中追保方向为“单向追保”或“双向追保”的客户: call 看涨
//(1)Call净名义本金 = 客户Call卖方持仓名义本金 - 客户Call买方持仓名义本金;
//(2)Put净名义本金 = 客户Put卖方持仓名义本金 - 客户Put买方持仓名义本金;
//(3)单品种预付金占用 = max(0, max(∑逐笔交易持仓预付金,(Call净名义本金 + Put净名义本金)*1 % *1.1)
//(4)预付金占用 =∑单品种预付金占用。
//客户信息追保方向“对手方单向追保”或“其他”的客户:预付金占用 = 0。
//var callsale = clientGroup.Where(m => m.UnderlyingId == item.UnderlyingId && m.trade.BuySell == "买入" && m.trade.OptionType == "看涨").Select(m => m.trade.StockEqvNotional).Sum();
//var callbuy = clientGroup.Where(m => m.UnderlyingId == item.UnderlyingId && m.trade.BuySell == "卖出" && m.trade.OptionType == "看涨").Select(m => m.trade.StockEqvNotional).Sum();
//var putsale = clientGroup.Where(m => m.UnderlyingId == item.UnderlyingId && m.trade.BuySell == "买入" && m.trade.OptionType == "看跌").Select(m => m.trade.StockEqvNotional).Sum();
//var putbuy = clientGroup.Where(m => m.UnderlyingId == item.UnderlyingId && m.trade.BuySell == "卖出" && m.trade.OptionType == "看跌").Select(m => m.trade.StockEqvNotional).Sum();
//var StockEqvNotional = (double)((callsale - callbuy + putsale - putbuy) * GuaranteedIncome * clientRatio);
//if (client.MarginOptionType != (int)MarginOptionEnum.双向追保)
//{
// item.WorstCastClientPayable = -Math.Max(0, Math.Max(-(double)item.WorstCastClientPayable, StockEqvNotional));
//}
}
var clientSpan = new ClientSpan
{
ClientId = clientGroup.Key ?? 0,
ValueDate = DateTime.Now,
Spv1 = underlyingGroup.Sum(g => g.Spv1),
Spv2 = underlyingGroup.Sum(g => g.Spv2),
Spv3 = underlyingGroup.Sum(g => g.Spv3),
Spv4 = underlyingGroup.Sum(g => g.Spv4),
Spv5 = underlyingGroup.Sum(g => g.Spv5),
Spv6 = underlyingGroup.Sum(g => g.Spv6),
Spv7 = underlyingGroup.Sum(g => g.Spv7),
Spv8 = underlyingGroup.Sum(g => g.Spv8),
//负数代表客户应缴预付金,正数代表客户应收预付金
WorstCastClientPayable = underlyingGroup.Sum(g => g.WorstCastClientPayable),
DeltaMargin = 0,
TwoSideMargin = underlyingGroup.Sum(g => g.TwoSideMargin),
OptId = 0,
OptName = "ddd",
OptDate = DateTime.Now,
SpanType = 1,
AdditionalWorstCastClientPayable = 0
};
clientSpanNews.Add(clientSpan);
}
}
}
}
}