Bond Yield Script

import QuantLib as ql ''' The clean_price parameter in the bond.bondYield() method represents the bond's quoted price (clean price). It is the purchase price of the bond excluding any accrued interest.How It Works in QuantLibQuoted as Percentage of Par: In QuantLib, bond prices (clean or dirty) are evaluated and expected as a percentage of their face value (base 100). For example, a clean price of 98.50 means 98.5% of the par value If a target_price is input, the yield represents the yield required to get that price for the bond ''' def yield_to_maturity(curve, bond, compounding, target_price=None): curve_handle = ql.YieldTermStructureHandle(curve) bond_engine = ql.DiscountingBondEngine(curve_handle) bond.setPricingEngine(bond_engine) if target_price!=None: return bond.bondYield(ql.BondPrice(target_price, ql.BondPrice.Clean),bond.dayCounter(), compounding ,bond.frequency()) bond_yield = bond.bondYield(bond.dayCounter(), compounding ,bond.frequency()) return bond_yield