python障礙式期權(quán)定價公式
更新時間:2019年07月19日 09:44:11 作者:王大錘95
這篇文章主要為大家詳細(xì)介紹了python障礙式期權(quán)定價公式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
早期寫的python障礙式期權(quán)的定價腳本,供大家參考,具體內(nèi)容如下
#coding:utf-8
'''
障礙期權(quán)
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\\'): #獲取數(shù)據(jù)N=1當(dāng)天數(shù)據(jù)
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 列代碼數(shù)據(jù)和第4列收盤價數(shù)據(jù)
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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python 簡易計算器程序,代碼就幾行
- Python實(shí)現(xiàn)簡單的四則運(yùn)算計算器
- 僅用50行代碼實(shí)現(xiàn)一個Python編寫的計算器的教程
- 基于python的Tkinter實(shí)現(xiàn)一個簡易計算器
- Python只用40行代碼編寫的計算器實(shí)例
- 利用Tkinter(python3.6)實(shí)現(xiàn)一個簡單計算器
- Python PyQt5實(shí)現(xiàn)的簡易計算器功能示例
- python正則表達(dá)式之作業(yè)計算器
- Python開發(fā)的實(shí)用計算器完整實(shí)例
- 基于wxpython開發(fā)的簡單gui計算器實(shí)例
相關(guān)文章
使用Python對微信好友進(jìn)行數(shù)據(jù)分析
這篇文章主要介紹了使用Python對微信好友進(jìn)行數(shù)據(jù)分析的實(shí)現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
Django搭建MySQL主從實(shí)現(xiàn)讀寫分離
本文主要介紹了Django搭建MySQL主從實(shí)現(xiàn)讀寫分離,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
從零學(xué)python系列之從文件讀取和保存數(shù)據(jù)
在Python一般都是運(yùn)用內(nèi)置函數(shù)open()與文件進(jìn)行交互,下面說說具體用法2014-05-05
python利用proxybroker構(gòu)建爬蟲免費(fèi)IP代理池的實(shí)現(xiàn)
這篇文章主要介紹了python利用proxybroker構(gòu)建爬蟲免費(fèi)IP代理池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
pyecharts動態(tài)軌跡圖的實(shí)現(xiàn)示例
這篇文章主要介紹了pyecharts動態(tài)軌跡圖的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

