Exemplo n.º 1
0
def iv(series,S, cd_price,option_type=None):
    if option_type is None:
        if series['期权类型'] == 'C':
            option_type = c.OptionType.CALL
        elif series['期权类型'] == 'P':
            option_type = c.OptionType.PUT
        else:
            option_type = None
    black = QlBlackFormula(eval_date, series['到期日'], option_type, S, series['行权价'], rf=rf)
    iv = black.estimate_vol(series[cd_price])
    return iv
Exemplo n.º 2
0
 def _get_pricing_engine(self, spot):
     dt_maturity = self.maturitydt()
     strike = self.applicable_strike()
     option_type = self.option_type()
     if self.exercise_type == OptionExerciseType.EUROPEAN:
         black_formula = QlBlackFormula(
             dt_eval=self.eval_date,
             dt_maturity=dt_maturity,
             option_type=option_type,
             spot=spot,
             strike=strike,
             rf=self.rf
         )
         return black_formula
     else:
         binomial = QlBinomial(
             dt_eval=self.eval_date,
             dt_maturity=dt_maturity,
             option_type=option_type,
             option_exercise_type=OptionExerciseType.AMERICAN,
             spot=spot,
             strike=strike,
             rf=self.rf,
             n=800
         )
         return binomial
Exemplo n.º 3
0
 def _set_pricing_engine(self):
     if self.pricing_engine is None:
         dt_maturity = self.maturitydt()
         strike = self.applicable_strike()
         option_type = self.option_type()
         spot = self.underlying_close()
         if self.exercise_type == OptionExerciseType.EUROPEAN:
             black_formula = QlBlackFormula(
                 dt_eval=self.eval_date,
                 dt_maturity=dt_maturity,
                 option_type=option_type,
                 spot=spot,
                 strike=strike,
                 rf=self.rf
             )
             self.pricing_engine = black_formula
         else:
             binomial = QlBinomial(
                 dt_eval=self.eval_date,
                 dt_maturity=dt_maturity,
                 option_type=option_type,
                 option_exercise_type=OptionExerciseType.AMERICAN,
                 spot=spot,
                 strike=strike,
                 rf=self.rf,
                 n=800
             )
             self.pricing_engine = binomial
Exemplo n.º 4
0
 def fun_htb_rate_adjusted_iv(self, df_series: pd.DataFrame,
                              option_type: OptionType, htb_r: float):
     ttm = df_series[Util.AMT_TTM]
     K = df_series[Util.AMT_APPLICABLE_STRIKE]
     S = df_series[Util.AMT_UNDERLYING_CLOSE] * math.exp(-htb_r * ttm)
     dt_eval = df_series[Util.DT_DATE]
     dt_maturity = df_series[Util.DT_MATURITY]
     if option_type == OptionType.CALL:
         C = df_series[Util.AMT_CALL_QUOTE]
         if self.exercise_type == OptionExerciseType.EUROPEAN:
             pricing_engine = QlBlackFormula(dt_eval, dt_maturity,
                                             OptionType.CALL, S, K, self.rf)
         else:
             # pricing_engine = QlBinomial(dt_eval, dt_maturity, OptionType.CALL, OptionExerciseType.AMERICAN, S, K,
             #                             rf=self.rf)
             pricing_engine = QlBAW(dt_eval,
                                    dt_maturity,
                                    OptionType.CALL,
                                    OptionExerciseType.AMERICAN,
                                    S,
                                    K,
                                    rf=self.rf)
         iv = pricing_engine.estimate_vol(C)
     else:
         P = df_series[Util.AMT_PUT_QUOTE]
         if self.exercise_type == OptionExerciseType.EUROPEAN:
             pricing_engine = QlBlackFormula(dt_eval, dt_maturity,
                                             OptionType.PUT, S, K, self.rf)
         else:
             # pricing_engine = QlBinomial(dt_eval, dt_maturity, OptionType.PUT, OptionExerciseType.AMERICAN, S, K,
             #                             rf=self.rf)
             pricing_engine = QlBAW(dt_eval,
                                    dt_maturity,
                                    OptionType.PUT,
                                    OptionExerciseType.AMERICAN,
                                    S,
                                    K,
                                    rf=self.rf)
         iv = pricing_engine.estimate_vol(P)
     return iv
