using YLErp.Model;
using YLErp.Modules.CalculationModule;
using YLErp.Modules.EodModule;
namespace YLErp.Modules.ManagerAPI.EodModule
{
///
/// 日终持仓风险查询API
///
public class EodPositionRisksQueryApiService : YLBaseService
{
public EodPositionRisksQueryApiService(OptUserInfo userInfo) : base(userInfo)
{
}
///
/// 日终持仓风险查询
///
public IEnumerable QueryPositionRisks(EodPositionRisksQueryApiRequest req)
{
if (req.DividendRateType == "采集")
{
req.VolType = "分红率0";
}
var req2 = new EodPositionRisksReq
{
ValueDate = req.ValueDate,
VolType = req.VolType,
EodSettlePriceMode = req.SettlePriceMode,
rows = 999999,
page = 0
};
var clientNames = req.ClientNames;
var clientNumbers = req.ClientNumbers;
if (clientNames != null && clientNames.All(n => string.IsNullOrEmpty(n)))
{
clientNames = null;
}
if (clientNumbers != null && clientNumbers.All(n => string.IsNullOrEmpty(n)))
{
clientNumbers = null;
}
if (clientNames != null || clientNumbers != null)
{
req2.ClientIds = DataCacheProvider.GetClientDataSource().AsQueryable()
.Where(n => ((clientNames == null || clientNames.Contains(n.Name))
&& clientNumbers == null) || clientNumbers.Contains(n.Number))
.Select(n => n.id).ToList();
}
IEnumerable assetGroupIds = null;
if (req.AssetGroupNames != null && req.AssetGroupNames.Any(n => !string.IsNullOrEmpty(n)))
{
assetGroupIds = DataCacheProvider.GetAssetUnitGroupDataSource().AsQueryable()
.Where(n => req.AssetGroupNames.Contains(n.Name)).Select(n => n.id).ToList();
}
var assetNames = req.AssetBookNames;
if (assetNames != null && assetNames.All(n => string.IsNullOrEmpty(n)))
{
assetNames = null;
}
if (assetGroupIds != null || assetNames != null)
{
req2.BookIds = DataCacheProvider.GetAssetUnitDataSource().AsQueryable()
.Where(n => (assetGroupIds == null || assetGroupIds.Contains(n.GroupId)) && (assetNames == null || assetNames.Contains(n.Name)))
.Select(n => n.id).ToList();
}
if (req.UnderlyingCodes != null && req.UnderlyingCodes.Any(n => !string.IsNullOrEmpty(n)))
{
req2.UnderlyingIds = DataCacheProvider.GetUnderlyingDataSource().AsQueryable()
.Where(n => req.UnderlyingCodes.Contains(n.UnderlyingCode)).Select(n => n.id).ToList();
}
req2.TradeTypes = req.TradeTypes;
var result = new EodPositionRisksQueryService(OptUser).SearchList(req2, false);
return result.rows.Select(n => new EodPositionRiskItem
{
AssetBookName = n.AssetBookName,
TradeSide = n.BuySell,
ChildLeg = n.ChildLeg,
ClientName = n.ClientName,
ClientNumber = n.ClientNumber,
ContractCode = n.ContractCode,
CreditExposure = n.Exposure,
CurVolValue = n.CurrentVolatility,
Delta = n.Delta,
DeltaLots = n.DeltaLots,
DividendRate = n.DividendRate,
dPnLDelta = n.PnLDelta,
dPnLGamma = n.PnLGamma,
dPnLPsi = n.PnLPsi,
dPnLTheta = n.PnLTheta,
dPnLVega = n.PnLVega,
EtcTradePrice = n.etcTradePrice,
ExchangeOptionCode = n.ExchangeOptionCode,
ExerciseDate = n.ExerciseDate.OtcFormatDate(),
ExerciseMode = n.ExerciseMode,
Gamma = n.Gamma,
GammaLots = n.GammaLots,
InitialSpotPrice = n.InitialSpotPrice,
KnockInOutStatus = n.KnockInOutStatus,
OptionType = n.OptionType,
PositionMargin = n.Margin,
PositionPnl = PS.Config.IsPVRounded ? n.RoundedPositionPnl : n.PositionPnl,
PositionPV = PS.Config.IsPVRounded ? n.RoundedPV : n.PV,
RealizedPnl = n.RealizedPnl,
Rho = n.Rho,
RiskFreeRate = n.RiskFreeRate,
SinglePV = n.SinglePV,
StockEqvNotional = n.StockEqvNotionalToShow,
Strike = n.Strike,
Theta = n.Theta,
TradeAmount = n.TradeAmount,
TradeAmountV = n.trade == null ? n.TradeAmount : TradeCalcHelper.GetTradeAmountV(n.trade, n.TradeAmount ?? 0, 1),
TradeDate = n.TradeDate.OtcFormatDate(),
TradeNumber = n.TradeNumber,
TradeOriginalAmount = n.TradeOriginalAmount,
TradeOriginalAmountV = n.trade == null ? n.TradeAmount : TradeCalcHelper.GetTradeAmountV(n.trade, n.TradeOriginalAmount ?? 0, 1),
TradePrice = n.TradePrice,
TradeSinglePrice = n.TradeSinglePrice,
TradeType = n.TradeType,
UnderlyingCode = n.UnderlyingCode,
UnderlyingName = n.UnderlyingAssetName,
UnderlyingPrice = n.UnderlyingPrice,
Vega = n.Vega
});
}
///
/// 日终持仓风险查询_互换
///
public IEnumerable QuerySwapPositionRisks(EodSwapPositionRisksQueryApiRequest req)
{
var req2 = new EodPositionRisksReq
{
ValueDate = req.ValueDate,
EodSettlePriceMode = req.SettlePriceMode,
rows = 999999,
page = 0
};
var clientNames = req.ClientNames;
var clientNumbers = req.ClientNumbers;
if (clientNames != null && clientNames.All(n => string.IsNullOrEmpty(n)))
{
clientNames = null;
}
if (clientNumbers != null && clientNumbers.All(n => string.IsNullOrEmpty(n)))
{
clientNumbers = null;
}
if (clientNames != null || clientNumbers != null)
{
req2.ClientIds = DataCacheProvider.GetClientDataSource().AsQueryable()
.Where(n => ((clientNames == null || clientNames.Contains(n.Name))
&& clientNumbers == null) || clientNumbers.Contains(n.Number))
.Select(n => n.id).ToList();
}
IEnumerable assetGroupIds = null;
if (req.AssetGroupNames != null && req.AssetGroupNames.Any(n => !string.IsNullOrEmpty(n)))
{
assetGroupIds = DataCacheProvider.GetAssetUnitGroupDataSource().AsQueryable()
.Where(n => req.AssetGroupNames.Contains(n.Name)).Select(n => n.id).ToList();
}
var assetNames = req.AssetBookNames;
if (assetNames != null && assetNames.All(n => string.IsNullOrEmpty(n)))
{
assetNames = null;
}
if (assetGroupIds != null || assetNames != null)
{
req2.BookIds = DataCacheProvider.GetAssetUnitDataSource().AsQueryable()
.Where(n => (assetGroupIds == null || assetGroupIds.Contains(n.GroupId)) && (assetNames == null || assetNames.Contains(n.Name)))
.Select(n => n.id).ToList();
}
if (req.UnderlyingCodes != null && req.UnderlyingCodes.Any(n => !string.IsNullOrEmpty(n)))
{
req2.UnderlyingIds = DataCacheProvider.GetUnderlyingDataSource().AsQueryable()
.Where(n => req.UnderlyingCodes.Contains(n.UnderlyingCode)).Select(n => n.id).ToList();
}
var result = new EodPositionRisksQueryService(OptUser).SearchList(req2, true);
var umProvider = DataCacheProvider.GetUnderlyingDataSource();
return result.rows.Select(n =>
{
var swap = n.trade.trade_swap;
n.trade.MetaDic.TryGetValue("GetUnderlyingName", out var GetUnderlyingName);
n.trade.MetaDic.TryGetValue("PayUnderlyingName", out var PayUnderlyingName);
return new EodSwapPositionRiskItem
{
AssetBookName = n.AssetBookName,
ClientName = n.ClientName,
ClientNumber = n.ClientNumber,
ContractCode = n.ContractCode,
Delta = n.Delta,
DeltaLots = n.DeltaLots,
ExerciseDate = n.ExerciseDate.OtcFormatDate(),
PositionPnl = PS.Config.IsPVRounded ? n.RoundedPositionPnl : n.PositionPnl,
PositionPV = PS.Config.IsPVRounded ? n.RoundedPV : n.PV,
RealizedPnl = n.RealizedPnl,
StockEqvNotional = n.StockEqvNotionalToShow,
TradeDate = n.TradeDate.OtcFormatDate(),
TradeNumber = n.ParentTradeNumber,
TradeType = n.TradeType,
GetLongShort = swap.GetLongShort,
GetMarginRate = swap.GetMarginRate,
GetSpotPrice = swap.GetSpotPrice,
GetSwapTimeAndRate = swap.GetSwapTimeAndRate,
GetTradeAmount = swap.GetTradeAmount,
GetTradePrice = swap.GetTradePrice,
GetUnderlyingCode = swap.GetUnderlyingCode,
GetUnderlyingName = GetUnderlyingName.TrimToNull() ?? (umProvider.GetData(swap.GetUnderlyingCode)?.UnderlyingName),
IsGetFloatingProfit = swap.IsGetFloatingProfit,
IsPayFloatingProfit = swap.IsPayFloatingProfit,
PayLongShort = swap.PayLongShort,
PayMarginRate = swap.PayMarginRate,
PaySpotPrice = swap.PaySpotPrice,
PaySwapTimeAndRate = swap.PaySwapTimeAndRate,
PayTradeAmount = swap.PayTradeAmount,
PayTradePrice = swap.PayTradePrice,
PayUnderlyingCode = swap.PayUnderlyingCode,
PayUnderlyingName = PayUnderlyingName.TrimToNull() ?? (umProvider.GetData(swap.PayUnderlyingCode)?.UnderlyingName)
};
});
}
}
}