Files
zszq-trs/YLErpUnitTest/Modules/MarginModule/MarginCalculation/Inner/eod_commodity_future_priceBLL.cs
T
2024-05-09 14:06:26 +08:00

115 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using YLErp.BLL;
namespace YLErp.Modules.MarginModule
{
public class eod_commodity_future_priceBLLBak
{
private readonly YLContext db = new YLContext();
public static List<string> DataSources = new List<string> { "系统", "人工" };
/// <summary>
/// 获取指定日期的收盘价或结算价
/// </summary>
/// <param name="codes">标的代码</param>
/// <param name="date">日期</param>
/// <param name="priceType">
/// 价格类型:
/// <para>0:最高价;</para>
/// <para>1:最低价;</para>
/// <para>2:收盘价;</para>
/// <para>3:结算价;</para>
/// </param>
/// <returns></returns>
public Dictionary<string, double> GetEodPrice(IEnumerable<string> codes, DateTime date, int priceType)
{
if (priceType < 0 || priceType > 3) { throw new ArgumentOutOfRangeException("priceType"); }
var result = new Dictionary<string, double>();
#region 商品期货
var futurePirce =
(from priceDb in db.eod_commodity_future_price
where priceDb.ValueDate == date
&& codes.Contains(priceDb.UnderlyingCode)
select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice, priceDb.SettlePrice })
.ToDictionary(
K => K.UnderlyingCode,
V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.SettlePrice; default: return 0; } }
, StringComparer.OrdinalIgnoreCase);
#endregion
#region 股票
var stockPirce =
(from priceDb in db.eod_stock_price
where priceDb.ValueDate == date
&& codes.Contains(priceDb.UnderlyingCode)
select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice })
.ToDictionary(
K => K.UnderlyingCode,
V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.ClosePrice; default: return 0; } }
, StringComparer.OrdinalIgnoreCase);
#endregion
#region 场内期权
var exchangePirce =
(from priceDb in db.eod_exchange_option_price
where priceDb.ValueDate == date
&& codes.Contains(priceDb.UnderlyingCode)
select new { priceDb.UnderlyingCode, priceDb.HighPrice, priceDb.LowPrice, priceDb.ClosePrice, priceDb.SettlePrice })
.ToDictionary(
K => K.UnderlyingCode,
V => { switch (priceType) { case 0: return V.HighPrice ?? 0; case 1: return V.LowPrice ?? 0; case 2: return V.ClosePrice; case 3: return V.SettlePrice; default: return 0; } }
, StringComparer.OrdinalIgnoreCase);
#endregion
var errorCode = new List<string>();
foreach (var item in codes)
{
if (!futurePirce.ContainsKey(item) &&
!stockPirce.ContainsKey(item) &&
!exchangePirce.ContainsKey(item))
{ errorCode.Add(item); }
if (futurePirce.ContainsKey(item))
{ result[item] = futurePirce[item]; }
else if (stockPirce.ContainsKey(item))
{ result[item] = stockPirce[item]; }
else if (exchangePirce.ContainsKey(item))
{ result[item] = exchangePirce[item]; }
else { result[item] = 0; }
}
if (errorCode.Count > 0)
{ throw new Exception($"{string.Join(",", errorCode)} 价格缺失"); }
return result;
}
/// <summary>
/// 获取指定日期的收盘价或结算价
/// </summary>
/// <param name="codes">标的代码</param>
/// <param name="date">日期</param>
/// <param name="priceType">
/// 价格类型:
/// <para>0:最高价;</para>
/// <para>1:最低价;</para>
/// <para>2:收盘价;</para>
/// <para>3:结算价;</para>
/// </param>
/// <returns></returns>
public Dictionary<int, double> GetEodPriceToId(IEnumerable<string> codes, DateTime date, int priceType)
{
var result = new Dictionary<int, double>();
var dict = GetEodPrice(codes, date, priceType);
foreach (var item in dict)
{
var um = DataCacheProvider.GetUnderlyingDataSource().GetData(item.Key);
if (um == null)
{
continue;
}
result[um.id] = item.Value;
}
return result;
}
}
}