Giter VIP home page Giter VIP logo

cairo-black-scholes's Introduction

Cairo Black-Scholes Library

Black-Scholes library implemented as a Cairo smart contract (black_scholes_contract.cairo). The address of the contract on the goerli testnet is: 0x044a7311f4dec0bd09ae59e2fc5d8ab6967cc32bd1e4dd3b4538f1d65450a36e. See on voyager here.

No un-whitelisted hints were used; functions like ln and exp are implemented with numerical approximations in native Cairo. All inputs, outputs, and internal calculations use 27-digit fixed-point numbers.

Example usage of contract:

Inputs are:
t_annualised = 1000000000000000000000000000 (1 year)
volatility = 150000000000000000000000000 (15%)
spot price = 300000000000000000000000000000 ($300)
strike price = 250000000000000000000000000000 ($250)
interest rate = 30000000000000000000000000 (3%)

Option price calculation:
starknet call --address 0x044a7311f4dec0bd09ae59e2fc5d8ab6967cc32bd1e4dd3b4538f1d65450a36e --abi black_scholes_contract_abi.json  --function option_prices --inputs 1000000000000000000000000000 150000000000000000000000000 300000000000000000000000000000 250000000000000000000000000000 30000000000000000000000000

Results are (call price = $58.82, put price = $1.43):
0xbe0e94e51c07da31bc68edb7 0x49fd4a0ba906e391f8e8589

Delta calculation:
starknet call --address 0x044a7311f4dec0bd09ae59e2fc5d8ab6967cc32bd1e4dd3b4538f1d65450a36e --abi black_scholes_contract_abi.json  --function delta --inputs 1000000000000000000000000000 150000000000000000000000000 300000000000000000000000000000 250000000000000000000000000000 30000000000000000000000000

Results are (call delta = 0.932, put delta = -0.068):
0x302e63a1bd2e76d922c0000 -0x38480283fd98cf55d40000

Library

This library implements the interface described here. The external functions exposed allow you to retrieve the Black-Scholes call and put option prices and the option greeks, given the expiration time, volatility, spot price, strike price, and interest rate.

Validation

The pytest deploys the smart contract in a local test environment and queries for the call and put option prices with randomly generated test inputs. Each test input consists of (expiration time, volatility, spot price, strike price, interest rate).

The results are compared against the options prices as computed by the py_vollib library. The test validates that the error from the Cairo library and the py_vollib libary is minimal. Option greeks were validated against online tools such as this one.

Test Instructions

  1. First make sure you can run a basic StarkNet contract unit test (see here).
  2. Install py_vollib: pip install py_vollib.
  3. Make sure you're using Cairo v0.7.0 or later.
  4. Clone this repo and run pytest --capture=no black_scholes_test.py.

Sample Test Output

Example output from running the pytest:

Input 0:
t_annualised: 2.26664 years
volatility: 10.56165%
spot price: $326.08126
strike price: $321.88154
interest rate: 32.53044%

Result 0:
Computed call price: $172.09933, Expected call price: $172.09932
Computed put price: $0.00001, Expected put price: $0.00001

Input 1:
t_annualised: 4.92524 years
volatility: 28.79982%
spot price: $747.82545
strike price: $885.09734
interest rate: 4.57839%

Result 1:
Computed call price: $203.70129, Expected call price: $203.70106
Computed put price: $162.28934, Expected put price: $162.28911

Input 2:
t_annualised: 4.15624 years
volatility: 41.86169%
spot price: $144.70197
strike price: $407.66489
interest rate: 17.34711%

Result 2:
Computed call price: $33.43700, Expected call price: $33.43693
Computed put price: $86.97104, Expected put price: $86.97096

...

cairo-black-scholes's People

Contributors

araghava avatar mjs-12 avatar

Stargazers

yettaahhh avatar Krati Jain 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.