150 lines
4.5 KiB
C#
150 lines
4.5 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using YieldChain.Commons;
|
|
using YLErp.Cache;
|
|
using YLErp.Modules.CalculationModule;
|
|
using static YLErp.ConsGlobal;
|
|
|
|
namespace YLErp.Modules.TradeRiskCalcModule.TaskRunner
|
|
{
|
|
/// <summary>
|
|
/// 场内实时持仓运行
|
|
/// </summary>
|
|
class ExchangePositionRunner : InnerRunnerBase
|
|
{
|
|
|
|
readonly static IYLCache ylCache;
|
|
static ExchangePositionRunner()
|
|
{
|
|
ylCache = YLServiceLocator.ServiceProvider.GetService<IYLCache>();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return "场内持仓";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对冲持仓计算结果
|
|
/// </summary>
|
|
public ExchangePositionCalcResult Result { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 执行任务
|
|
/// </summary>
|
|
public override void StartTask(InnerRunTaskData taskData)
|
|
{
|
|
if (!taskData.IsReady)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (IsRunning)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (IsRunning)
|
|
{
|
|
//120秒的超时时间
|
|
if (IsTimeOut(120))
|
|
{
|
|
CancelTask(5000);
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lock (this)
|
|
{
|
|
//3秒的频次控制
|
|
if (IsRunning || !IsTimeOut(3))
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
AppManager.SetSysInfo("场内实时持仓API-计算开始", "开始");
|
|
|
|
InnerStartTask(() =>
|
|
{
|
|
//获取所有未结算过的对冲交易
|
|
var newTrades = taskData.TradeDataSource.GetExchangeTrades() ?? Enumerable.Empty<ExchangeTrade>();
|
|
|
|
//昨日持仓信息
|
|
var lastEodPositions = taskData.DataProvider.YdEodPositionDataProvider.GetExchangeTradePositionList("持仓").ToArray();
|
|
|
|
var resultItems = new ExchangePositionCalcService(taskData.ValueDate, new ExchangeTradeCommissionCalc())
|
|
.Calculate(newTrades, lastEodPositions, taskData.DataProvider.ExchangeOptionPriceProvider);
|
|
|
|
//如果结果的开始时间大于当前任务的开始时间则丢失计算结果
|
|
var result = GetResult();
|
|
if (result == null || result.StartTime < StartTime)
|
|
{
|
|
result = new ExchangePositionCalcResult(StartTime)
|
|
{
|
|
Items = resultItems,
|
|
ValueDate = taskData.ValueDate,
|
|
LastDate = taskData.PreSettleDate
|
|
};
|
|
|
|
SaveResult(result);
|
|
|
|
AppManager.SetSysInfo("场内实时持仓API-计算完成", "耗时" + (DateTime.Now - StartTime).TotalSeconds.ToString("F") + "ms");
|
|
}
|
|
|
|
}).ContinueWith(t =>
|
|
{
|
|
if (t.Exception != null)
|
|
{
|
|
LogFactory.GetLogger("实时风险").Error(t.Exception, $"[场内实时持仓API]计算出错");
|
|
}
|
|
else
|
|
{
|
|
LogFactory.GetLogger("实时风险").Debug($"[场内实时持仓API]计算完成");
|
|
}
|
|
|
|
InnerCompleteTask();
|
|
});
|
|
}
|
|
|
|
|
|
private const string ExchangePositionCalcResultRedisKey = "ExchangePosition";
|
|
|
|
/// <summary>
|
|
/// 将结果保存
|
|
/// </summary>
|
|
private void SaveResult(ExchangePositionCalcResult result)
|
|
{
|
|
if (ylCache.CacheEnable())
|
|
{
|
|
ylCache.StringSet<ExchangePositionCalcResult>($"{ExchangePositionCalcResultRedisKey}", result);
|
|
}
|
|
else
|
|
{
|
|
Result = result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询结果
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public ExchangePositionCalcResult GetResult()
|
|
{
|
|
if (ylCache.CacheEnable())
|
|
{
|
|
return ylCache.StringGet<ExchangePositionCalcResult>($"{ExchangePositionCalcResultRedisKey}");
|
|
}
|
|
else
|
|
{
|
|
return Result;
|
|
}
|
|
}
|
|
}
|
|
}
|