179 lines
6.5 KiB
C#
179 lines
6.5 KiB
C#
using YLErp.Abstract;
|
|
using YLErp.Abstract.DataProviders;
|
|
using YLErp.BLL;
|
|
using YLErp.Enums;
|
|
using YLErp.Modules.CalculationModule.Abstract;
|
|
using YLErp.Modules.DataProviderModule;
|
|
|
|
namespace YLErp.Modules.CalculationModule
|
|
{
|
|
/// <summary>
|
|
/// 持仓风险对冲计算上下文
|
|
/// </summary>
|
|
public class HedgePnlCalcContext : IHedgePnlCalcContext
|
|
{
|
|
#region----属性定义----
|
|
|
|
/// <summary>
|
|
/// 估值日
|
|
/// </summary>
|
|
public DateTime ValueDate { get; }
|
|
|
|
/// <summary>
|
|
/// 计算场景
|
|
/// </summary>
|
|
public CalcScenarioEnum CalcScenario { get; }
|
|
|
|
/// <summary>
|
|
/// 波动率类型
|
|
/// </summary>
|
|
public string VolType { get; }
|
|
|
|
public bool IsEodCalc { get; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public IPriceProvider UnderlyingPriceProvider { get; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public IPriceProvider UnderlyingSettlePriceProvider { get; }
|
|
|
|
/// <summary>
|
|
/// 场内标的价格提供者
|
|
/// </summary>
|
|
public IPriceProvider ExchangeOptionPriceProvider { get; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public IUnderlyingDataProvider UnderlyingDataProvider { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// 对冲交易佣金计算接口
|
|
/// </summary>
|
|
public IExchangeTradeCommissionCalc CommissionCalc { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// 错误处理接口
|
|
/// </summary>
|
|
public IErrorHandler ErrorHandler { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public OptUserInfo OptUser { get; }
|
|
|
|
/// <summary>
|
|
/// 场内期权价格使用选项(默认SetExOptionPrice)
|
|
/// 注意:在计算类中光子将忽略此项并固定为CalcPv
|
|
/// </summary>
|
|
public ExchangeOptionPriceUseFlag ExchangeOptionPriceUseFlag { get; set; } = ExchangeOptionPriceUseFlag.SetExOptionPrice;
|
|
|
|
#endregion
|
|
|
|
#region----构造函数----
|
|
|
|
public HedgePnlCalcContext(CalcScenarioEnum calcScenario, DateTime valueDate, string volType, bool isEodCalc
|
|
, IPriceProvider underlyingPriceProvider, IPriceProvider underlyingSettlePriceProvider,
|
|
IPriceProvider exchangeOptionPriceProvider, OptUserInfo optUser
|
|
, IExchangeTradeCommissionCalc commissionCalc = null
|
|
, IUnderlyingDataProvider underlyingDataProvider = null)
|
|
{
|
|
if (string.IsNullOrEmpty(volType))
|
|
{
|
|
throw new ArgumentException("volType不能为空", nameof(volType));
|
|
}
|
|
|
|
VolType = volType;
|
|
IsEodCalc = isEodCalc;
|
|
ValueDate = valueDate;
|
|
CalcScenario = calcScenario;
|
|
OptUser = optUser ?? throw new ArgumentNullException(nameof(optUser));
|
|
UnderlyingPriceProvider = underlyingPriceProvider ?? throw new ArgumentNullException(nameof(underlyingPriceProvider));
|
|
UnderlyingSettlePriceProvider = underlyingSettlePriceProvider ?? throw new ArgumentNullException(nameof(underlyingPriceProvider));
|
|
ExchangeOptionPriceProvider = exchangeOptionPriceProvider ?? throw new ArgumentNullException(nameof(exchangeOptionPriceProvider));
|
|
|
|
CommissionCalc = commissionCalc ?? new ExchangeTradeCommissionCalc();
|
|
UnderlyingDataProvider = underlyingDataProvider ?? new UnderlyingDataProvider();
|
|
}
|
|
|
|
public HedgePnlCalcContext(IOtcTradeValueCalcContext optionCalcContext,
|
|
IExchangeTradeCommissionCalc tradeCommissionCalc, IPriceProvider exchangeOptionPriceProvider)
|
|
{
|
|
if (optionCalcContext is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(optionCalcContext));
|
|
}
|
|
|
|
CommissionCalc = tradeCommissionCalc ?? throw new ArgumentNullException(nameof(tradeCommissionCalc));
|
|
ExchangeOptionPriceProvider = exchangeOptionPriceProvider ?? throw new ArgumentNullException(nameof(exchangeOptionPriceProvider));
|
|
|
|
var dataProvider = CalcCheckHelper.CheckOptionCalcDataProvider(optionCalcContext.DataProvider);
|
|
UnderlyingDataProvider = dataProvider.UnderlyingDataProvider;
|
|
UnderlyingPriceProvider = dataProvider.UnderlyingPriceProvider;
|
|
UnderlyingSettlePriceProvider = dataProvider.UnderlyingPriceProvider;
|
|
|
|
VolType = optionCalcContext.VolType;
|
|
ValueDate = optionCalcContext.ValueDate;
|
|
CalcScenario = optionCalcContext.CalcScenario;
|
|
ErrorHandler = optionCalcContext.ErrorHandler;
|
|
|
|
OptUser = OptUserInfo.SystemUser;
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 创建期权计算上下文对象
|
|
/// </summary>
|
|
public virtual IOtcTradeValueCalcContext CreateOptionCalculateContext()
|
|
{
|
|
var sysRiskFreeRate = valuedateBLL.RiskFreeRate * 0.01;
|
|
var dataProvider = new InnerOptionCalcDataProvider(this);
|
|
return new OptionValueCalcContext(VolType, IsEodCalc, ValueDate, sysRiskFreeRate, dataProvider)
|
|
{
|
|
AddingVolRate = 0,
|
|
ErrorHandler = ErrorHandler,
|
|
CalcScenario = CalcScenario
|
|
};
|
|
}
|
|
|
|
#region----InnerOptionCalcDataProvider----
|
|
|
|
class InnerOptionCalcDataProvider : IOptionCalcDataProvider
|
|
{
|
|
public InnerOptionCalcDataProvider(HedgePnlCalcContext context)
|
|
{
|
|
UnderlyingDataProvider = context.UnderlyingDataProvider;
|
|
UnderlyingPriceProvider = context.UnderlyingPriceProvider;
|
|
UnderlyingSettlePriceProvider = context.UnderlyingSettlePriceProvider;
|
|
VolatilityDataProvider = new VolatilityDataProvider(context.ValueDate);
|
|
TradeExtendDataProvider = new TradeExtendDataProvider(context.OptUser);
|
|
}
|
|
|
|
public IPriceProvider UnderlyingPriceProvider { get; }
|
|
public IPriceProvider UnderlyingSettlePriceProvider { get; }
|
|
|
|
public IUnderlyingDataProvider UnderlyingDataProvider { get; }
|
|
|
|
public ITradeExtendDataProvider TradeExtendDataProvider { get; }
|
|
|
|
public IVolatilityDataProvider VolatilityDataProvider { get; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
public HedgePnlCalc GetHedgePnlCalc()
|
|
{
|
|
return new HedgePnlCalc(this);
|
|
}
|
|
public MaturityOptionHedgePnlCalc GetOptionHedgePnlCalc()
|
|
{
|
|
return new MaturityOptionHedgePnlCalc(this);
|
|
}
|
|
}
|
|
}
|