using YLErp.BLL.MarginCalculation; using YLErp.DBModels.Abstract; using YLErp.DBModels.Consts; using YLErp.Modules.CalculationModule; using YLErp.Modules.DataProviderModule; using YLErp.Plugins.TradeDocGenerator.Abstracts; namespace YLErp.Modules.TradeModule.DocGenerateModule { /// /// 交易结算生成上下文 /// public class SettlementBillGenerateContext : ConfirmationGenerateContext, ITradeSettleBillGeneratorContext { public SettlementBillGenerateContext(IEnumerable tradeCashIds, IEnumerable trades, string contractType, OptUserInfo userInfo, DateTime? startDate, DateTime? endDate) : base(tradeCashIds, trades, contractType, userInfo, startDate, endDate) { } public SettlementBillGenerateContext(int tradeCashId, trade tradeObj, string contractType, OptUserInfo userInfo) : base(tradeCashId, tradeObj, contractType, userInfo) { } public SettlementBillGenerateContext(Client client, List flowEvents, List allFlowEvents, List trades, List swapPositions, string contractType, OptUserInfo userInfo) :base(client, flowEvents, allFlowEvents, trades, swapPositions, contractType, userInfo) { } /// /// 获取预付金 /// public double? GetMargin(double endPrice) { var req = new RunMarginCalculationReq(UserInfo) { tradeList = new List { (trade)Trade }, settleDate = Trade.UnWindDate.Value, PriceProvider = new SinglePriceProvider(Trade.UnderlyingCode, endPrice) }; var tradeSpans = MarginDefault.RunMarginCalculation(req); return tradeSpans?.FirstOrDefault()?.WorstCastClientPayable; } //交易确认书对象 trade_contract_document _contractDoc; /// /// 获取交易确认书 /// public ITradeContractDocument GetTradeContractDocument(int tradeId, string tradeNumber, bool throwException = true) { if (_contractDoc == null) { var db = DbContextFactory.GetYLDbContext(); var query = from contractDoc in db.trade_contract_document join tcr in db.trade_contract_r on contractDoc.Code equals tcr.ContractCode where tcr.TradeId == tradeId && contractDoc.Type == ContractTypeEnum.Trade && tcr.Type == ContractTypeEnum.Trade && tcr.IsValid select contractDoc; _contractDoc = query.FirstOrDefault(); } if (_contractDoc == null && throwException) { throw new ServiceException($"未找到交易确认书(交易编号:{tradeNumber})"); } return _contractDoc; } public bool OnSettlmentBillGenerated() { return true; } public double GetAverageSpotPrice(OtcTradeBase trade, trade_swap trade_swap) { var trades = (from td in DbContext.trade.Where(x => x.ClientId == trade.ClientId && x.UnderlyingCode == trade.UnderlyingCode && x.BuySell == trade.BuySell && x.TradeDate == trade.TradeDate && x.ValidState != "InValid") join swap in DbContext.trade_swap.Where(x => x.PayLongShort == trade_swap.PayLongShort && x.GetLongShort == trade_swap.GetLongShort) on td.id equals swap.TradeId select new { td, swap }).ToList(); var totalSpotPrice = 0.0; var totalNotional = 0.0; trades.ForEach(x => { var tradeprice = x.swap.IsGetFloatingProfit ? (x.swap.PayTradePrice ?? 0) : (x.swap.GetTradePrice ?? 0); var notional = x.swap.IsGetFloatingProfit ? (x.swap.GetNotional ?? 0) : (x.swap.PayNotional ?? 0); var longshort = x.swap.IsGetFloatingProfit ? x.swap.GetLongShort : x.swap.PayLongShort; var spotprice = x.swap.IsGetFloatingProfit ? (x.swap.GetSpotPrice ?? 0) : (x.swap.PaySpotPrice ?? 0); var sigleprice = Math.Abs(tradeprice / notional); totalSpotPrice += (spotprice + (longshort == "空头" ? -1 : 1) * sigleprice) * notional; totalNotional += notional; }); return totalNotional > 0 ? totalSpotPrice / totalNotional : 0; } } }