110 lines
3.0 KiB
C#
110 lines
3.0 KiB
C#
using YLErp.DBModels.Helpers;
|
|
|
|
namespace YLErp.Modules.CalculationModule
|
|
{
|
|
/// <summary>
|
|
/// 交易手数计算
|
|
/// </summary>
|
|
public static class TradeLotsCalc
|
|
{
|
|
/// <summary>
|
|
/// 根据标的计算手数,返回null表示未获取到合约乘数
|
|
/// </summary>
|
|
public static double GetLots(string underlyingCode, double notional)
|
|
{
|
|
var underlying = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode);
|
|
|
|
if (underlying == null)
|
|
{
|
|
return notional;
|
|
}
|
|
|
|
double contractSize;
|
|
|
|
if (underlying.ContractSize > 0)
|
|
{
|
|
contractSize = underlying.ContractSize;
|
|
}
|
|
else if (underlying.IsStock())
|
|
{
|
|
contractSize = 100;
|
|
}
|
|
else
|
|
{
|
|
var variety = DataCacheProvider.GetVarietyDataSource().GetData(underlying?.UnderlyingTypeId ?? 0);
|
|
if (variety == null)
|
|
{
|
|
return notional;
|
|
}
|
|
contractSize = variety.TradeUnitValue ?? 0;
|
|
}
|
|
|
|
if (contractSize < 1)
|
|
{
|
|
return notional;
|
|
}
|
|
|
|
return notional / contractSize;
|
|
}
|
|
|
|
public static double CalcDeltaInLots(double Delta, Variety variety, underlying_manager udm)
|
|
{
|
|
return Delta / GetTradeUnitValue(variety, udm);
|
|
}
|
|
|
|
public static double CalcGammaInLots(double gamma, Variety variety, underlying_manager udm)
|
|
{
|
|
return gamma / GetTradeUnitValue(variety, udm);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取标的的交易手数单位,一手多少
|
|
/// </summary>
|
|
public static double GetTradeUnitValue(Variety variety, underlying_manager udm)
|
|
{
|
|
if (udm is null)
|
|
{
|
|
if (variety != null)
|
|
{
|
|
var TradeUnit = VarietyHelper.GetTradeUnitValue(variety.VarietyCode, variety.TradeUnit);
|
|
return TradeUnit > 0 ? TradeUnit.Value : 1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
if (udm.ContractSize > 1)
|
|
{
|
|
return udm.ContractSize;
|
|
}
|
|
|
|
if (udm.IsStock())
|
|
{
|
|
return 100;
|
|
}
|
|
|
|
if (udm.IsSynthetic())
|
|
{
|
|
return udm.ContractSize < 1 ? 1 : udm.ContractSize;
|
|
}
|
|
|
|
if (udm.ContractSize > 0)
|
|
{
|
|
return udm.ContractSize;
|
|
}
|
|
|
|
if (variety == null)
|
|
{
|
|
variety = DataCacheProvider.GetVarietyDataSource().GetData(udm.UnderlyingTypeId);
|
|
|
|
if (variety == null)
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
var TradeUnit2 = VarietyHelper.GetTradeUnitValue(variety.VarietyCode, variety.TradeUnit);
|
|
return TradeUnit2 > 0 ? TradeUnit2.Value : 1;
|
|
}
|
|
}
|
|
}
|