请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  源码分享 帖子:3365773 新帖:5

【期权研究】期权损益图绘制

吃瓜群众小王发表于:5 月 10 日 05:44回复(1)

最近开始做毕设了,方向是期权方面的策略开发,写了一份简单的【期权损益图】绘制的代码 ,贴出来供大家参考和讨论。

import pandas as pd
import numpy as np

def draw_opt(opt_df):
    gainloss_df = pd.DataFrame()
    gap = 0.0001
    (start, end) = (mean(opt_df.loc['X']) - 0.5, mean(opt_df.loc['X']) + 0.5)
    for opt_num in opt_df.columns:
        opt_info = opt_df.loc[['direction', 'catagory'], opt_num].tolist()
        price = opt_df.loc['price', opt_num]
        X = opt_df.loc['X', opt_num]
        count = opt_df.loc['count', opt_num]

        if opt_info == ['buy', 'call']:  # 买入看涨期权
            v = [(-price * count) for i in np.arange(start, X - gap * 0.1, gap)]
            for i in np.arange(X, end - gap * 0.1, gap):
                v.append((i - X - price) * count)
        elif opt_info == ['buy', 'put']:  # 买入看跌期权
            v = []
            for i in np.arange(start, X - gap * 0.1, gap):
                v.append((X - i - price) * count)
            v += [(-price * count) for i in np.arange(X, end - gap * 0.1, gap)]
        elif opt_info == ['sell', 'call']:  # 卖出看涨期权
            v = [(price * count) for i in np.arange(start, X - gap * 0.1, gap)]
            for i in np.arange(X, end - gap * 0.1, gap):
                v.append(-(i - X - price) * count)
        elif opt_info == ['sell', 'put']:  # 卖出看跌期权
            v = []
            for i in np.arange(start, X - gap * 0.1, gap):
                v.append(-(X - i - price) * count)
            v += [(price * count) for i in np.arange(X, end - gap * 0.1, gap)]

        gainloss_df[opt_num] = v

    gainloss_df.index = np.arange(start, end - gap * 0.1, gap)
    gainloss_df['sum'] = gainloss_df['opt1'] + gainloss_df['opt2']

    plt.figure(figsize=(12, 8))
    # 正常显示负号
    plt.rcParams['axes.unicode_minus'] = False
    plt.plot(gainloss_df['opt1'], '--', label='opt1')
    plt.plot(gainloss_df['opt2'], '--', label='opt2')
    plt.plot(gainloss_df['sum'], c='r')
    plt.axhline(0, c='gray')
    plt.ylim(min(gainloss_df['sum']) - 0.2, max(gainloss_df['sum']) + 0.2)
    plt.xlim(start, end)
    plt.xticks(np.arange(start, end, 0.1))
    #plt.grid(True)
    plt.legend()
    plt.show()

反向比例认购组合到期损益图¶


  • 买入两份价外认购期权,同时卖出一份价内认购期权
opt_df = pd.DataFrame(index=['direction',  # 买卖方向
                             'catagory',   # 期权类型(call、put)
                             'price',      # 期权价格
                             'X',          # 执行价
                             'count'])     # 期权份数
opt_df['opt1'] = ['buy', 'call', 0.02, 2.2, 2]
opt_df['opt2'] = ['sell', 'call', 0.16, 2, 1]
draw_opt(opt_df)

跨式期权组合到期损益图¶


  • 同时买入相同行权价格的认购、认沽期权
opt_df['opt1'] = ['buy', 'call', 0.08, 2.2, 1]
opt_df['opt2'] = ['buy', 'put', 0.04, 2.2, 1]
draw_opt(opt_df)

全部回复

0/140

量化课程

    移动端课程