FC2ブログ

# ブラック-ショールズモデル
# 引数 原資産価格 権利行使価格 残存期間(日) 変動率(%) 安全資産利子率(%) 原資産利回り(%) ($Price, $Strike, $CurrentMaturity, $Volatility, $RiskFreeRate, $DividendYield)
# 戻り値 hash ({'price'} {'delta'} {'gamma'} {'vega'} {'theta'} {'rho'} {'vanna'} {'charm'} {'zomma'} {'color'} {'dualdelta'} {'dualgamma'} {'omega'}) -> ({'call'} {'put'}) (%bsmodel)
sub BSMODEL{
my ($Price, $Strike, $CurrentMaturity, $Volatility, $RiskFreeRate, $DividendYield) = @_;
my %bsmodel = ();
my $Year = 365;
my $pi = atan2(1, 1) * 4;
my $SqrtCurrentMaturity = 0;
$CurrentMaturity = $CurrentMaturity / $Year;
$SqrtCurrentMaturity = sqrt($CurrentMaturity);
$Volatility = $Volatility / 100;
$RiskFreeRate = $RiskFreeRate / 100;
$DividendYield = $DividendYield / 100;

# 計算
my $num1 = log($Price / $Strike);
my $num2 = ($RiskFreeRate - $DividendYield + (($Volatility * $Volatility) / 2)) * $CurrentMaturity;
my $den = $Volatility * $SqrtCurrentMaturity;
my $d1 = ($num1 + $num2) / $den;
my $d2 = $d1 - ($Volatility * $SqrtCurrentMaturity);
my $call_d1 = &NORMDIST($d1);
my $call_d2 = &NORMDIST($d2);
my $put_d1 = &NORMDIST(-$d1);
my $put_d2 = &NORMDIST(-$d2);
my $nd1 = exp(-($d1 * $d1) / 2) / sqrt(2 * $pi);
my $nd2 = exp(-($d2 * $d2) / 2) / sqrt(2 * $pi);

# 理論価格
$bsmodel{'price'}->{'call'} = (exp(-($CurrentMaturity * $DividendYield)) * ($Price * $call_d1)) - ($Strike * exp(-($CurrentMaturity * $RiskFreeRate)) * $call_d2);
$bsmodel{'price'}->{'put'} = ($Strike * exp(-($CurrentMaturity * $RiskFreeRate)) * $put_d2) -(exp(-($CurrentMaturity * $DividendYield)) * ($Price * $put_d1));
# デルタ
$bsmodel{'delta'}->{'call'} = exp(-($CurrentMaturity * $DividendYield)) * $call_d1;
$bsmodel{'delta'}->{'put'} = -(exp(-($CurrentMaturity * $DividendYield))) * $put_d1;
# ガンマ
my $gamma = (exp(-($CurrentMaturity * $DividendYield)) * $nd1) / ($Price * $Volatility * $SqrtCurrentMaturity);
$bsmodel{'gamma'}->{'call'} = $gamma;
$bsmodel{'gamma'}->{'put'} = $gamma;
# ベガ
my $vega = $Price * exp(-($CurrentMaturity * $DividendYield)) * $nd1 * $SqrtCurrentMaturity / 100;
$bsmodel{'vega'}->{'call'} = $vega;
$bsmodel{'vega'}->{'put'} = $vega;
# セータ
$bsmodel{'theta'}->{'call'} = -(((exp(-($CurrentMaturity * $DividendYield)) * $Price * $nd1 * $Volatility) / (2 * $SqrtCurrentMaturity)) - ($RiskFreeRate * $Strike * exp(-($CurrentMaturity * $RiskFreeRate)) * $call_d2) + ($DividendYield * $Price * exp(-($CurrentMaturity * $DividendYield)) * $call_d1)) / $Year;
$bsmodel{'theta'}->{'put'} = -(((exp(-($CurrentMaturity * $DividendYield)) * $Price * $nd1 * $Volatility) / (2 * $SqrtCurrentMaturity)) + ($RiskFreeRate * $Strike * exp(-($CurrentMaturity * $RiskFreeRate)) * $put_d2) - ($DividendYield * $Price * exp(-($CurrentMaturity * $DividendYield)) * $put_d1)) / $Year;
# ロー 安全資産利子率
$bsmodel{'rho'}->{'call'} = ($Strike * $CurrentMaturity * exp(-($CurrentMaturity * $RiskFreeRate)) * $call_d2) / 100;
$bsmodel{'rho'}->{'put'} = -($Strike * $CurrentMaturity * exp(-($CurrentMaturity * $RiskFreeRate)) * $put_d2) / 100;
# ロー 原資産利回り
# $bsmodel{'rho'}->{'call'} = -($CurrentMaturity * $Price * exp(-($CurrentMaturity * $DividendYield)) * $call_d1) / 100;
# $bsmodel{'rho'}->{'put'} = ($CurrentMaturity * $Price * exp(-($CurrentMaturity * $DividendYield)) * $put_d1) / 100;


# 動作している????????????????????????
# Vanna
my $vanna = -(exp(-($CurrentMaturity * $DividendYield))) * $nd1 * ($d2 / $Volatility);
$bsmodel{'vanna'}->{'call'} = $vanna;
$bsmodel{'vanna'}->{'put'} = $vanna;
# Charm
$bsmodel{'charm'}->{'call'} = -($DividendYield * exp(-($CurrentMaturity * $DividendYield)) * $call_d1) + (exp(-($CurrentMaturity * $DividendYield)) * $nd1 * ((2 * ($RiskFreeRate - $DividendYield) * $CurrentMaturity) - ($d2 * $Volatility * $SqrtCurrentMaturity)) / (2 * $CurrentMaturity * $Volatility * $SqrtCurrentMaturity));
$bsmodel{'charm'}->{'put'} = ($DividendYield * exp(-($CurrentMaturity * $DividendYield)) * $put_d1) + (exp(-($CurrentMaturity * $DividendYield)) * $nd1 * ((2 * ($RiskFreeRate - $DividendYield) * $CurrentMaturity) - ($d2 * $Volatility * $SqrtCurrentMaturity)) / (2 * $CurrentMaturity * $Volatility * $SqrtCurrentMaturity));
# Zomma
my $zomma = exp(-($CurrentMaturity * $DividendYield)) * ((($nd1 * $d1 * $d2) - 1) / ($Price * $Volatility * $Volatility * $SqrtCurrentMaturity));
$bsmodel{'zomma'}->{'call'} = $zomma;
$bsmodel{'zomma'}->{'put'} = $zomma;
# Color
my $color = ((exp(-($CurrentMaturity * $DividendYield)) * $nd1) / ($Volatility * $Price * $SqrtCurrentMaturity)) * ($DividendYield + ((1 - ($d1 * $d2)) / (2 * $CurrentMaturity)) - (($d1 * ($RiskFreeRate - $DividendYield) / ($Volatility * $SqrtCurrentMaturity))));
$bsmodel{'color'}->{'call'} = $color;
$bsmodel{'color'}->{'put'} = $color;
# Dual Delta
$bsmodel{'dualdelta'}->{'call'} = -(exp(-($CurrentMaturity * $RiskFreeRate))) * $call_d2;
$bsmodel{'dualdelta'}->{'put'} = exp(-($CurrentMaturity * $RiskFreeRate)) * $put_d2;
# Dual Gamma
my $dualgamma = exp(-($CurrentMaturity * $RiskFreeRate)) * ($nd2 / ($Strike * $Volatility * $SqrtCurrentMaturity));
$bsmodel{'dualgamma'}->{'call'} = $dualgamma;
$bsmodel{'dualgamma'}->{'put'} = $dualgamma;
# Omega or Lambda
$bsmodel{'omega'}->{'call'} = $bsmodel{'delta'}->{'call'} * ($Price / $bsmodel{'price'}->{'call'});
$bsmodel{'omega'}->{'put'} = $bsmodel{'delta'}->{'put'} * ($Price / $bsmodel{'price'}->{'put'});


# 数値が変???????????????
# Speed
my $speed = -($gamma / $Price) * (($d1 / ($Volatility * $SqrtCurrentMaturity)) + 1);
$bsmodel{'speed'}->{'call'} = $speed;
$bsmodel{'speed'}->{'put'} = $speed;
# Vomma or Volga
my $vomma = $Price * $SqrtCurrentMaturity * exp(-($CurrentMaturity * $DividendYield)) * $nd1 * (($d1 * $d2) / $Volatility);
$bsmodel{'vomma'}->{'call'} = $vomma;
$bsmodel{'vomma'}->{'put'} = $vomma;


# 式が読めない!!!!!!!!!
# DvegaDtime
#$bsmodel{'dvegadtime'}->{'call'} =
#$bsmodel{'dvegadtime'}->{'put'} =
# Ultima
#$bsmodel{'ultima'}->{'call'} =
#$bsmodel{'ultima'}->{'put'} =

return %bsmodel;
}



