using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using YLErp.BLL.Calculation; namespace YLErp.Modules.CalculationModule { /// /// /// [TestClass] public class ForwardradeCalcServiceTest { [TestMethod] public void TestCalcValueOld() { var testItems = new[] { new{ BuySell = "买入", CallPut = "Call", Strike = 3500,Notional = 100,SpotPrice= 3550 }, new{ BuySell = "买入", CallPut = "Call", Strike = 3500, Notional = 100, SpotPrice= 3450 }, new{ BuySell = "买入", CallPut = "Put", Strike = 3500, Notional = 100, SpotPrice= 3550 }, new{ BuySell = "买入", CallPut = "Put", Strike = 3500, Notional = 100, SpotPrice= 3450 }, new{ BuySell = "卖出", CallPut = "Call", Strike = 3500,Notional = 100,SpotPrice= 3550 }, new{ BuySell = "卖出", CallPut = "Call", Strike = 3500, Notional = 100, SpotPrice= 3450 }, new{ BuySell = "卖出", CallPut = "Put", Strike = 3500, Notional = 100, SpotPrice= 3550 }, new{ BuySell = "卖出", CallPut = "Put", Strike = 3500, Notional = 100, SpotPrice= 3450 }, }; foreach (var item in testItems) { var r1 = CalcValueV1(item.Strike, item.SpotPrice, item.Notional, item.CallPut, item.BuySell); var r2 = CalcValueV2(item.Strike, item.SpotPrice, item.Notional, item.CallPut, item.BuySell); Console.WriteLine(item.ToJson()); Console.WriteLine($"pv1: {r1.Pv}, pv2: {r2.Pv}, delta1: {r1.Delta}, delta2: {r2.Delta}"); Assert.IsTrue(r1.Pv == r2.Pv && r1.Delta == r2.Delta); } } /// /// 计算远期交易(买方角度)ValueCalculator.CalculateForward /// static TradeValueResult CalculateForwardV1(double strike, double spotPrice, double notional, string callPut) { var pv = 0.0; if (callPut == "Call") { pv = (spotPrice - strike) * notional; } else if (callPut == "Put") { pv = (strike - spotPrice) * notional; } return new TradeValueResult() { Pv = pv, Delta = callPut == "Call" ? notional : -notional, DeltaCash = callPut == "Call" ? spotPrice * notional : -spotPrice * notional }; } public static TradeValueResult CalcValueV1(double strike, double spotPrice, double notional, string callput, string buysell) { var result = CalculateForwardV1(strike, spotPrice, notional, callput); result.Pv *= TradeCalcHelper.GetSign(buysell); result.Delta *= TradeCalcHelper.GetSign(buysell); //买入看跌和卖出看涨取反 //var flag = (IsBuy(buysell) ? 1 : 2) | (callput == "Call" ? 1 : 2); //if (flag == 3) //{ // result.Delta = -result.Delta; //} return result; } /// /// 计算PV/Risk(交易员角度) /// public static TradeValueResult CalcValueV2(double strike, double spotPrice, double notional, string callput, string buysell) { var isCall = callput == "Call"; var pv = (spotPrice - strike) * notional; //买入看跌和卖出看涨取负值 var flag = (TradeCalcHelper.IsBuy(buysell) ? 1 : 2) | (isCall ? 1 : 2); TradeValueResult result; if (flag == 3) { result = new TradeValueResult { Pv = -pv, Delta = -notional, DeltaCash = -spotPrice * notional }; } else { result = new TradeValueResult { Pv = pv, Delta = notional, DeltaCash = spotPrice * notional }; } result.RoundedPv = result.Pv; return result; } static bool IsBuy(string tradeType) { return tradeType == "Buy" || tradeType == "买入" || string.IsNullOrWhiteSpace(tradeType); } } }