53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using System.Collections.Concurrent;
|
|
using YLErp.Abstract.DataProviders;
|
|
using YLErp.Modules.DataCacheModule;
|
|
|
|
namespace YLErp.Modules.TradeRiskCalcModule.TaskRunner
|
|
{
|
|
/// <summary>
|
|
/// 标的价格提供源,非线程安全,
|
|
/// 此价格数据源保证计算中同一标的的价格唯一,
|
|
/// 不受DataCacheManager的更新频率影响
|
|
/// </summary>
|
|
class TradeRiskCalcPriceProvider : IPriceProvider
|
|
{
|
|
readonly ConcurrentDictionary<string, double> _priceDic;
|
|
|
|
public TradeRiskCalcPriceProvider()
|
|
{
|
|
_priceDic = new ConcurrentDictionary<string, double>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除掉价格数据(用于进入下一循环的计算)
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
_priceDic.Clear();
|
|
}
|
|
|
|
public double GetPrice(string instrumentId)
|
|
{
|
|
return TryGetPrice(instrumentId, out var price) ? price : 0;
|
|
}
|
|
|
|
public bool TryGetPrice(string underlyingCode, out double price)
|
|
{
|
|
if (string.IsNullOrEmpty(underlyingCode))
|
|
{
|
|
price = 0;
|
|
return false;
|
|
}
|
|
|
|
if (_priceDic.TryGetValue(underlyingCode, out price))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
var result = DataCacheManager.GetUnderlyingDataSource().TryGetPrice(underlyingCode, out price);
|
|
_priceDic[underlyingCode] = price;
|
|
return result;
|
|
}
|
|
}
|
|
}
|