Exemplo n.º 1
0
def get_implied_volatilities(mat_date, valid_options, date=None):
    # считает implied volatility для полученных опционов, оставляя интервалы где d2 монотонно убывает
    f = valid_options[1]
    t = option.get_t(mat_date, date)
    x = list()
    y = list()
    for put_option in reversed(valid_options[2]):
        vol = option.euro_option_put_volatility(f, 0, t, 0, put_option[0],
                                                put_option[1])
        k = log(put_option[0] / f)
        d2 = get_d2(k, t, vol)
        if len(x) > 0:
            if d2 < x[-1]:
                break
        x.append(d2)
        y.append(vol * vol)
    x.reverse()
    y.reverse()
    for call_option in valid_options[3]:
        vol = option.euro_option_call_volatility(f, 0, t, 0, call_option[0],
                                                 call_option[1])
        k = log(call_option[0] / f)
        d2 = get_d2(k, t, vol)
        if len(x) > 0:
            if d2 > x[-1]:
                break
        x.append(d2)
        y.append(vol * vol)
    x.reverse()
    y.reverse()
    return x, y,
Exemplo n.º 2
0
def plot_implied_volatility_smile(option_dict, rtsi, sub_plot=None):
    #рисует улыбку волатильности
    mult = 1
    if sub_plot == None:
        sub_plot = plt
    mat_dates = option_dict.keys()
    mat_dates.sort()
    for mat_date in mat_dates:
        t = option.get_t(mat_date)
        opt_dict = option_dict[mat_date]
        strikes = opt_dict.keys()
        strikes.sort()
        vols = list()
        res_strikes = list()
        for strike in strikes:
            opt_data = opt_dict[strike]
            price = 0
            if strike <= rtsi:
                if opt_data.put_last_price != 0:
                    price = opt_data.put_last_price
                elif opt_data.put_buy != 0 and opt_data.put_sell != 0:
                    price = (opt_data.put_buy + opt_data.put_sell) / 2
                if price != 0:
                    vol = option.euro_option_put_volatility(
                        rtsi, 0, t, 0, strike, price) * mult
                    vols.append(vol)
                    res_strikes.append(strike)
            else:
                if opt_data.call_last_price != 0:
                    price = opt_data.call_last_price
                elif opt_data.call_buy != 0 and opt_data.call_sell != 0:
                    price = (opt_data.call_buy + opt_data.call_sell) / 2
                if price != 0:
                    vol = option.euro_option_call_volatility(
                        rtsi, 0, t, 0, strike, price) * mult
                    vols.append(vol)
                    res_strikes.append(strike)
        #sub_plot.xlabel("K")
        #sub_plot.set_xlabel("K")
        #sub_plot.ylabel(r'$\sigma$')
        #sub_plot.set_ylabel(r'$\sigma$')
        sub_plot.plot(res_strikes, vols, label=mat_date.strftime("%d.%m.%Y"))
        #sub_plot.title("volatility smile")
        #sub_plot.show()
    sub_plot.legend()
Exemplo n.º 3
0
def get_valid_options(opt_dict,s,t):
    #возвращает список ликвидных опционов с признаком call или put
    opts=list()
    strikes=opt_dict.keys()
    c=1.2
    calls=0
    for strike in strikes:
        opt=opt_dict[strike]
        if opt.call_buy!=0 and opt.call_sell!=0:
            if opt.call_sell/opt.call_buy<c:
                midprice=(opt.call_buy+opt.call_sell)/2
                vol=option.euro_option_call_volatility(s,0,t,0,strike,midprice)
                opts.append((strike,midprice,True,vol,))
                calls+=1
        if opt.put_buy!=0 and opt.put_sell!=0:
            if opt.put_sell/opt.put_buy<c:
                midprice=(opt.put_buy+opt.put_sell)/2
                vol=option.euro_option_put_volatility(s,0,t,0,strike,midprice)
                opts.append((strike,midprice,False,vol,))
    print "calls: ",calls
    return opts
Exemplo n.º 4
0
def plot_implied_volatility(option_dict, rtsi):
    #рисует графики подразумеваемой волатильности для call и put
    mult = sqrt(252)
    for mat_date in option_dict.keys():
        t = option.get_t(mat_date)
        put_strikes = list()
        call_strikes = list()
        put_prices = list()
        call_prices = list()
        put_vol = list()
        call_vol = list()
        opt_dict = option_dict[mat_date]
        strikes = opt_dict.keys()
        strikes.sort()
        for strike in strikes:
            opt_data = opt_dict[strike]
            if opt_data.put_last_price != 0:
                put_prices.append(opt_data.put_last_price)
                vol = option.euro_option_put_volatility(
                    rtsi, 0, t, 0, strike, opt_data.put_last_price) * mult
                put_vol.append(vol)
                put_strikes.append(strike)
            if opt_data.call_last_price != 0:
                call_prices.append(opt_data.call_last_price)
                vol = option.euro_option_call_volatility(
                    rtsi, 0, t, 0, strike, opt_data.call_last_price) * mult
                call_vol.append(vol)
                call_strikes.append(strike)
        plt.subplot(2, 2, 1)
        plt.plot(put_strikes, put_prices)
        plt.title(u"put")
        plt.subplot(2, 2, 2)
        plt.plot(call_strikes, call_prices)
        plt.title(u"call")
        plt.subplot(2, 2, 3)
        plt.plot(put_strikes, put_vol)
        plt.subplot(2, 2, 4)
        plt.plot(call_strikes, call_vol)
        plt.show()
Exemplo n.º 5
0
dest_headers = ['day', 'strike', 'price', 'expiration', 'expiration_month', 'option_type', 'ba', 'vix', 'avg_daily_price', 'volatility']
dest = open(destPath, 'w')
dest.write(','.join(dest_headers) + '\n')

source = open('C:\\findata\\deal_rts_options_extended.csv', 'r')
for line in source:
    splitted = line.split(',')
    if splitted[0] != 'day':
         date = splitted[0]
         key = splitted[0] + '/' + splitted[1] + '/' + splitted[4] + '/' + splitted[5]
         avg_daily_price_arr = dict_strike.get(key)
         avg_daily_price = mean(avg_daily_price_arr)

         option_type = splitted[5].replace('\n', '')

         s = int(float(dict_ba.get(date).replace('\n', '')))
         tl = float(splitted[3].replace('\n',''))
         k = int(float(splitted[1].replace('\n', '')))
         o = float(splitted[2].replace('\n',''))

         if s > 0 and tl > 0 and k > 0 and o > 0:
             if option_type == 'P':
                 volatility = str(option.euro_option_put_volatility(s, 0, tl, 0, k, o))
             else:
                 volatility = str(option.euro_option_call_volatility(s, 0, tl, 0, k, o))

             line += ',' + dict_ba.get(date) + ',' + dict_vix.get(date) + ',' + str(avg_daily_price) + ',' + volatility
             dest.write(line.replace('\n', '') + '\n')

source.close()
dest.close()