Files
zszq-trs/YLErpDAL/Modules/ExchangeTradeModule/ExchangeTradePositionService.cs
T
2024-05-09 14:06:26 +08:00

136 lines
5.5 KiB
C#

using YLErp.BLL.Eod;
using YLErp.DBModels.Enums;
using YLErp.DBModels.Helpers;
using YLErp.Modules.DataProviderModule;
namespace YLErp.Modules.ExchangeTradeModule
{
/// <summary>
/// 交易持仓服务
/// </summary>
public class ExchangeTradePositionService : YLBaseService
{
public ExchangeTradePositionService(OptUserInfo userInfo) : base(userInfo)
{
}
/// <summary>
/// 同步对冲交易持仓
/// </summary>
public void ResetExchangeTradePosition()
{
var valueDate = SystemValueDate;
var lastDate = EodOperationBase.GetLastSettlementDate(valueDate, false);
List<string> tradetypes = new List<string> { "股票", "商品期货", "商品现货", "场内期权", "利率债", "信用债", "其它债券" };
var eodQuery = from et in DbContext.eod_trade_position.AsNoTracking()
where et.ValueDate == lastDate && tradetypes.Contains(et.TradeType)
select new TradePositionEx
{
UnderlyingId = et.UnderlyingId,
TradeType = et.TradeType,
BookId = et.BookId,
InstrumentCode = et.ExchangeOptionCode,
UnderlyingCode = et.UnderlyingCode,
Position = et.Amount,
PositionCost = et.Cost,
BuySell = et.PositionType
};
var positionList = eodQuery.ToList();
//如果当天未收盘
if (valueDate != lastDate)
{
var tdQuery = from trad in DbContext.ExchangeTrade.AsNoTracking()
where trad.TradeDate > lastDate && trad.TradeDate <= valueDate && trad.IsValid
select new TdTradePositionEx
{
TradeType = trad.TradeType,
BookId = trad.AssetBookId,
InstrumentCode = trad.OptionCode,
Position = trad.Notional,
BuySell = trad.TradeSide,
UnderlyingCode = trad.UnderlyingCode,
TradeSinglePrice = trad.TradeSinglePrice
};
var tdDatas = tdQuery.ToArray();
positionList.AddRange(tdDatas);
}
var dic = new Dictionary<string, TradePosition>(positionList.Count);
foreach (var n in positionList)
{
var tradeType1 = TradeHelper.GetTradeType1(n.TradeType);
var positionType = TradeHelper.GetPositionType(n.BuySell);
var key = $"{n.BookId}^{(int)tradeType1}^{(int)positionType}^{n.UnderlyingCode}^{n.InstrumentCode}";
var position = n.IsEod ? n.Position : TradeHelper.GetPositionNotional(n.BuySell, n.Position);
var positionCost = n.IsEod ? n.PositionCost : position * n.TradeSinglePrice;
if (dic.TryGetValue(key, out var pos))
{
System.Diagnostics.Debug.Assert(!n.IsEod);
pos.Position += position;
pos.PositionCost += positionCost;
}
else
{
dic[key] = pos = new TradePosition
{
TradeType = n.TradeType,
BookId = n.BookId,
InstrumentCode = n.InstrumentCode,
TradeType1 = tradeType1,
UnderlyingId = n.UnderlyingId,
UnderlyingCode = n.UnderlyingCode,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
Position = position,
PositionCost = positionCost,
PositionType = positionType
};
DateTime maturityDate;
if (pos.TradeType1 == TradeTypeFlag.Stocks)
{
maturityDate = DateTime.Today.AddYears(10);
}
else if (pos.TradeType1 == TradeTypeFlag.Options)
{
ExchangeOptionDataProvider.TryGetMaturityDate(pos.InstrumentCode, out var date);
maturityDate = date;
}
else if (!UnderlyingDataProvider.TryGetMaturityDate(pos.UnderlyingCode, out maturityDate))
{
maturityDate = DateTime.Today.AddMonths(1);
}
pos.MaturityDate = maturityDate;
}
}
//删除原来的对冲交易
var sql = "truncate trade_position;";
DbContext.Database.ExecuteSqlRaw(sql);
DbContext.TradePosition.AddRange(dic.Values);
DbContext.SaveChanges();
}
class TradePositionEx : TradePositionDto
{
public virtual bool IsEod => true;
public string BuySell { get; set; }
public double TradeSinglePrice { get; set; }
}
class TdTradePositionEx : TradePositionEx
{
public override bool IsEod => false;
}
}
}