python障碍式期权定价公式

yipeiwu_com5年前Python基础

早期写的python障碍式期权的定价脚本,供大家参考,具体内容如下

#coding:utf-8
'''
障碍期权
q=x/s
H = h/x H 障碍价格
[1] Down-and-in call cdi
[2] Up-and-in call cui
[3] Down-and-in put pdi
[4] Up-and-in put pui
[5] Down-and-out call cdo
[6] Up-and-out call cuo
[7] Down-and-out put pdo
[8] Up-and-out put puo

'''
from math import log,sqrt,exp,ceil
from scipy import stats
import datetime
import tushare as ts
import pandas as pd
import numpy as np
import random
import time as timess
import os

def get_codes(path='D:\\code\\20180313.xlsx'):     #从代码表格从获取代码
 codes = pd.read_excel(path)
 codes = codes.iloc[:,1]    
 return codes

def get_datas(code,N=1,path='D:\\data\\'):        #获取数据N=1当天数据
 datas = pd.read_csv(path+eval(code)+'.csv',encoding='gbk',skiprows=2,header=None,skipfooter=N,engine='python').dropna() #读取CSV文件 名称为股票代码 解gbk skiprows跳过前两行文字 第一行不做为表头
 date_c = datas.iloc[:,[0,4,5]]     #只用第0 列代码数据和第4列收盘价数据
 date_c.index = datas[0]
 return date_c

def get_sigma(close,std_th):
 x_i = np.log(close/close.shift(1)).dropna()
 sigma = x_i.rolling(window=std_th).std().dropna()*sqrt(244)
 return sigma

def get_mu(sigma,r):
 mu = (r-pow(sigma,2)/2)/pow(sigma,2)
 return mu

def get_lambda(mu,r,sigma):
 lam = sqrt(mu*mu+2*r/pow(sigma,2))
 return lam

def x_y(sigma,T,mu,H,lam,q=1):
 x1 = log(1/q)/(sigma*sqrt(T))+(1+mu)*sigma*sqrt(T)
 x2 = log(1/(q*H))/(sigma*sqrt(T))+(1+mu)*sigma*sqrt(T)
 y1 = log(H*H/q)/(sigma*sqrt(T))+(1+mu)*sigma*sqrt(T)
 y2 = log(q*H)/(sigma*sqrt(T))+(1+mu)*sigma*sqrt(T)
 z = log(q*H)/(sigma*sqrt(T))+lam*sigma*sqrt(T)
 return x1,x2,y1,y2,z

def get_standardBarrier(eta,phi,mu,sigma,r,T,H,lam,x1,x2,y1,y2,z,q=1):
 f1 = phi*1*stats.norm.cdf(phi*x1,0.0,1.0)-phi*q*exp(-r*T)*stats.norm.cdf(phi*x1-phi*sigma*sqrt(T),0.0,1.0)
 f2 = phi*1*stats.norm.cdf(phi*x2,0.0,1.0)-phi*q*exp(-r*T)*stats.norm.cdf(phi*x2-phi*sigma*sqrt(T),0.0,1.0)
 f3 = phi*1*pow(H*q,2*(mu+1))*stats.norm.cdf(eta*y1,0.0,1.0)-phi*q*exp(-r*T)*pow(H*q,2*mu)*stats.norm.cdf(eta*y1-eta*sigma*sqrt(T),0.0,1.0)
 f4 = phi*1*pow(H*q,2*(mu+1))*stats.norm.cdf(eta*y2,0.0,1.0)-phi*q*exp(-r*T)*pow(H*q,2*mu)*stats.norm.cdf(eta*y2-eta*sigma*sqrt(T),0.0,1.0)
 f5 = (H-1)*exp(-r*T)*(stats.norm.cdf(eta*x2-eta*sigma*sqrt(T),0.0,1.0)-pow(H*q,2*mu)*stats.norm.cdf(eta*y2-eta*sigma*sqrt(T),0.0,1.0))
 f6 = (H-1)*(pow(H*q,(mu+lam))*stats.norm.cdf(eta*z,0.0,1.0)+pow(H*q,(mu-lam))*stats.norm.cdf(eta*z-2*eta*lam*sigma*sqrt(T),0.0,1.0))
 return f1,f2,f3,f4,f5,f6

def main(param,t,r=0.065):
 typeflag = ['cdi','cdo','cui','cuo','pdi','pdo','pui','puo']
 r = log(1+r)
 T = t/365
 codes = get_codes()
 H = 1.2
 for i in range(len(codes)):
 sdbs = []
 for j in typeflag:
 code = codes.iloc[i]
 datas = get_datas(code)
 close = datas[4]
 sigma = get_sigma(close,40)[-1]
 mu = get_mu(sigma,r)
 lam = get_lambda(mu,r,sigma)
 x1,x2,y1,y2,z = x_y(sigma,T,mu,H,lam)
 eta = param[j]['eta']
 phi = param[j]['phi']
 f1,f2,f3,f4,f5,f6 = get_standardBarrier(eta,phi,mu,sigma,r,T,H,lam,x1,x2,y1,y2,z)
 if j=='cdi':
 sdb = f1-f2+f4+f5
 if j=='cui':
 sdb = f2-f3+f4+f5
 if j=='pdi':
 sdb = f1+f5
 if j=='pui':
 sdb = f3+f5
 if j=='cdo':
 sdb = f2+f6-f4
 if j=='cuo':
 sdb = f1-f2+f3-f4+f6
 if j=='pdo':
 sdb = f6
 if j=='puo':
 sdb = f1-f3+f6
 sdbs.append(sdb)
 print(T,r,sigma,H,sdbs)
if __name__ == '__main__':
 param = {'cdi':{'eta':1,'phi':1},'cdo':{'eta':1,'phi':1},'cui':{'eta':-1,'phi':1},'cuo':{'eta':-1,'phi':1},
 'pdi':{'eta':1,'phi':-1},'pdo':{'eta':1,'phi':-1},'pui':{'eta':-1,'phi':-1},'puo':{'eta':-1,'phi':-1}}
 t = 30
 main(param,t)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pycharm debug功能实现跳到循环末尾的方法

pycharm debug功能实现跳到循环末尾的方法

可以使用条件断点,如图,在断点上右键可以设置,条件自己输入,python语法: 以上这篇pycharm debug功能实现跳到循环末尾的方法就是小编分享给大家的全部内容了,希望能给大家...

Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法

Python 出现错误TypeError: ‘NoneType' object is not iterable解决办法 TypeError: 'NoneType' object is n...

对python中的装包与解包实例详解

*args和 **kwargs是常用的两个参数 *args:用于接受多余的未命名的参数,元组类型。 **kwargs:用于接受形参的命名参数,字典类型的数据。 可变参数args: d...

对numpy Array [: ,] 的取值方法详解

NumPy数组是一个多维数组对象,称为ndarray 创建一个numpy数组,如下所示 import numpy as np x=np.array([[1,2,3],[4,5,6],...

使用Python进行二进制文件读写的简单方法(推荐)

总的感觉,python本身并没有对二进制进行支持,不过提供了一个模块来弥补,就是struct模块。 python没有二进制类型,但可以存储二进制类型的数据,就是用string字符串类型来...