public override void PriceTradelet(Tradelet tradelet, Exposure _expo, DateTime marketDate) { Trade trade = tradelet.MyTrade; if (trade.isExcercised) return; Product prod = tradelet.MyTrade.MyProduct; CurveSeries curves = prod.ReturnCurveSeries(); VolCurveSeries volCurves = prod.ReturnVolCurveSeries(); TradingCenter tc = prod.TC; double Premium, Delta, FwdDelta, Gamma, FwdGamma, Vega, Theta, Rho, vol, priceUL, iRate, hcRate; DateTime expDate, matDate; iRate = 0; //@@@ hcRate = 0; // @@@ List<DateTime> delDates, expoDates; double nrDays = (tradelet.DelEnd - tradelet.DelStart).TotalDays; double dailyDelta; if (trade.OptionTenorType == TENOR_TYPE.DAY) // @@@better switch! also we can use EACHTENOR! which provides exactely this!! { delDates = TradingCenter.TCEachDay(tradelet.DelStart, tradelet.DelEnd).ToList(); } else { delDates = new List<DateTime>(); delDates.Add(tradelet.DelStart); } foreach (DateTime day in delDates.Where(k => k > marketDate).ToList()) // no exposure on option trades, although we should never get here as expired? @@@ first introduce an "Expired!" { expDate = tc.GetGasOptExDate(trade.OptionTenorType, day, isExchange); if (expDate <= marketDate) break; // @@@ make dates eventually with hours, etc. // on marketDate the excercise will be triggered matDate = tc.GetGasMaturityDate(trade.OptionTenorType, day, isExchange); // @@@ ICE payment date? priceUL = curves.GetValue(trade.OptionTenorType, day, marketDate); vol = volCurves.GetValue(trade.OptionTenorType, day, marketDate, SURFACE_TYPE.MONEYNESS, trade.Strike / priceUL); Debugger.AddDebugLine("Dnum = " + trade.DealID + "; marketDate = " + marketDate.ToShortDateString() + "; expDate = " + expDate.ToShortDateString()); RWE_API.Final_Wrapper.GenOptWithGreeks(out Premium, out Delta, out FwdDelta, out Gamma, out FwdGamma, out Vega, out Theta, out Rho, trade.OptionType, priceUL, trade.Strike, vol, iRate, hcRate, expDate, matDate, marketDate); dailyDelta = prod.UnitSet.ToFullSize(trade.Volume, 1) * FwdDelta; //@@@ watch out for Exposure Type FWDDELTA vs DELTA!! ... fix if (trade.OptionTenorType == TENOR_TYPE.DAY) { expoDates = new List<DateTime>(); expoDates.Add(day); } else { expoDates = delDates; } SplitExpoToDaily_tofix(_expo, EXPOSURE_TYPE.DELTA, expoDates, dailyDelta); SplitExpoToDaily_tofix(_expo, EXPOSURE_TYPE.PNL, expoDates, dailyDelta * (Premium - trade.Price)); // @@@ CFDate wrong } }
public void PriceSet() { foreach (Trade trade in _trades.Values) trade.PriceTrade(this.MarketDate); this.Expo = Exposure.CumulateExposures(this.MarketDate, _trades.Values.Select(k => k._expo), Exposure.StandardUnit, Exposure.StandardCCY); // @@@ add to Backtest as settings Product prod; foreach (string name in GetProductNames()) { prod = DynamicData.GetProduct(name); this.ProductExpoures[name] = Exposure.CumulateExposures(this.MarketDate, _trades.Values.Where(k => k.ProductName == name).Select(k => k._expo), prod.UnitName, prod.CCY); } }
/// <summary> /// Creates a new ExposureItem that has accumulated every expoType in every tenorType over one MarketDate. @@@ slow? /// [Note CF is accumulated on delivery date?? @@@ check // plus how to accumulate monthly?] /// </summary> public static Exposure CumulateExposures(DateTime marketDate, IEnumerable<Exposure> items, string targetUnit, string targetCCY) { var totalExpo = new Exposure(marketDate, DynamicData.GetUnit(targetUnit), targetCCY); // @@@ all total Exposures are in therms/EUR - create a new Exposure Constructor and add this to the backtest as setting. foreach(Exposure expo in items) // exposure of different items, e.g. different trades or portfolios { foreach (int tenor in TENOR_TYPE.ToStringMapper.Keys) // @@ why loop this way / { foreach (int expoType in expo.DataContainer.Keys) { foreach (DateTime day in expo.DataContainer[tenor][expoType].Keys) totalExpo.AddExpo(expoType, tenor, day, expo.GetExpoValue(expoType, day, tenor) * 1); //Unit.ConvertVolume(expo.UnitSet, totalExpo.UnitSet)); } } } return totalExpo; }
public void PriceTrade(DateTime marketDate) { _expo = new Exposure(marketDate, DynamicData.GetUnit(this.UnitName), this.CCY); foreach (Tradelet item in this.tradelets.Values) { _instrument.PriceTradelet(item, _expo, marketDate); } }
// remember: 'virtual' can be overwritten, 'abstract' must be overwritten public virtual void PriceTradelet(Tradelet tradelet, Exposure _expo, DateTime marketDate) { if (tradelet.DelEnd < marketDate) return; Trade trade = tradelet.MyTrade; Product prod = tradelet.MyTrade.MyProduct; CurveSeries curves = prod.ReturnCurveSeries(); TradingCenter tc = prod.TC; List<DateTime> delDates, expoDates; double nrDays = (tradelet.DelEnd - tradelet.DelStart).TotalDays; double dailyDelta; delDates = TradingCenter.TCEachDay(tradelet.DelStart, tradelet.DelEnd).ToList(); double priceUL = 0; foreach (DateTime day in delDates) { if (day > marketDate) priceUL = curves.GetValue(TENOR_TYPE.DAY, day, marketDate); dailyDelta = prod.UnitSet.ToFullSize(trade.Volume, 1); _expo.AddExpo(EXPOSURE_TYPE.DELTA, TENOR_TYPE.DAY, day, dailyDelta); if(day > marketDate) _expo.AddExpo(EXPOSURE_TYPE.PNL, TENOR_TYPE.DAY, day, dailyDelta * (priceUL - trade.Price)); } }
private void SplitExpoToDaily_tofix(Exposure expo, int expoType, IEnumerable<DateTime> expoDates, double expoValue) { foreach (DateTime day in expoDates) { expo.AddExpo(EXPOSURE_TYPE.DELTA, TENOR_TYPE.DAY, day, expoValue); } }