145 lines
5.2 KiB
C#
145 lines
5.2 KiB
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using YLErp.Abstract;
|
|
using YLErp.Abstract.DataProviders;
|
|
using YLErp.BLL;
|
|
using YLErp.DBModels;
|
|
using YLErp.Models;
|
|
using YLErp.Modules.CalculationModule.Abstract;
|
|
using YLErp.Modules.DataProviderModule;
|
|
using YLErp.Modules.VolatilityModule;
|
|
using YLErp.QdpModule;
|
|
|
|
namespace YLErp.Modules.CalculationModule
|
|
{
|
|
[TestClass]
|
|
public class OptionCaclTest : YLUnitTestBase
|
|
{
|
|
[TestMethod("测试雪球期权PV")]
|
|
public void TestSnowball()
|
|
{
|
|
var td = DbContext.trade.FirstOrDefault(n => n.TradeNumber == "CW20200053E0008");
|
|
Assert.IsNotNull(td);
|
|
var calcDataProvider = new CalcDataProvider(this)
|
|
{
|
|
UnderlyingPriceProvider = new InnerUnderlyingPriceProvider(),
|
|
VolatilityDataProvider = new InnerVolatilityDataProvider()
|
|
};
|
|
var context = new OptionValueCalcContext("对冲", valuedateBLL.ValueDate, valuedateBLL.RiskFreeRate / 100, calcDataProvider)
|
|
{
|
|
AddingVolRate = 0,
|
|
IsEodSettle = false,
|
|
IsUseTradeVol = true
|
|
};
|
|
context.IsPreciseTimeMode = context.IsUseTradeVol || !context.IsEodSettle;
|
|
var result = TradeRiskCalcUtil.CalcTradeRisk(td, context, out var underlyings);
|
|
Console.WriteLine($"PV:{result.Pv}; Delta:{result.Delta}");
|
|
}
|
|
|
|
[TestMethod("测试二元期权计算")]
|
|
public void TestBinaryOption()
|
|
{
|
|
var path = Path.Combine(AppContext.BaseDirectory, "Resources\\期权计算\\二元期权.json");
|
|
var json = File.ReadAllText(path);
|
|
var td = JsonHelper.Deserialize<trade>(json);
|
|
var result = OptionCalculatorV2.GetOptionValueResult(DateTime.Today, td, new OptionValueCalcRequest( 0.025)
|
|
{
|
|
spotPrices = new[] { 6105.8249 },
|
|
vols = new[] { 0.23 }
|
|
}, out _);
|
|
Console.WriteLine(result.Delta);
|
|
}
|
|
|
|
[TestMethod("测试二元期权计算")]
|
|
public void TestBinaryOption2()
|
|
{
|
|
var path = Path.Combine(AppContext.BaseDirectory, "Resources\\期权计算\\二元期权计算参数.json");
|
|
var json = File.ReadAllText(path);
|
|
var calcParam = JsonHelper.Deserialize<OptionCalcParam<BinaryOptionTradeParam>>(json);
|
|
using (var mp = new MarketProxy(DateTime.Today, 0.025))
|
|
{
|
|
var vols = QdpVolHelper.GetDefaultVolatility(0.23);
|
|
mp.SaveVolSurface(calcParam.volSurfaceNames[0], vols);
|
|
var result = TradeRiskCalcUtil.GetBinaryOptionValue(mp, calcParam);
|
|
Console.WriteLine(result.Gamma);
|
|
}
|
|
}
|
|
|
|
class CalcDataProvider : IOptionCalcDataProvider
|
|
{
|
|
public CalcDataProvider(YLBaseService baseService)
|
|
{
|
|
UnderlyingDataProvider = new UnderlyingDataProvider();
|
|
TradeExtendDataProvider = new TradeExtendDataProvider(baseService);
|
|
}
|
|
|
|
public IPriceProvider UnderlyingPriceProvider { get; set; }
|
|
|
|
public IUnderlyingDataProvider UnderlyingDataProvider { get; }
|
|
|
|
public ITradeExtendDataProvider TradeExtendDataProvider { get; }
|
|
|
|
public IVolatilityDataProvider VolatilityDataProvider { get; set; }
|
|
}
|
|
|
|
class InnerUnderlyingPriceProvider : IPriceProvider
|
|
{
|
|
public double GetPrice(string instrumentCode)
|
|
{
|
|
return 15.45;
|
|
}
|
|
|
|
public bool TryGetPrice(string instrumentCode, out double price)
|
|
{
|
|
price = 15.45;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class InnerVolatilityDataProvider : IVolatilityDataProvider
|
|
{
|
|
public double? GetExchangeOptionTradeHedgeVol(string optionCode, DateTime valueDate)
|
|
{
|
|
return 0.25;
|
|
}
|
|
|
|
public double? GetOtcOptionTradeEodVol(int tradeId, DateTime valueDate)
|
|
{
|
|
return 0.25;
|
|
}
|
|
|
|
public double? GetOtcOptionTradeHedgeVol(int tradeId, DateTime valueDate)
|
|
{
|
|
return 0.25;
|
|
}
|
|
|
|
public IOtcTradeVolatility GetOtcOptionTradeVol(int tradeId, DateTime valueDate)
|
|
{
|
|
return new OtcTradeVolatility
|
|
{
|
|
OpenVol = 0.25,
|
|
CloseVol = 0.25,
|
|
SmoothingDays = 1,
|
|
IsFirst = false,
|
|
ValueDate = valueDate
|
|
};
|
|
}
|
|
|
|
public IVolatility GetUnderlyingVolatility(string voltype, string contractCode, string userGroup)
|
|
{
|
|
return VolatilityHelper.GetDefaultVol(new SingleVolatilityRequest
|
|
{
|
|
QuotationDate = DateTime.Today,
|
|
TradeVolWithBidAsk = false,
|
|
UnderlyingCode = contractCode,
|
|
UnderlyingId = 0,
|
|
UserGroup = userGroup,
|
|
VolType = voltype
|
|
}, 0.25);
|
|
}
|
|
}
|
|
}
|
|
}
|