Skip to content

wegamekinglc/Derivatives-Algorithms-Lib

Repository files navigation

DAL - Derivatives Algorithms Lib

Build Status Build and test
Coverage coverage

Introduction

This is a project inspired by following books & codes repositories:

Some codes are directly copied from above resources.

Install

Downloads

just download source codes from github and don't forget to get the submodule

$ git clone git@github.com:wegamekinglc/Derivatives-Algorithms-Lib.git
$ cd Derivatives-Algorithms-Lib
$ git submodule init
$ git submodule update

Windows

Prerequisite

  • git
  • cmake
  • anaconda python distribution (only for python binding)
  • swig (only for python)
  • Visual studio 2022 community edition

build

$ ./build_windows.bat

after built, you will get:

  • ./lib: the static library and xll excel extension.
  • ./bin: all the runnable examples

Linux

Prerequisite

  • git
  • cmake
  • anaconda python distribution (only for python binding)
  • swig (only for python)
  • zip
  • g++

build

$ bash build_linux.sh

after built, you will get:

  • ./lib: the static library.
  • ./bin: all the runnable examples.

python binding

both for windows and linux user:

$ cd public/python
$ python setup.py wrap
$ python setup.py install

Examples

Interface

Excel

NOTE: This part is only in infancy and should evolve quickly.

We will give a public interface to show the functionality of this project.

we have following data table

x y
1 10
3 8
5 6
7 4
9 2

and we will use follow excel function to create a linear interpolator:

=INTERP1.NEW.LINEAR(E1,A2:A6,B2:B6)  # return a object string id, e.g. ~Interp1~my.interp~2F18E558

later we can use the interpolator:

=INTERP1.GET("~Interp1~my.interp~2F18E558", 6.5)  # will return 4.5

Scripted Exotic Option Pricing

We will price an european option with our script ability and a basic BS model

The product will be described in excel like :

Date Event
2022/9/25 call pays MAX(spot() - 120, 0.0)

and we can create a product in excel with the above table:

=PRODUCT.NEW("my_product", A2, B2)

then we set a model to price this:

Field Value
spot 100
vol 0.15
rate 0.0
dividend 0.0
=BSMODELDATA.NEW("model", D2, D3, D4, D5)

finally we price this product with the model:

=MONTECARLO.VALUE(A5, C7, 2^20, "sobol", FALSE)
value 4.0389

other excel based exotic products example

Some screenshot

img.png

Python

The above simple european option pricing example can also be replicated in python:

from dal import *

today = Date_(2022, 9, 15)
EvaluationDate_Set(today)

spot = 100.0
vol = 0.15
rate = 0.0
div = 0.0
strike = 120.0
maturity = Date_(2025, 9, 15)

n_paths = 2 ** 20
use_bb = False
rsg = "sobol"
model_name = "bs"

event_dates = [maturity]
events = [f"call pays MAX(spot() - {strike}, 0.0)"]

product = Product_New(event_dates, events)
model = BSModelData_New(spot, vol, rate, div)

res = MonteCarlo_Value(product, model, n_paths, rsg, False, True)
vega = 0.0
for k, v in res.items():
    print(f"{k:<8}: {v:>10.4f}")

The output should look like:

d_div   :   -85.2290
d_rate  :    73.1011
d_spot  :     0.2838
d_vol   :    58.7140
value   :     4.0389

other python based exotic products example