using YLErp.Modules.CalculationModule; using YLErp.Modules.EodModule; namespace YLErp.BLL.Eod { /// /// 日终结算基类 /// public class EodOperationBase { /// /// 根据交易方向判断是否需要加符号 /// public static int GetSign(string tradeSide) { return TradeCalcHelper.GetBuySellSign(tradeSide); } /// /// 获取收盘成功的最后一个结算日(isPreviousDate:date参数的上一日) /// public static DateTime GetLastSettlementDate(DateTime date, bool isPreviousDate = false) { return isPreviousDate ? EodDataHelper.GetPreSettleDate(date) : EodDataHelper.GetLastSettleDate(date); } /// /// 获取结算价收盘成功的最后一个结算日 /// /// 查询不到最后收盘日时是否返回当年最早收盘价收盘日,false返回DateTime.MinValue public static DateTime GetLastSettlementDate_Settle(bool lastSettleDateIsDefault = true) { DateTime? date = null; using (var db = new YLContext()) { date = db.eod_pnl_s.Select(O => O.ValueDate).OrderByDescending(O => O).FirstOrDefault(); if (date == null || date == DateTime.MinValue) { if (lastSettleDateIsDefault) { var accruedTotalPnlStartDate = valuedateBLL.SystemDate.AccruedTotalPnlStartDate ?? DateTime.Today.AddDays(-DateTime.Today.DayOfYear + 1); date = db.eodStatus.Where(t => t.Status == "已收盘" && t.ValueDate >= accruedTotalPnlStartDate).Min(O => (DateTime?)O.ValueDate) ?? DateTime.MinValue; } else { date = DateTime.MinValue; } } } return date.Value; } /// /// 获取持仓盈亏 /// public static double GetPositionPnl(double Pv, double TradePrice, double Notional, double OriginalNotional, string BuySell) { var d = (double.IsNaN(Pv) ? 0.0 : Pv) - TradePrice * Notional / OriginalNotional * GetSign(BuySell); return double.IsNaN(d) ? 0 : d; } /// /// 获取持仓成本(交易员角度) /// public static double GetPositionCost(double TradePrice, double PositionNotional, double OriginalNotional, string BuySell) { return OriginalNotional > 0 ? TradePrice * PositionNotional / OriginalNotional * GetSign(BuySell) : 0; } } }