从山证v2.3.0拷贝
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using YLErp.DBModels;
|
||||
using YLErp.Modules.DataProviderModule;
|
||||
|
||||
namespace YLErp.Modules.VolatilityModule
|
||||
{
|
||||
[TestClass]
|
||||
public class VarietyVolServiceTest : YLUnitTestBase
|
||||
{
|
||||
[ClassInitialize]
|
||||
public static void Init(TestContext context)
|
||||
{
|
||||
using (var db = DbContextFactory.GetYLDbContext())
|
||||
{
|
||||
db.OptUser = new OptUserInfo(0, "UnitTest");
|
||||
|
||||
db.Database.ExecuteSqlCommand("truncate table variety_vol;");
|
||||
|
||||
var datas = new[] {
|
||||
new VarietyVol{ VarietyId = 1,ValueDate = new DateTime(2020,9,1),Vol = 0.301 },
|
||||
new VarietyVol{ VarietyId = 1,ValueDate = new DateTime(2020,9,3),Vol = 0.311 },
|
||||
new VarietyVol{ VarietyId = 1,ValueDate = new DateTime(2020,9,13),Vol = 0.331 },
|
||||
|
||||
new VarietyVol{ VarietyId = 2,ValueDate = new DateTime(2020,9,21),Vol = 0.401 },
|
||||
new VarietyVol{ VarietyId = 2,ValueDate = new DateTime(2020,9,22),Vol = 0.501 },
|
||||
new VarietyVol{ VarietyId = 2,ValueDate = new DateTime(2020,10,10),Vol = 0.601 },
|
||||
};
|
||||
|
||||
foreach (var data in datas)
|
||||
{
|
||||
data.OptId = 0;
|
||||
data.OptName = "UnitTest";
|
||||
data.OptDate = DateTime.Now;
|
||||
}
|
||||
|
||||
db.variety_vol.AddRange(datas);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetVols()
|
||||
{
|
||||
var dates = new[] { new DateTime(2020, 9, 10), new DateTime(2020, 9, 22) };
|
||||
var check = new[] {
|
||||
new Action<VarietyVolDto[]>(n=>{
|
||||
Assert.IsTrue(n.Length == 1);
|
||||
Assert.IsTrue(n[0].Vol == 0.311);
|
||||
Assert.IsTrue(n[0].ValueDate == new DateTime(2020, 9, 3));
|
||||
}),
|
||||
new Action<VarietyVolDto[]>(n=>{
|
||||
Assert.IsTrue(n.Length == 2);
|
||||
Assert.IsTrue(n[0].Vol == 0.331);
|
||||
Assert.IsTrue(n[0].ValueDate == new DateTime(2020, 9, 13));
|
||||
|
||||
Assert.IsTrue(n[1].Vol == 0.501);
|
||||
Assert.IsTrue(n[1].ValueDate == new DateTime(2020, 9, 22));
|
||||
}),
|
||||
};
|
||||
|
||||
for (var i = 0; i < dates.Length; i++)
|
||||
{
|
||||
var date = dates[i];
|
||||
|
||||
var grpQry = from vol in DbContext.variety_vol
|
||||
where vol.ValueDate <= date
|
||||
group vol by vol.VarietyId into grp
|
||||
select new
|
||||
{
|
||||
VarietyId = grp.Key,
|
||||
ValueDate = grp.Max(n => n.ValueDate)
|
||||
};
|
||||
|
||||
var qry = from gv in grpQry
|
||||
join vol in DbContext.variety_vol on gv equals new { vol.VarietyId, vol.ValueDate }
|
||||
orderby gv.VarietyId
|
||||
select new VarietyVolDto
|
||||
{
|
||||
VarietyId = vol.VarietyId,
|
||||
ValueDate = vol.ValueDate,
|
||||
Vol = vol.Vol
|
||||
};
|
||||
|
||||
var qryDatas = qry.ToArray();
|
||||
|
||||
check[i](qryDatas);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetVols2()
|
||||
{
|
||||
var dates = new[] { new DateTime(2020, 9, 10), new DateTime(2020, 9, 22) };
|
||||
var check = new[] {
|
||||
new Action<VarietyVolDto[]>(n=>{
|
||||
Assert.IsTrue(n.Length == 3);
|
||||
Assert.IsTrue(n[0].Vol == 0.311);
|
||||
Assert.IsTrue(n[0].ValueDate == new DateTime(2020, 9, 3));
|
||||
|
||||
Assert.IsTrue(n[1].Vol == 0.3);
|
||||
Assert.IsTrue(n[1].ValueDate == new DateTime(2020, 9, 10));
|
||||
|
||||
Assert.IsTrue(n[2].Vol == 0.3);
|
||||
Assert.IsTrue(n[2].ValueDate == new DateTime(2020, 9, 10));
|
||||
}),
|
||||
new Action<VarietyVolDto[]>(n=>{
|
||||
Assert.IsTrue(n.Length == 3);
|
||||
Assert.IsTrue(n[0].Vol == 0.331);
|
||||
Assert.IsTrue(n[0].ValueDate == new DateTime(2020, 9, 13));
|
||||
|
||||
Assert.IsTrue(n[1].Vol == 0.501);
|
||||
Assert.IsTrue(n[1].ValueDate == new DateTime(2020, 9, 22));
|
||||
|
||||
Assert.IsTrue(n[2].Vol == 0.3);
|
||||
Assert.IsTrue(n[2].ValueDate == new DateTime(2020, 9, 22));
|
||||
}),
|
||||
};
|
||||
|
||||
for (var i = 0; i < dates.Length; i++)
|
||||
{
|
||||
var date = dates[i];
|
||||
|
||||
var qryDatas = new VarietyVolService(this).GetVols(date, new[] { 1, 2, 3 });
|
||||
|
||||
check[i](qryDatas.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestGetVols3()
|
||||
{
|
||||
var provider = new VarietyVolProvider(new DateTime(2020, 9, 22));
|
||||
|
||||
Assert.IsTrue(provider.TryGetVol(1, out var vol1)&&vol1 == 0.331);
|
||||
|
||||
Assert.IsTrue(provider.TryGetVol(5, out var vol11)&&vol11 == 0.3);
|
||||
|
||||
Assert.IsTrue(provider.TryGetVol("RB00", out var vol2) && vol2 == 0.501);
|
||||
|
||||
Assert.IsTrue(provider.TryGetVol("ceshi1", out var vol3) && vol3 == 0.3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using YLErp.DBModels;
|
||||
using YLErp.Models;
|
||||
using YLErp.QdpModule.Constants;
|
||||
|
||||
namespace YLErp.Modules.VolatilityModule
|
||||
{
|
||||
[TestClass]
|
||||
public class VolatilityBasicTest
|
||||
{
|
||||
[TestMethod("测试波动率数据序列化")]
|
||||
public void TestSerialize()
|
||||
{
|
||||
var date = new DateTime(2020, 4, 23);
|
||||
|
||||
var vola = VolatilityBuilder.CreateMoneynessVolBuilder(date)
|
||||
.SetUnderlying(111, "RB2005").SetData(0.25533333).Build();
|
||||
|
||||
Assert.AreEqual(vola.QuotationDate, date);
|
||||
Assert.AreEqual(vola.VolType, ConsVolInfos.defVolType);
|
||||
Assert.AreEqual(vola.VolSurfaceMode, ConsVolInfos.defVolMode);
|
||||
Assert.AreEqual(vola.UnderlyingId, 111);
|
||||
Assert.AreEqual(vola.ContractCode, "RB2005");
|
||||
|
||||
var list = JsonHelper.Deserialize<List<SingleVol>>(vola.Data);
|
||||
|
||||
Assert.AreEqual(0.255333, list[0].Vol);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using YLErp.DBModels;
|
||||
|
||||
namespace YLErp.Modules.VolatilityModule
|
||||
{
|
||||
[TestClass]
|
||||
public class VolatilityQueryServiceTest : YLUnitTestBase
|
||||
{
|
||||
readonly VolatilityQueryService service;
|
||||
|
||||
public VolatilityQueryServiceTest()
|
||||
{
|
||||
service = new VolatilityQueryService(new OptUserInfo(0, "UnitTest"));
|
||||
}
|
||||
|
||||
[TestMethod("获取单个标的的曲面波动率")]
|
||||
public void TestGetVolatility()
|
||||
{
|
||||
var date = new DateTime(2020, 4, 20);
|
||||
|
||||
//标的不存在的情况下获取不到波动率
|
||||
|
||||
var vols = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = date,
|
||||
TradeVolWithBidAsk = true,
|
||||
UnderlyingId = 1,
|
||||
UnderlyingCode = "TA006",
|
||||
UserGroup = "",
|
||||
VolType = "交易"
|
||||
});
|
||||
|
||||
Assert.AreEqual(vols.Count(), 0);
|
||||
|
||||
//同源标的不存在波动率的情况下获取默认波动率
|
||||
|
||||
var vols2 = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = date,
|
||||
TradeVolWithBidAsk = true,
|
||||
UnderlyingCode = "AP005",
|
||||
UserGroup = "",
|
||||
VolType = "交易"
|
||||
});
|
||||
|
||||
Assert.AreEqual(vols2.Count(), 3);
|
||||
Assert.AreEqual(vols2.First().VolTable[0].Vol, 0.3);
|
||||
Assert.AreEqual(vols2.First().QuotationDate, date);
|
||||
|
||||
var vols3 = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = date,
|
||||
TradeVolWithBidAsk = true,
|
||||
UnderlyingId = 14,
|
||||
//UnderlyingCode = "AP005",
|
||||
UserGroup = "",
|
||||
VolType = "交易"
|
||||
});
|
||||
|
||||
Assert.AreEqual(vols3.Count(), 3);
|
||||
Assert.AreEqual(vols3.First().VolTable[0].Vol, 0.3);
|
||||
Assert.AreEqual(vols3.First().QuotationDate, date);
|
||||
|
||||
//标的已过期的情况下返回波动率为0的默认波动率
|
||||
|
||||
var vols4 = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = date,
|
||||
TradeVolWithBidAsk = true,
|
||||
UnderlyingCode = "RB2003",
|
||||
UserGroup = "",
|
||||
VolType = "交易"
|
||||
});
|
||||
|
||||
Assert.AreEqual(vols4.Count(), 3);
|
||||
Assert.AreEqual(vols4.First().VolTable[0].Vol, 0);
|
||||
Assert.AreEqual(vols4.First().QuotationDate, date);
|
||||
}
|
||||
|
||||
[TestMethod("获取单个标的的曲面波动率2")]
|
||||
public void TestGetVolatility2()
|
||||
{
|
||||
var date = DateTime.Today;
|
||||
|
||||
var vols = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = date,
|
||||
TradeVolWithBidAsk = true,
|
||||
UnderlyingCode = "AL00",
|
||||
VolType = "交易"
|
||||
});
|
||||
|
||||
Assert.AreEqual(vols.Count(), 0);
|
||||
}
|
||||
|
||||
[TestMethod("获取批量标的的曲面波动率")]
|
||||
public void TestGetVolatilities()
|
||||
{
|
||||
var vols = service.GetVolatilities(new BatchVolatilityRequest
|
||||
{
|
||||
QuotationDate = DateTime.Today,
|
||||
TradeVolWithBidAsk = true,
|
||||
UserGroup = string.Empty,
|
||||
VolType = "交易"
|
||||
}, true);
|
||||
}
|
||||
|
||||
[TestMethod("验证波动率复制")]
|
||||
public void TestMissingVolatilities()
|
||||
{
|
||||
var un = GetUnderlyingManager();
|
||||
|
||||
AddClearSQL<volatility>($"{nameof(volatility.ContractCode)}='{un.UnderlyingCode}'");
|
||||
|
||||
var vols = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = DateTime.Today,
|
||||
TradeVolWithBidAsk = false,
|
||||
UserGroup = string.Empty,
|
||||
VolType = "交易",
|
||||
UnderlyingCode = un.UnderlyingCode,
|
||||
UnderlyingId = un.id
|
||||
}, true);
|
||||
|
||||
Assert.IsTrue(vols.Count() == 1 && vols.First().VolTable[0].Vol == 0.3);
|
||||
|
||||
var vols2 = service.GetVolatility(new SingleVolatilityRequest
|
||||
{
|
||||
QuotationDate = DateTime.Today,
|
||||
TradeVolWithBidAsk = true,
|
||||
UserGroup = string.Empty,
|
||||
VolType = "交易",
|
||||
UnderlyingCode = un.UnderlyingCode,
|
||||
UnderlyingId = un.id
|
||||
}, true);
|
||||
|
||||
Assert.IsTrue(vols2.Count() == 3 && vols2.First().VolTable[0].Vol == 0.3);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user