There are no reviews yet. Be the first to send feedback to the community and the maintainers!
Repository Details
Option pricing based on Black-Scholes processes, Monte-Carlo simulations with Geometric Brownian Motion, historical volatility, implied volatility, Greeks hedging
pyOptionPricing
Content
use python 2.7
option pricing
Your Support
You can contribute to the project by reporting bugs, suggesting enhancements, exchanging portfolio management experiences or
you can make a donation to this project:
*
Traditional Historical Volatility Calculation
# -*- coding: utf-8 -*-# @Author: boyac# @Date: 2016-05-02 18:28:28# @Last Modified by: boyac# @Last Modified time: 2016-05-02 19:09:29frompandasimportnpimportpandas_datareader.dataaswebdefhistorical_volatility(sym, days): # stock symbol, number of days"Return the annualized stddev of daily log returns of picked stock"try:
# past number of 'days' close price data, normally between (30, 60)quotes=web.DataReader(sym, 'yahoo')['Close'][-days:]
exceptException, e:
print"Error getting data for symbol '{}'.\n".format(sym), ereturnNone, Nonelogreturns=np.log(quotes/quotes.shift(1))
# return square root * trading days * logreturns variance# NYSE = 252 trading days; Shanghai Stock Exchange = 242; Tokyo Stock Exchange = 246 days?returnnp.sqrt(252*logreturns.var())
if__name__=="__main__":
printhistorical_volatility('FB', 30) # facebook: 0.296710526109
# -*- coding: utf-8 -*-# @Author: boyac# @Date: 2016-05-02 18:28:28# @Last Modified by: boyac# @Last Modified time: 2016-05-04 00:27:52from __future__ importdivisionfromscipy.statsimportnormfrommathimport*# Cumulative normal distributiondefCND(X):
returnnorm.cdf(X)
# Black Sholes FunctiondefBlackScholes(CallPutFlag,S,K,t,r,s):
""" S = Current stock price t = Time until option exercise (years to maturity) K = Option striking price r = Risk-free interest rate N = Cumulative standard normal distribution e = Exponential term s = St. Deviation (volatility) Ln = NaturalLog """d1= (log(S/K) + (r+ (s**2)/2) *t)/(s*sqrt(t))
d2=d1-s*sqrt(t)
ifCallPutFlag=='c':
returnS*CND(d1) -K*exp(-r*t) *CND(d2) # call optionelse:
returnK*exp(-r*t) *CND(-d2) -S*CND(-d1) # put option if__name__=="__main__":
# Number taken from: http://wiki.mbalib.com/wiki/Black-Scholes期权定价模型printBlackScholes('c', S=164.0, K=165.0, t=0.0959, r=0.0521, s=0.29) # 5.788529972549341
Exotic Options Example: Shout Options by Monte Carlo Approximation