期權定價
quantlib python 中的 CDS 期權定價
我是 Python 的新手,我正在嘗試為 quantlib Python 中的 CDS 選項定價。我有以下程式碼:
expiry= ql.Date(15,ql.May,2012) cds_vol=0.5 exercise = ql.EuropeanExercise(expiry) cds_option=ql.CdsOption(cds, exercise, True) risk_free_rate = ql.YieldTermStructureHandle(ql.FlatForward(todaysDate, 0.01, ql.Actual365Fixed())) probability = ql.DefaultProbabilityTermStructureHandle(hazard_curve) cds = ql.CreditDefaultSwap(ql.Protection.Seller, nominal, s, schedule, ql.Following, ql.Actual365Fixed()) engine = ql.MidPointCdsEngine(probability, recovery_rate, risk_free_rate) cds.setPricingEngine(engine) cds_option_price=cds_option.setPricingEngine((ql.BlackCdsOptionEngine(probability, recovery_rate, risk_free_rate,cds_vol))) cds_option_price.NPV()
這給了我一個錯誤:TypeError: in method ’new_BlackCdsOptionEngine’, argument 4 of type ‘Handle< Quote > const &’
首先,錯誤是因為您應該輸入 cds_vol 作為報價。
所以而不是
cds_col
使用ql.QuoteHandle(ql.SimpleQuote(cds_vol))
除此之外,該
.setPricingEngine()
方法將直接影響 cds_option 對象,因此您應該將其用作:cds_option.setPricingEngine((ql.BlackCdsOptionEngine(probability, recovery_rate, risk_free_rate, ql.QuoteHandle(ql.SimpleQuote(cds_vol))))) cds_option.NPV()