从山证v2.3.0拷贝
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
using YieldChain.Commons;
|
||||
using YLErp.Abstract.DataProviders;
|
||||
using YLErp.Models;
|
||||
|
||||
namespace YLErp.Modules.DataProviderModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 期权现价提供
|
||||
/// </summary>
|
||||
public class ExchangeOptionPriceProvider : IPriceProvider
|
||||
{
|
||||
readonly bool _initializeAll;
|
||||
readonly IDictionary<string, PriceModel> _priceDic;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="initializeAll">是否初始化全部数据</param>
|
||||
public ExchangeOptionPriceProvider(bool initializeAll = true)
|
||||
{
|
||||
_initializeAll = initializeAll;
|
||||
|
||||
_priceDic = initializeAll
|
||||
? InnerDataProvider.Default.GetPriceDic()
|
||||
: new Dictionary<string, PriceModel>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public double GetPrice(string optionCode)
|
||||
{
|
||||
return (InnerGetPrice(optionCode)?.Price) ?? 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool TryGetPrice(string optionCode, out double price)
|
||||
{
|
||||
var data = InnerGetPrice(optionCode);
|
||||
if (data != null)
|
||||
{
|
||||
price = data.Price;
|
||||
return true;
|
||||
}
|
||||
price = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool TryGetPriceModel(string optionCode, out PriceModel priceModel)
|
||||
{
|
||||
return null != (priceModel = InnerGetPrice(optionCode));
|
||||
}
|
||||
|
||||
private PriceModel InnerGetPrice(string optionCode)
|
||||
{
|
||||
if (string.IsNullOrEmpty(optionCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_priceDic.TryGetValue(optionCode, out var data))
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
if (_initializeAll)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var db = DbContextFactory.GetYLDbContext();
|
||||
|
||||
data = db.exchange_list_option.Where(n => n.ContractCode == optionCode)
|
||||
.Select(n => new PriceModel
|
||||
{
|
||||
InstrumentCode = n.ContractCode,
|
||||
Price = n.Price ?? 0,
|
||||
PriceTime = n.PriceTime
|
||||
}).FirstOrDefault();
|
||||
|
||||
_priceDic[optionCode] = data;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部数据提供以保证行情可以及时更新
|
||||
/// </summary>
|
||||
class InnerDataProvider
|
||||
{
|
||||
readonly ThrottleAction _updateThrottle;
|
||||
readonly Dictionary<string, PriceModel> _priceDic;
|
||||
|
||||
private InnerDataProvider()
|
||||
{
|
||||
_updateThrottle = new ThrottleAction(UpdatePrice, 10);
|
||||
_priceDic = new Dictionary<string, PriceModel>();
|
||||
}
|
||||
|
||||
private void UpdatePrice()
|
||||
{
|
||||
using var db = DbContextFactory.GetYLDbContext();
|
||||
|
||||
var date = DateTime.Today.AddDays(-10);
|
||||
|
||||
var arr = db.exchange_list_option.Where(n => n.MaturityDate > date && n.ContractCode != null)
|
||||
.Select(n => new PriceModel
|
||||
{
|
||||
InstrumentCode = n.ContractCode,
|
||||
Price = n.Price ?? 0,
|
||||
PriceTime = n.PriceTime
|
||||
}).ToArray();
|
||||
|
||||
foreach (var item in arr)
|
||||
{
|
||||
_priceDic[item.InstrumentCode] = item;
|
||||
}
|
||||
}
|
||||
|
||||
public IDictionary<string, PriceModel> GetPriceDic()
|
||||
{
|
||||
_updateThrottle.Execute();
|
||||
|
||||
return new Dictionary<string, PriceModel>(_priceDic);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单例
|
||||
/// </summary>
|
||||
public static readonly InnerDataProvider Default;
|
||||
|
||||
static InnerDataProvider()
|
||||
{
|
||||
Default = new InnerDataProvider();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user