DMelt:Finance/Financial Computations

From HandWiki
Member


Financial computations

DataMelt can be used for financial computations. It provides several mathematical and statistical tools needed for the valuation of shares, options, futures, swaps, and other financial instruments, also providing tools related to risk management and money management.

Equity option values

In this section we calculate equity option values with a number of methods, such as Black-Scholes, Barone-Adesi/Whaley, Bjerksund/Stensland, Ju Quadratic.

This code is based on the package "org.jquantlib" which is included by default in DataMelt. Look at the API of jquantlib project.

# @see http://quantlib.org/reference/_equity_option_8cpp-example.html
# @author Richard Gomes & S. Chekanov 

from org.jquantlib import *
from org.jquantlib.exercise import EuropeanExercise,AmericanExercise 
from org.jquantlib.instruments import Option,PlainVanillaPayoff,VanillaOption,EuropeanOption 
from org.jquantlib.pricingengines.vanilla import BjerksundStenslandApproximationEngine,BaroneAdesiWhaleyApproximationEngine,BaroneAdesiWhaleyApproximationEngine
from org.jquantlib.methods.lattices import *
from org.jquantlib.pricingengines import *
from org.jquantlib.time import Month,Date 
from org.jquantlib.time.calendars import Target 
from org.jquantlib.daycounters import Actual365Fixed 
from org.jquantlib.quotes import SimpleQuote,Handle 
from org.jquantlib.termstructures.yieldcurves import FlatForward 
from org.jquantlib.termstructures.volatilities import *
from org.jquantlib.processes import *
from org.jquantlib.pricingengines.vanilla import JuQuadraticApproximationEngine 


# setup dates
calendar =Target()
todaysDate = Date(15, Month.May, 1998)
settlementDate = Date(17, Month.May, 1998)
Settings().setEvaluationDate(todaysDate)

# options
type=Option.Type.Put
strike = 40
underlying = 36
riskFreeRate = 0.06
volatility = 0.2
dividendYield = 0

maturity = Date(17, Month.May, 1999)
dayCounter = Actual365Fixed()

# Define exercise for European Options and setEvaluationDate(todaysDate);
europeanExercise =EuropeanExercise(maturity)
americanExercise = AmericanExercise(settlementDate, maturity);


payoff = PlainVanillaPayoff(type, strike)
underlyingH = Handle(SimpleQuote(underlying))
flatDividendTS =Handle(FlatForward(settlementDate, dividendYield, dayCounter))
flatTermStructure =Handle(FlatForward(settlementDate, riskFreeRate, dayCounter))
flatVolTS = Handle(BlackConstantVol(settlementDate, calendar, volatility, dayCounter))
bsmProcess = BlackScholesMertonProcess(underlyingH, flatDividendTS, flatTermStructure, flatVolTS)


#  Black-Scholes for European
europeanOption = EuropeanOption(payoff, europeanExercise)
method = "Black-Scholes"
europeanOption.setPricingEngine(AnalyticEuropeanEngine(bsmProcess))
print method, europeanOption.NPV()