Files
zszq-trs/UnitTestProject/Modules/CalcModules/HedgePnlCalcTest.cs
T
2024-05-09 14:06:26 +08:00

309 lines
12 KiB
C#

using YLErp.Abstract.DataProviders;
using YLErp.BLL.Hedge;
using YLErp.Modules.CalculationModule;
using YLErp.Modules.CalculationModule.Abstract;
using YLErp.Modules.DataProviderModule;
namespace YLErp.Modules.CalcModules
{
[TestClass]
public class HedgePnlCalcTest
{
[TestMethod]
public void TestExchangeOption()
{
var priceProvider = new ManualPriceProvider();
priceProvider.SetPrice("CU2112", 79000);
priceProvider.SetPrice("CU2112C79000", 11);
var context = new HedgePnlCalcContext(Enums.CalcScenarioEnum.RealtimeRisk, new DateTime(2021, 9, 8), "持仓", false, priceProvider, priceProvider, priceProvider, OptUserInfo.SystemUser);
var calc = new HedgePnlCalc(context);
var extdList = new List<ExchangeTrade>
{
new ExchangeTrade
{
AssetBookId = 1,
Comments = "",
Commission = 10,
CommissionType = DBModels.Enums.CommissionType.手动录入,
CreateTime = DateTime.Now,
ExchangeAccountCode = "",
ExchangeAccountId = 1,
ExerciseMode = "European",
InstrumentType = "Stock",
IsValid = true,
MaturityDate = DateTime.Today.AddMonths(1),
Notional = 1,
OptDate = DateTime.Now,
OptId = 0,
OptionCode = "CU2112C79000",
OptionStrike = 79000,
OptionType = "看涨",
OptName = "",
TradeAmount = 1,
TradeDate = DateTime.Today,
TradeLots = 1,
TradeNumber = "111",
TraderId = 1,
TraderName = "",
TradeSide = "多头买入",
TradeSinglePrice = 10,
TradeSource = "",
TradeType = "场内期权",
UnderlyingCode = "CU2112",
UnderlyingId = 3320555
}
};
var results = calc.Calculate(extdList, null);
Console.WriteLine(results.First().Vol);
}
[TestMethod("测试验证HedgePnl")]
public void TestExchangeOption2()
{
var priceProvider = new ManualPriceProvider();
priceProvider.SetPrice("RB00", 110);
var context = new HedgePnlCalcContext(Enums.CalcScenarioEnum.RealtimeRisk, new DateTime(2022, 11, 15)
, "持仓", false, priceProvider, priceProvider, priceProvider, OptUserInfo.SystemUser);
var calc = new HedgePnlCalc(context);
var underlying = DataCacheProvider.GetUnderlyingDataSource().GetData("RB00");
Assert.IsNotNull(underlying, "取不到标的信息:RB00");
var tradeDate = new DateTime(2022, 11, 15);
var tradeDateStr = tradeDate.ToString("yyyy_MM_dd_HH_mm_ss");
var etd0 = new ExchangeTrade
{
id = 1,
TradeDate = tradeDate,
TradeNumber = tradeDateStr + "_1",
TradeType = "商品期货",
TradeSide = "多头开仓",
UnderlyingCode = "RB00",
UnderlyingId = underlying.id,
Notional = 10,
TradeLots = 1,
TradeAmount = 10.0,
TradeSinglePrice = 100,
AssetBookId = 1,
TraderId = 1,
TraderName = "管理员",
InstrumentType = ConsGlobal.InstrumentType.CommodityFutures,
CreateTime = tradeDate,
IsValid = true,
TradeSource = "导入交易",
MaturityDate = new DateTime(2030, 12, 31),
OptionCode = null,
OptionStrike = null,
OptionType = null,
ExerciseMode = null,
ExchangeAccountId = 1,
ExchangeAccountCode = "11111",
Commission = 0,
CommissionType = DBModels.Enums.CommissionType.手动录入,
Comments = "",
OptId = 1,
OptName = "管理员",
OptDate = DateTime.Now
};
var etd1 = etd0.Clone();
etd1.TradeSide = "多头平仓";
var etd2 = etd0.Clone();
etd2.TradeLots = 2;
etd2.Notional = etd2.TradeAmount = 20;
var hedgePnl1 = calc.Calculate(new[] { etd1, etd2 }, null).First();
Assert.AreEqual(hedgePnl1.Notional, 10);
}
private eod_trade_position GetEodPosition(HedgePnl hedgePnl)
{
var cost = (double)hedgePnl.Cost;
var notional = (double)hedgePnl.Notional;
var positionPnL = hedgePnl.Pv - hedgePnl.Cost;
return new eod_trade_position
{
ValueDate = new DateTime(2022, 10, 28),
TradeType = hedgePnl.TradeType,
BookId = hedgePnl.BookId,
UnderlyingId = hedgePnl.UnderlyingId,
UnderlyingCode = hedgePnl.UnderlyingCode,
BuySell = hedgePnl.BuySell,
PositionType = hedgePnl.PositionType,
Cost = cost,
Amount = notional,
LastPv = hedgePnl.LastPv,
Pv = hedgePnl.Pv,
RoundedPv = hedgePnl.Pv,
DailyPnL = hedgePnl.DailyPnL,
TotalPnL = (double)hedgePnl.TotalPnl,
PositionPnL = positionPnL,
RoundedPositionPnL = positionPnL,
OptDate = DateTime.Now,
OptId = 0,
OptName = "",
HedgeUniqueCode = hedgePnl.HedgeUniqueCode,
ExchangeOptionCode = hedgePnl.ExchangeOptionCode,
Commission = hedgePnl.Commission,
Margin = 0,
TradeId = 0,
ParentTradeId = 0,
ClientId = 0,
ClosedPnL = hedgePnl.RealizedPnL
};
}
[TestMethod("20220302修改场内期权测试")]
public void TestMethod20220302()
{
var priceProvider = new ManualPriceProvider();
priceProvider.SetPrice("CU2112", 79000);
priceProvider.SetPrice("CU2112C79000", 11);
var context = new HedgePnlCalcContext(calcScenario: Enums.CalcScenarioEnum.EodSettlement
, valueDate: new DateTime(2021, 9, 8), volType: "持仓", isEodCalc: false
, underlyingPriceProvider: priceProvider, underlyingSettlePriceProvider: priceProvider
, exchangeOptionPriceProvider: priceProvider, optUser: OptUserInfo.SystemUser
, commissionCalc: new InnerExchangeTradeCommissionCalc());
var calc = new HedgePnlCalc(context);
var extdList = new List<ExchangeTrade>
{
new ExchangeTrade
{
AssetBookId = 1,
Comments = "",
Commission = 10,
CommissionType = DBModels.Enums.CommissionType.手动录入,
CreateTime = DateTime.Now,
ExchangeAccountCode = "",
ExchangeAccountId = 1,
ExerciseMode = "European",
InstrumentType = "Stock",
IsValid = true,
MaturityDate = DateTime.Today.AddMonths(1),
Notional = 1,
OptDate = DateTime.Now,
OptId = 0,
OptionCode = "CU2112C79000",
OptionStrike = 79000,
OptionType = "看涨",
OptName = "",
TradeAmount = 1,
TradeDate = DateTime.Today,
TradeLots = 1,
TradeNumber = "111",
TraderId = 1,
TraderName = "",
TradeSide = "多头买入",
TradeSinglePrice = 10,
TradeSource = "",
TradeType = "场内期权",
UnderlyingCode = "CU2112",
UnderlyingId = 3320555
}
};
var results = calc.Calculate(extdList, null);
Console.WriteLine(results.First().Vol);
}
class InnerTradeCommissionCalcResult : ITradeCommissionCalcResult
{
public double GetTradeCommission(int tradeId)
{
return 0;
}
}
class InnerExchangeTradeCommissionCalc : IExchangeTradeCommissionCalc
{
public ITradeCommissionCalcResult GetTradeCommission(IEnumerable<ExchangeTrade> tradeList, bool isActualTrade = true)
{
return new InnerTradeCommissionCalcResult();
}
}
class InnerUnderlyingDataProvider : IUnderlyingDataProvider
{
public CorrelationTable GetCorrelation(int underlyingId1, int underlyingId2)
{
return new CorrelationTable();
}
public int GetCountRatio(string underlyingCode)
{
return 1;
}
public ExchangeListOption GetExchange_List_Option(string ContractCode)
{
return new ExchangeListOption
{
ContractCode = "CU2112C7900",
ContractSize = 10,
CreateTime = DateTime.Now,
ExerciseMode = ConsGlobal.ExerciseMode.European,
MarginRate = 10,
MarketCode = "SHEF",
MaturityDate = DateTime.Today.AddMonths(6),
OpenDate = DateTime.Today,
OptionType = "看涨",
PrevClosePrice = 100,
Price = 100,
PriceTick = 0.01,
PriceTime = DateTime.Now,
Strike = 7900,
UnderlyingCode = "CU2112"
};
}
public SyntheticUnderlying GetSyntheticUnderlying(string underlyingCode)
{
return new SyntheticUnderlying { };
}
public underlying_manager GetUnderlying(int underlyingId)
{
return new underlying_manager { };
}
public underlying_manager GetUnderlying(string underlyingCode)
{
return new underlying_manager { };
}
public underlying_manager GetUnderlying(string underlyingCode, out double contractSize)
{
contractSize = 10;
return new underlying_manager { };
}
public Variety GetVariety(int varietyId)
{
return new Variety { };
}
public Variety GetVariety(string underlyingCode)
{
return new Variety { };
}
public Variety GetVariety(string underlyingCode, out double contractSize)
{
contractSize = 10;
return new Variety { };
}
public bool TryGetMaturityDate(string underlyingCode, out DateTime date)
{
date = new DateTime(2030, 1, 1);
return true;
}
}
}
}