168 lines
7.8 KiB
C#
168 lines
7.8 KiB
C#
using YLErp.BLL.Calculation;
|
|
using YLErp.QdpModule;
|
|
|
|
namespace YLErp.BLL
|
|
{
|
|
public class underlying_main_contractBLL
|
|
{
|
|
public static bool IsListOld = true;
|
|
|
|
private static List<underlying_main_contract> Allunderlying_main_contractModel = null;
|
|
|
|
public static List<underlying_main_contract> GetAllunderlying_main_contractModel()
|
|
{
|
|
if (IsListOld)
|
|
{
|
|
using (YLContext con = new YLContext())
|
|
{
|
|
Allunderlying_main_contractModel = con.underlying_main_contract.ToList();
|
|
}
|
|
IsListOld = false;
|
|
}
|
|
return Allunderlying_main_contractModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 过滤掉不符合规则的标的
|
|
/// 国君规则: 标的到期日前一个月15号必需大于输入的到期日参数maturitydate(到期日为当前时间一个月后的第一个工作日)
|
|
/// </summary>
|
|
public static List<underlying_manager> GetGtja_MaturityUnderlying(List<underlying_manager> tofilterUnderlyings, DateTime maturityDate)
|
|
{
|
|
//调整:
|
|
//原来的逻辑是:underlyingMaturityDate > (maturityDate + 1 month).15th
|
|
//修正后的逻辑是: (underlyingMaturityDate - 1 month).15th > maturityDate
|
|
var underlyings = new List<underlying_manager>();
|
|
foreach (var x in tofilterUnderlyings)
|
|
{
|
|
if (x.IsVirtualContract() || !x.IsFutures())
|
|
{
|
|
underlyings.Add(x);
|
|
}
|
|
else
|
|
{
|
|
var underlyingMaturityDate = DateTime.Now;
|
|
if (x.MaturityDate == null)
|
|
{
|
|
underlyingMaturityDate = QdpHelper.defaultMaturityDateFromContract(x.UnderlyingCode);
|
|
}
|
|
else
|
|
{
|
|
underlyingMaturityDate = CalculatorHelper.GetContractMaturityDateForQuote(x.MaturityDate.Value, x.UnderlyingCode);
|
|
}
|
|
|
|
var underlyingCodeArray = x.UnderlyingCode.TakeWhile(c => char.IsLetter(c)).ToArray();
|
|
|
|
var mainUnderlyingCode = new string(underlyingCodeArray) + "00";
|
|
int.TryParse(x.UnderlyingCode.Substring(x.UnderlyingCode.Length - 2), out var monthInCode);
|
|
var beforUnderlyingCode = x.UnderlyingCode.Substring(0, x.UnderlyingCode.Length - 2) + (monthInCode - 1).ToString("00");
|
|
var mainUnderlying = YLErp.Modules.DataCacheProvider.GetUnderlyingDataSource().GetData(mainUnderlyingCode);
|
|
//是否为“主力合约”,或上一个为“主力合约”
|
|
var IsMainUnderlying = x.UnderlyingCode == mainUnderlying.TradeCode || beforUnderlyingCode == mainUnderlying.TradeCode;
|
|
|
|
|
|
|
|
var checkDate = underlyingMaturityDate;
|
|
//做个月份补丁,如果是1809默认是9月份
|
|
//没有考虑到1月这种情况,需要测试
|
|
//if (int.TryParse(x.UnderlyingCode.Substring(x.UnderlyingCode.Length - 2), out int tempData) && tempData > 1 && tempData <= 12 && x.UnderlyingInstrumentType != "Stock")
|
|
//{
|
|
// checkDate = new DateTime(underlyingMaturityDate.Year, tempData, 15);
|
|
//}
|
|
if (IsMainUnderlying)
|
|
{
|
|
//当前所报的合约的到期日必须小于该合约的到期日-10个交易日
|
|
checkDate = QdpCalendarHelper.BizDayShift(checkDate, -10);
|
|
if (checkDate >= maturityDate)
|
|
{
|
|
underlyings.Add(x);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
checkDate = new DateTime(underlyingMaturityDate.Year, underlyingMaturityDate.Month, 15);
|
|
checkDate = checkDate.AddMonths(-1);
|
|
if (PS.Config.Company==Configuration.CompanyEnum.国贸启润)
|
|
{
|
|
checkDate = underlyingMaturityDate;
|
|
}
|
|
if (checkDate >= maturityDate)
|
|
{
|
|
underlyings.Add(x);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return underlyings;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取给定到期日的平值期权报价列表
|
|
/// 这个函数需要给Web和API使用,因此不使用缓存,直接从数据库查询
|
|
/// 国君规则: 标的到日期前一个月15号必需大于 maturitydate(到期日为当前时间一个月后的第一个工作日)
|
|
/// </summary>
|
|
/// <param name="maturityDate">报价到期日</param>
|
|
/// <param name="onlyMainContract">是否只包含主力合约</param>
|
|
public static List<underlying_manager> GetDefaultAtMoneyQuoteList(DateTime maturityDate, bool onlyMainContract = true)
|
|
{
|
|
var del = new char[] { ',' };
|
|
|
|
using (var db = new YLContext())
|
|
{
|
|
//筛选所有需要报价的品种,并将其对应的合约代码加入codes
|
|
var activeAssetList = GetAllunderlying_main_contractModel().Where(x => x.NeedQuote == 1).OrderBy(n => n.UnderlyingType).ToList();
|
|
string preUnderlyingType = null;
|
|
var unCodeSet = new HashSet<string>();
|
|
foreach (var asset in activeAssetList.ToArray())
|
|
{
|
|
if (preUnderlyingType == asset.UnderlyingType ||
|
|
string.IsNullOrWhiteSpace(asset.UnderlyingType) ||
|
|
string.IsNullOrWhiteSpace(asset.UnderlyingCode))
|
|
{
|
|
activeAssetList.Remove(asset);
|
|
}
|
|
else
|
|
{
|
|
preUnderlyingType = asset.UnderlyingType;
|
|
var arr = asset.UnderlyingCode.Split(del, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var item in arr)
|
|
{
|
|
unCodeSet.Add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
//需要获取underlying最新的价格,所以需要从数据库读取
|
|
var underlyings = db.underlying_manager.Where(x => unCodeSet.Contains(x.UnderlyingCode) && x.LaunchState == "1").ToList();
|
|
underlyings = GetGtja_MaturityUnderlying(underlyings, maturityDate);
|
|
|
|
if (onlyMainContract)
|
|
{
|
|
var quoteContracts = new Dictionary<string, underlying_manager>();
|
|
//每个品种,满足条件的合约中到日期最早的那个
|
|
foreach (var underlying in underlyings)
|
|
{
|
|
if ((!quoteContracts.ContainsKey(underlying.UnderlyingType))
|
|
|| underlying.MaturityDate < quoteContracts[underlying.UnderlyingType].MaturityDate)
|
|
{
|
|
quoteContracts[underlying.UnderlyingType] = underlying;
|
|
}
|
|
}
|
|
underlyings = quoteContracts.Values.ToList();
|
|
}
|
|
|
|
var dic = activeAssetList.ToDictionary(n => n.UnderlyingType);
|
|
foreach (var un in underlyings)
|
|
{
|
|
if (dic.TryGetValue(un.UnderlyingType, out var asset))
|
|
{
|
|
un.order = asset.OrderId;
|
|
}
|
|
}
|
|
|
|
underlyings.Sort((x, y) => (x.order ?? 0) - (y.order ?? 0));
|
|
return underlyings;
|
|
}
|
|
}
|
|
}
|
|
}
|