optcal.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. from QuantLib import *
  3. def cal_implvol(spot, strike, callput, evaldate, exdate, rate, div, vol, premium):
  4. Settings.instance().evaluationDate = str2qdate(evaldate)
  5. exercise = EuropeanExercise(str2qdate(exdate))
  6. payoff = PlainVanillaPayoff(str2qopt_type(callput), strike)
  7. option = EuropeanOption(payoff,exercise)
  8. S = QuoteHandle(SimpleQuote(spot))
  9. r = YieldTermStructureHandle(FlatForward(0, TARGET(), rate, Actual360()))
  10. q = YieldTermStructureHandle(FlatForward(0, TARGET(), div, Actual360()))
  11. sigma = BlackVolTermStructureHandle(BlackConstantVol(0, TARGET(), vol, Actual360()))
  12. process = BlackScholesMertonProcess(S,q,r,sigma)
  13. im = 0.0
  14. im = option.impliedVolatility(premium, process)
  15. results = {}
  16. results['imvol'] = im
  17. return results
  18. def cal_option(spot, strike, callput, evaldate, exdate, rate, div, vol):
  19. Settings.instance().evaluationDate = str2qdate(evaldate)
  20. exercise = EuropeanExercise(str2qdate(exdate))
  21. payoff = PlainVanillaPayoff(str2qopt_type(callput), strike)
  22. option = EuropeanOption(payoff,exercise)
  23. S = QuoteHandle(SimpleQuote(spot))
  24. r = YieldTermStructureHandle(FlatForward(0, TARGET(), rate, Actual360()))
  25. q = YieldTermStructureHandle(FlatForward(0, TARGET(), div, Actual360()))
  26. sigma = BlackVolTermStructureHandle(BlackConstantVol(0, TARGET(), vol, Actual360()))
  27. process = BlackScholesMertonProcess(S,q,r,sigma)
  28. engine = AnalyticEuropeanEngine(process)
  29. option.setPricingEngine(engine)
  30. results = {}
  31. results['npv'] = option.NPV()
  32. results['delta'] = option.delta()
  33. results['gamma'] = option.gamma()
  34. results['theta'] = option.theta() / 360
  35. results['vega'] = option.vega()
  36. return results
  37. def str2qdate(yyyymmdd):
  38. months = [January, February, March, April, May, June, July, August, September, October,
  39. November, December]
  40. #print '%d%d%d'% (int(yyyymmdd[6:8]), int(yyyymmdd[4:6])-1 , int(yyyymmdd[0:4]))
  41. return Date(int(yyyymmdd[6:8]), months[int(yyyymmdd[4:6])-1 ], int(yyyymmdd[0:4]))
  42. def str2qopt_type(callput):
  43. if callput.upper() == 'C':
  44. return Option.Call
  45. return Option.Put
  46. if __name__ == '__main__':
  47. # todaysDate = Date(10,August, 2015)
  48. # Settings.instance().evaluationDate = todaysDate
  49. #
  50. # exercise = EuropeanExercise(Date(28,August,2015))
  51. # payoff = PlainVanillaPayoff(Option.Call, 25600.0)
  52. # option = EuropeanOption(payoff,exercise)
  53. #
  54. # v = 0.19
  55. # S = QuoteHandle(SimpleQuote(24507.0))
  56. # r = YieldTermStructureHandle(FlatForward(0, TARGET(), 0.0005, Actual360()))
  57. # q = YieldTermStructureHandle(FlatForward(0, TARGET(), 0.0005, Actual360()))
  58. # sigma = BlackVolTermStructureHandle(BlackConstantVol(0, TARGET(), v, Actual360()))
  59. # process = BlackScholesMertonProcess(S,q,r,sigma)
  60. #
  61. #
  62. # im = option.impliedVolatility(85.0, process)
  63. # print im
  64. #
  65. # engine = AnalyticEuropeanEngine(process)
  66. # option.setPricingEngine(engine)
  67. # print '%0.4f delta=%0.4f gamma=%0.4f theta=%0.4f vega=%0.4f ' % (option.NPV(), option.delta(), option.gamma(), option.theta() / 360, option.vega())
  68. # for i in range(24000, 27000, 200):
  69. # results = cal_option(24189.0, i * 1.0, 'C', '20150804', '20150828', 0.0005, 0.0005, 0.19)
  70. # results2 = cal_option(24189.0, i * 1.0, 'P', '20150804', '20150828', 0.0005, 0.0005, 0.19)
  71. # print ('%d: '% i) + ''.join ('%s=%0.4f, '%(k,v) for k, v in results.iteritems()) + '|' + ''.join ('%s=%0.4f, '%(k,v) for k, v in results2.iteritems())
  72. # for k, v in results.iteritems():
  73. # print '%s= %0.4f' % (k, v)
  74. #spot 24119.0, X 25000, right: P, evaldate: 20150812, expiry: 20150828, rate: 0.0005, div: 0.0005, vol: 0.2000, premium: 334.0000
  75. #spot 24149.0, X 25200, right: P, evaldate: 20150812, expiry: 20150828, rate: 0.0005, div: 0.0005, vol: 0.2000, premium: 437.5000
  76. results = cal_option(24149.0, 25200, 'P', '20150812', '20150828', 0.0005, 0.0005, 0.19)
  77. print ''.join ('%s=%0.4f, '%(k,v) for k, v in results.iteritems())
  78. results = cal_implvol(24149.0, 25200, 'P', '20150812', '20150828', 0.0005, 0.0005, 0.19, 1247)
  79. print ''.join ('%s=%0.4f, '%(k,v) for k, v in results.iteritems())