import math
import datetime
from PricingLibrary.BlackFormular import BlackFormula
from PricingLibrary.EngineQuantlib import QlBlackFormula
from PricingLibrary.BlackCalculator import BlackCalculator
import back_test.model.constant as c

dt_eval = datetime.date(2017, 1, 1)
dt_maturity = datetime.date(2017, 4, 1)
spot_price = 120
strike_price = 100
volatility = 0.3  # the historical vols or implied vols
dividend_rate = 0
risk_free_rate = 0.03
option_price = 30.0

black_formula = BlackFormula(dt_eval, dt_maturity, c.OptionType.CALL,
                             spot_price, strike_price, option_price)
iv1 = black_formula.ImpliedVolApproximation()
estimated_price1 = BlackCalculator(dt_eval, dt_maturity, strike_price,
                                   c.OptionType.CALL, spot_price, iv1).NPV()
ql_black = QlBlackFormula(dt_eval, dt_maturity, c.OptionType.CALL, spot_price,
                          strike_price)
iv2, estimated_price2 = ql_black.estimate_vol(option_price)

print(iv1, estimated_price1)
print(iv2, estimated_price2)
Exemplo n.º 6
0
     binomial_tree = QlBinomial(base_option_call.eval_date,
                                base_option_call.maturitydt(),
                                base_option_call.option_type(),
                                exercise_type,
                                spot,
                                base_option_call.strike(),
                                vol=init_vol,
                                rf=rf,
                                n=1000)
     iv_call = binomial_tree.estimate_vol(base_option_call.mktprice_close())
 else:
     cd_source = 'quantlib'
     black_formula = QlBlackFormula(
         dt_eval=base_option_call.eval_date,
         dt_maturity=base_option_call.maturitydt(),
         option_type=base_option_call.option_type(),
         spot=spot,
         strike=base_option_call.strike(),
         rf=rf)
     try:
         iv_call = black_formula.estimate_vol(
             base_option_call.mktprice_close())
     except:
         iv_call = 0.0
 print(base_option_call.id_instrument(), base_option_call.eval_date,
       iv_call)
 res = {
     'dt_date': base_option_call.eval_date,
     'name_code': name_code,
     'id_underlying': base_option_call.id_underlying(),
     'cd_option_type': 'call',
Exemplo n.º 7
0

strike = 1000.0  # 行权价
option_exercise_type = c.OptionExerciseType.EUROPEAN  # 行权方式
option_type = c.OptionType.CALL  # 期权类型
buy_write = c.BuyWrite.BUY  # 持仓头寸
vol = 0.2  # 波动率
rf = 0.03  # 利率
dt_eval = datetime.date(2018, 1, 6)  # 估值日
dt_maturity = dt_eval + datetime.timedelta(days=50)

spot = 1000.0  # 标的价格
black_formula = QlBlackFormula(dt_eval=dt_eval,
                               dt_maturity=dt_maturity,
                               option_type=option_type,
                               spot=spot,
                               strike=strike,
                               vol=vol,
                               rf=rf)
delta = black_formula.Delta(vol)
last_delta = delta
last_spot = spot
res = []
print('ww model,spot从1000到2000调仓信息')
print('-' * 100)
print("%20s %20s %20s" % ('spot', 'dS', 'gamma'))
print('-' * 100)
for spot in np.arange(1000.0, 2000.0, 0.5):
    black_formula = QlBlackFormula(dt_eval=dt_eval,
                                   dt_maturity=dt_maturity,
                                   option_type=option_type,