Code Monkey home page Code Monkey logo

vanilla-option-pricing's Introduction

Vanilla Option Pricing

Actions Status codecov Documentation Status Downloads DOI

A Python package implementing stochastic models to price financial options.
The theoretical background and a comprehensive explanation of models and their parameters can be found is the paper Fast calibration of two-factor models for energy option pricing by Emanuele Fabbiani, Andrea Marziali and Giuseppe De Nicolao, freely available on arXiv. A software paper describing the repository can be found in Software Impact.

Installing

The preferred way to install the package is using pip, but you can also download the code and install from source

To install the package using pip:

pip install vanilla_option_pricing

Quickstart

Let's create a call option.

from datetime import datetime, timedelta
from vanilla_option_pricing.option import VanillaOption

option = VanillaOption(
    spot=100,
    strike=101,
    dividend=0,
    date=datetime.today(),
    maturity=datetime.today() + timedelta(days=30),
    option_type='c',
    price=1,
    instrument='TTF'
)

We can compute the implied volatility and create a Geometric Brownian Motion model with it. Of course, if now we ask price the option using the Black framework, we'll get back the initial price.

from vanilla_option_pricing.models import GeometricBrownianMotion

volatility = option.implied_volatility_of_undiscounted_price
gbm_model = GeometricBrownianMotion(volatility)
gbm_price = gbm_model.price_option_black(option)
print(f'Actual price: {option.price}, model price: {gbm_price}')

But, if we adopt a different model, say a Log-spot price mean reverting to generalised Wiener process model (MLR-GW), we will get a different price.

import numpy as np
from vanilla_option_pricing.models import LogMeanRevertingToGeneralisedWienerProcess

p_0 = np.eye(2)
model = LogMeanRevertingToGeneralisedWienerProcess(p_0, 1, 1, 1)
lmrgw_price = model.price_option_black(option)
print(f'Actual price: {option.price}, model price: {lmrgw_price}')

In the previous snippet, the parameters of the LMR-GW model were chosen at random. We can also calibrate the parameters of a model against listed options.

from datetime import date
from vanilla_option_pricing.option import VanillaOption
from vanilla_option_pricing.models import OrnsteinUhlenbeck, GeometricBrownianMotion
from vanilla_option_pricing.calibration import ModelCalibration

data_set = [
    VanillaOption('TTF', 'c', date(2018, 1, 1), 2, 101, 100, date(2018, 2, 1)),
    VanillaOption('TTF', 'p', date(2018, 1, 1), 2, 98, 100, date(2018, 2, 1)),
    VanillaOption('TTF', 'c', date(2018, 1, 1), 5, 101, 100, date(2018, 5, 31))
]

models = [
    GeometricBrownianMotion(0.2),
    OrnsteinUhlenbeck(p_0=0, l=100, s=2)
]
calibration = ModelCalibration(data_set)

print(f'Implied volatilities: {[o.implied_volatility_of_undiscounted_price for o in data_set]}\n')

for model in models:
    result, trained_model = calibration.calibrate_model(model)
    print('Optimization results:')
    print(result)
    print(f'Calibrated parameters: {trained_model.parameters}\n\n')

vanilla-option-pricing's People

Contributors

donlelef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.