58 lines
2.7 KiB
C#
58 lines
2.7 KiB
C#
using YLErp.BLL;
|
|
|
|
namespace YLErp.Modules.ToolsModule
|
|
{
|
|
public class EodCommodityFuturePriceService : YLBaseService
|
|
{
|
|
public EodCommodityFuturePriceService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取收盘价或结算价差值
|
|
/// </summary>
|
|
/// <param name="codes">标的</param>
|
|
/// <param name="date1">日期被减数</param>
|
|
/// <param name="date2">日期减数</param>
|
|
/// <param name="isSettlePrice">
|
|
/// 价格模式
|
|
/// <para>true:结算价</para>
|
|
/// <para>false:收盘价</para>
|
|
/// <para>默认为根据系统设置选择收盘价或结算价</para>
|
|
/// </param>
|
|
/// <returns>
|
|
/// <para>Key:code</para>
|
|
/// <para>Value:差值</para>
|
|
/// </returns>
|
|
public Dictionary<string, double> GetPriceDiff(List<string> codes, DateTime date1, DateTime date2, bool? isSettlePrice = null)
|
|
{
|
|
string date1Str = date1.ToString("yyyyMMdd");
|
|
string date2Str = date2.ToString("yyyyMMdd");
|
|
Dictionary<string, double> result = new Dictionary<string, double>();
|
|
if (codes == null || codes.Count == 0) { return result; }
|
|
if (isSettlePrice == null) { isSettlePrice = valuedateBLL.SystemDate.EodSettlePriceMode == "结算价"; }
|
|
using (YLContext context = new YLContext())
|
|
{
|
|
Dictionary<string, List<eod_commodity_future_price>> dict =
|
|
(from left in (from db in context.eod_commodity_future_price where db.ValueDate == date1 && codes.Contains(db.UnderlyingCode) select db)
|
|
join right in (from db in context.eod_commodity_future_price where db.ValueDate >= date2 && db.ValueDate < date1 && codes.Contains(db.UnderlyingCode) orderby db.ValueDate descending select db).Take(1)
|
|
on left.UnderlyingCode equals right.UnderlyingCode into temp
|
|
from right in temp.DefaultIfEmpty()
|
|
select new { left, right })
|
|
.ToDictionary(O => O.left.UnderlyingCode, O => new List<eod_commodity_future_price>() { O.left, O.right });
|
|
for (int i = 0; i < codes.Count; i++)
|
|
{
|
|
if (!dict.ContainsKey(codes[i]))
|
|
{ result[codes[i]] = 1; continue; }
|
|
|
|
if (isSettlePrice.Value)
|
|
{ result[codes[i]] = dict[codes[i]][0].SettlePrice - ((dict[codes[i]][1]?.SettlePrice) ?? 0); }
|
|
else { result[codes[i]] = dict[codes[i]][0].ClosePrice - ((dict[codes[i]][1]?.ClosePrice) ?? 0); }
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|