Files
2024-05-09 14:06:26 +08:00

144 lines
5.5 KiB
C#

namespace YLErp.Modules.UnderlyingModule.ApiModudle
{
/// <summary>
/// API数据检索服务
/// </summary>
public class ApiDataQueryService : YLBaseService
{
public ApiDataQueryService(OptUserInfo userInfo) : base(userInfo)
{
}
public IEnumerable<ApiUnderlyingInfo> GetUnderlyingList()
{
var date = DateTime.Now.AddMonths(-3);
var underlyings = DbContext.underlying_manager.AsNoTracking()
.Where(n => n.UnderlyingInstrumentType != "CommodityFutures" || n.MaturityDate > date)
.Select(n => new
{
n.UnderlyingCode,
n.ContractSize,
n.UnderlyingInstrumentType,
n.UnderlyingName,
n.MarketCode,
n.CommodityCode,
n.MaturityDate,
n.PriceTick,
n.MarginRate,
n.OpenDate,
n.UpDownLimit,
n.VolatilityRate,
n.UnderlyingTypeId
}).ToList();
var varietyDic = DbContext.variety.Select(n => new { n.id, n.VolatilityRate, n.UpLimit, n.Margin }).ToDictionary(n => n.id);
return underlyings.Select(x =>
{
var contractSize = x.ContractSize;
var contractType = UnderlyingContractTypeEnum.None;
switch (x.UnderlyingInstrumentType)
{
case "Stock":
contractSize = 100;
contractType = UnderlyingContractTypeEnum.Stocks;
break;
case "CommodityFutures":
contractType = UnderlyingContractTypeEnum.Futures;
break;
case "CommoditySpot":
contractType = UnderlyingContractTypeEnum.Spot;
break;
}
var marginRate = x.MarginRate;
double? volatilitySpan = null, updownLimit = null;
bool isUpdownFixed = false;
if (NumberHelper.TryParse(x.VolatilityRate, out var dvalue, out bool isPercent))
{
volatilitySpan = dvalue;
}
if (NumberHelper.TryParse(x.UpDownLimit, out dvalue, out isPercent))
{
updownLimit = dvalue;
isUpdownFixed = !isPercent;
}
//如果标的的几个幅度没有则取品种的
if (varietyDic.TryGetValue(x.UnderlyingTypeId, out var va))
{
if (!marginRate.HasValue)
{
marginRate = va.Margin;
}
if (!volatilitySpan.HasValue && NumberHelper.TryParse(va.VolatilityRate, out dvalue, out isPercent))
{
volatilitySpan = dvalue;
}
if (!updownLimit.HasValue && NumberHelper.TryParse(va.UpLimit, out dvalue, out isPercent))
{
updownLimit = dvalue;
isUpdownFixed = false;
}
}
return new ApiUnderlyingInfoEx
{
Code = x.UnderlyingCode,
Name = x.UnderlyingName,
Exchange = x.MarketCode,
Product_Class = x.CommodityCode,
Expire_Date = x.MaturityDate?.ToString("yyyy-MM-dd"),
Multiple = contractSize,
Price_Tick = x.PriceTick,
Long_Margin_Ratio = x.MarginRate ?? 0,
Short_Margin_Ratio = x.MarginRate ?? 0,
Contract_Type = contractType,
Create_Date = x.OpenDate?.ToString("yyyy-MM-dd"),
OptionType = OptionTypeEnum.None,
Underlying_Code = "",
MarginRate = marginRate ?? 0,
IsUpdownLimitFixed = isUpdownFixed,
UpdownLimit = updownLimit ?? 0,
VolatilitySpan = volatilitySpan ?? 0
};
}).ToArray();
}
public IEnumerable<ApiUnderlyingInfo> GetExchangeOptionList()
{
var date = DateTime.Now.AddMonths(-1);
var exchangeOptions = DbContext.exchange_list_option.Where(x => x.MaturityDate >= date).ToList();
return exchangeOptions.Select(x =>
{
return new ApiUnderlyingInfo
{
Code = x.ContractCode,
Underlying_Code = x.UnderlyingCode,
Exchange = x.MarketCode,
Create_Date = x.OpenDate?.ToString("yyyy-MM-dd") ?? "",
Expire_Date = x.MaturityDate.ToString("yyyy-MM-dd"),
Strike = x.Strike,
Contract_Type = UnderlyingContractTypeEnum.Options,
OptionType = x.OptionType == "看涨" ? OptionTypeEnum.Call : OptionTypeEnum.Put,
Long_Margin_Ratio = x.MarginRate ?? 0,
Short_Margin_Ratio = x.MarginRate ?? 0,
Multiple = x.ContractSize,
Name = string.Empty,
Price_Tick = x.PriceTick,
Product_Class = string.Empty
};
}).ToArray();
}
}
}