# 正規分布関数
# 引数 値 ($Price)
# 戻り値 正規分布関数の値 ($normdist)
sub NORMDIST {
my ($Price) = @_;
my $normdist = 0;
my $pi = atan2(1, 1) * 4;
# my $mean = 0;
# my $sigma = 1;
# my $precision = 7;
my $precision = 10**-7;

my $z = $Price;
my $z2 = $z * $z;
my $p = $z * exp(-0.5 * $z2) / sqrt(2 * $pi);
my $s = $p;

for (my $n = 3; abs($s) > $precision; $n += 2) {
$s *= $z2 / $n;
$p += $s;
}

# 正規分布関数
$normdist = 0.5 + $p;

return $normdist;
}


参考URL
Greeks (finance) - Wikipedia
Chicheng Zhang / Finance-Options-Calc-0.90 - search.cpan.org
△▲ WebProg 初心者の質問 Part21 ▼▽ 
http://pc11.2ch.net/test/read.cgi/php/1257701535/123-
オンライン コンパイラ/インタプリタ
テクニカル分析
プロフィール

Author:雨宮
Firefoxを使用しているので気づかなかったけど、IE6でソースコードを上手くコピーできない

5/3
携帯用ならIE6でもソースコードをコピーできる
携帯用

検索フォーム


あわせて読みたいブログパーツ
一寸先は闇 RSS