// Update serialized dictionary string ICalculator.UpdateSerializedDictionary(string FileFullPath) { try { BondDictionary SD = new BondDictionary(@FileFullPath); SD.DeSerialize(); foreach (KeyValuePair <string, BaseBond> k in BondDictionarySerial) { string idCode = k.Key; BaseBond bond = k.Value; if (SD.dic.ContainsKey(idCode) == true) // check if idCode is in dictionary { SD.dic[idCode] = bond; // if true, updates it } else { SD.dic.Add(idCode, bond); // if false, adds it } } SD.Serialize(); // serialize it BondDictionarySerial.Clear(); return("Dictionary Serialized @ " + DateTime.Now.ToString()); // return time of last load } catch (Exception e) { return((string)e.ToString()); } }
// given a clean price and idCode will return the yield of the corresponding bond in BondDictionaryData object ICalculator.YieldFromDictionary(string idCode, double Today, double CleanPrice, int Freq, string DayCount, string Compounding) { // int Freq, string DayCount, string Compounding refers to yield calculation // string idCode is the identification code of bond in BondDictionaryData // make it volatile if (m_xlApp != null) { m_xlApp.Volatile(true); } try { BaseBond bond = null; // initialise a bond if (BondDictionaryData.TryGetValue(idCode, out bond)) // is the idCode in dictionary? { // update today since I the bond in dictionary can be the same but I can change my reference date to do simulations bond.SetNewToDay(new Date(Today)); // Parse enum Type Dc dc = (Dc)Enum.Parse(typeof(Dc), DayCount); Compounding comp = (Compounding)Enum.Parse(typeof(Compounding), Compounding); // return the yield given parameters return(bond.Yield(CleanPrice, Freq, dc, comp)); } else { return("IdCode not found"); // bond not found } } catch (Exception e) { return((string)e.ToString()); } }
// given a clean price and idCode will return the yield of the corresponding bond in BondDictionaryData object ICalculator.CleanFromYieldFromFile(string idCode, double Today, double Yield, int Freq, string DayCount, string Compounding, string FileFullName) { // int Freq, string DayCount, string Compounding refers to yield calculation // string idCode is the identification code of bond in BondDictionaryData // make it volatile if (m_xlApp != null) { m_xlApp.Volatile(true); } try { BaseBond bond = null; // initialise a bond // deserialize BondDictionary SD = new BondDictionary(@FileFullName); SD.DeSerialize(); if (SD.dic.TryGetValue(idCode, out bond)) // is the idCode in dictionary? { // update today since I the bond in dictionary can be the same but I can change my reference date to do simulations bond.SetNewToDay(new Date(Today)); // Parse enum Type Dc dc = (Dc)Enum.Parse(typeof(Dc), DayCount); Compounding comp = (Compounding)Enum.Parse(typeof(Compounding), Compounding); // if yield is zero return a message if (Yield == 0) { return((string)"YldZero"); } // return clean price from yield given parameters return(bond.CleanPriceFromYield(Yield, Freq, dc, comp)); } else { return("IdCode not found"); // bond not found } } catch (Exception e) { return((string)e.ToString()); } }
// Add fixed coupon bond to the dictionary string ICalculator.LoadBondFixedCouponSerialLoadBondFixedCoupon(string idCode, double Today, double StartDateBond, double EndDateBond, double Coupon, string BondFixedCouponType, string FileFullPath) { // for details of inputs refer to comments on BondDBR,BondBTAN,BondBTP classes // as example, I decide to use only 3 type of bond DBR,BTAN,BTP (arbitrary strings) BaseBond bond = null; if (BondFixedCouponType == "DBR") // BondDBR { bond = new BondDBR(new Date(Today), new Date(StartDateBond), new Date(EndDateBond), Coupon); } else if (BondFixedCouponType == "BTAN") // BondBTAN { bond = new BondBTAN(new Date(Today), new Date(StartDateBond), new Date(EndDateBond), Coupon); } else if (BondFixedCouponType == "BTP") // BondBTP { bond = new BondBTP(new Date(Today), new Date(StartDateBond), new Date(EndDateBond), Coupon); } else if (BondFixedCouponType == "BOT") // BondBOT { bond = new BondBOT(new Date(Today), new Date(StartDateBond), new Date(EndDateBond)); } else { return("Bond not recognized"); // if not managed } try { if (BondDictionarySerial.ContainsKey(idCode) == true) // check if idCode is in dictionary { BondDictionarySerial[idCode] = bond; // if true, updates it } else { BondDictionarySerial.Add(idCode, bond); // if false, adds it } return("Loaded @ " + DateTime.Now.ToString()); // return time of last load } catch (Exception e) { return((string)e.ToString()); } }