using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace YLErp.DBModels { [Table("synthetic_underlying")] public class SyntheticUnderlying : DBModelWithOperator, IClonable { /// /// 名称 /// [DisplayName("名称"), Required] public string Name { set; get; } /// /// 标的信息1 /// [DisplayName("标的信息1")] public int? UnderlyingId1 { set; get; } /// /// 标的信息2 /// [DisplayName("标的信息2")] public int? UnderlyingId2 { set; get; } /// /// 标的信息3 /// [DisplayName("标的信息3")] public int? UnderlyingId3 { set; get; } /// /// 标的信息4 /// [DisplayName("标的信息4")] public int? UnderlyingId4 { set; get; } /// /// 标的代码1 /// [DisplayName("标的代码1")] public string UnderlyingCode1 { set; get; } /// /// 标的代码3 /// [DisplayName("标的代码3")] public string UnderlyingCode2 { set; get; } /// /// 标的代码1 /// [DisplayName("标的代码1")] public string UnderlyingCode3 { set; get; } /// /// 标的代码4 /// [DisplayName("标的代码4")] public string UnderlyingCode4 { set; get; } /// /// 系数1 /// [DisplayName("系数1")] public double? Coefficient1 { set; get; } /// /// 系数2 /// [DisplayName("系数2")] public double? Coefficient2 { set; get; } /// /// 系数3 /// [DisplayName("系数3")] public double? Coefficient3 { set; get; } /// /// 系数4 /// [DisplayName("系数4")] public double? Coefficient4 { set; get; } /// /// 常数 /// public double? Constant { get; set; } /// /// 合约乘数 /// public double? ContractSize { get; set; } public bool IsEquals(SyntheticUnderlying su2) { if (ReferenceEquals(this, su2)) { return true; } if (su2 is null) { return false; } string InnerTrim(string str) { return str?.Trim() ?? string.Empty; } return InnerTrim(UnderlyingCode1) == InnerTrim(su2.UnderlyingCode1) && InnerTrim(UnderlyingCode2) == InnerTrim(su2.UnderlyingCode2) && InnerTrim(UnderlyingCode3) == InnerTrim(su2.UnderlyingCode3) && InnerTrim(UnderlyingCode4) == InnerTrim(su2.UnderlyingCode4) && Math.Abs((Coefficient1 ?? 0) - su2.Coefficient1 ?? 0) < 1e-7 && Math.Abs((Coefficient2 ?? 0) - su2.Coefficient2 ?? 0) < 1e-7 && Math.Abs((Coefficient3 ?? 0) - su2.Coefficient3 ?? 0) < 1e-7 && Math.Abs((Coefficient4 ?? 0) - su2.Coefficient4 ?? 0) < 1e-7 && Math.Abs((Constant ?? 0) - su2.Constant ?? 0) < 1e-7; } public string UnderlyingTipsInfo { get { var tipsInfo = ""; if (!string.IsNullOrWhiteSpace(UnderlyingCode1)) { tipsInfo += $"{Coefficient1}*{UnderlyingCode1}"; } if (!string.IsNullOrWhiteSpace(UnderlyingCode2)) { tipsInfo += $"{(Coefficient2 >= 0 ? "+" : "-")}{Math.Abs(Coefficient2.Value)}*{UnderlyingCode2}"; } if (!string.IsNullOrWhiteSpace(UnderlyingCode3)) { tipsInfo += $"{(Coefficient3 >= 0 ? "+" : "-")}{Math.Abs(Coefficient3.Value)}*{UnderlyingCode3}"; } if (!string.IsNullOrWhiteSpace(UnderlyingCode4)) { tipsInfo += $"{(Coefficient4 >= 0 ? "+" : "-")}{Math.Abs(Coefficient4.Value)}*{UnderlyingCode4}"; } if (Constant != null && Constant != 0) { tipsInfo += $"{(Constant >= 0 ? "+" : "-")}{Math.Abs(Constant.Value)}"; } return tipsInfo; } } /// /// 获取可用的标的代码 /// public IEnumerable GetUnderlyingCodes() { var arr = new string[] { UnderlyingCode1.TrimToNull(), UnderlyingCode2.TrimToNull(), UnderlyingCode3.TrimToNull(), UnderlyingCode4.TrimToNull(), }; return arr.Where(n => !string.IsNullOrEmpty(n)).ToArray(); } public SyntheticUnderlying Clone() { return (SyntheticUnderlying)MemberwiseClone(); } public SyntheticPriceModel GetSyntheticPriceModel() { var sulist = new List(); var model = new SyntheticPriceModel { SuList = sulist, Constant = Constant ?? 0 }; if (!string.IsNullOrWhiteSpace(UnderlyingCode1)) { sulist.Add(new UnderlyingPriceModel { UnderlyingCode = UnderlyingCode1, Coefficient = Coefficient1 ?? 0 }); } if (!string.IsNullOrWhiteSpace(UnderlyingCode2)) { sulist.Add(new UnderlyingPriceModel { UnderlyingCode = UnderlyingCode2, Coefficient = Coefficient2 ?? 0 }); } if (!string.IsNullOrWhiteSpace(UnderlyingCode3)) { sulist.Add(new UnderlyingPriceModel { UnderlyingCode = UnderlyingCode3, Coefficient = Coefficient3 ?? 0 }); } if (!string.IsNullOrWhiteSpace(UnderlyingCode4)) { sulist.Add(new UnderlyingPriceModel { UnderlyingCode = UnderlyingCode4, Coefficient = Coefficient4 ?? 0 }); } return model; } } /// /// 标的价格模型 /// public class UnderlyingPriceModel { public string UnderlyingCode { get; set; } public double Price { get; set; } public double Coefficient { get; set; } public double? ContractSize { get; set; } } /// /// 合成价差期权价格结果Model /// public class SyntheticPriceModel { public double Constant { get; set; } public double Price { get; set; } public IEnumerable SuList { get; set; } } }