-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpricing-risk.rkt
54 lines (46 loc) · 2.44 KB
/
pricing-risk.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang racket/base
(require math/distributions)
(provide black-scholes
black-scholes-delta
black-scholes-gamma
black-scholes-theta
black-scholes-vega
black-scholes-rho)
(define (black-scholes price years-left strike call-put rate vol divs)
(let* ([discounted-price
(- price (foldl (λ (div res)
(if (>= years-left (vector-ref div 0))
(+ res (* (vector-ref div 1)
(exp (* -1 rate (vector-ref div 0)))))
res))
0
divs))]
[d-1 (* (/ 1 (* vol (sqrt years-left)))
(+ (log (/ discounted-price strike))
(* (+ rate (/ (* vol vol) 2))
years-left)))]
[d-2 (- d-1 (* vol (sqrt years-left)))]
[pv (* strike (exp (* -1 rate years-left)))])
(cond [(or (equal? call-put 'Call) (equal? call-put 'call))
(- (* (cdf (normal-dist) d-1) discounted-price)
(* (cdf (normal-dist) d-2) pv))]
[(or (equal? call-put 'Put) (equal? call-put 'put))
(- (* (cdf (normal-dist) (* d-2 -1)) pv)
(* (cdf (normal-dist) (* d-1 -1)) discounted-price))])))
(define (black-scholes-delta price years-left strike call-put rate vol divs)
(* (- (black-scholes (+ price 1/100) years-left strike call-put rate vol divs)
(black-scholes price years-left strike call-put rate vol divs))
100))
(define (black-scholes-gamma price years-left strike call-put rate vol divs)
(* (- (black-scholes-delta (+ price 1/100) years-left strike call-put rate vol divs)
(black-scholes-delta price years-left strike call-put rate vol divs))
100))
(define (black-scholes-theta price years-left strike call-put rate vol divs)
(- (black-scholes price (max (- years-left 1/365) 1/1000000) strike call-put rate vol divs)
(black-scholes price years-left strike call-put rate vol divs)))
(define (black-scholes-vega price years-left strike call-put rate vol divs)
(- (black-scholes price years-left strike call-put rate (+ vol 1/100) divs)
(black-scholes price years-left strike call-put rate vol divs)))
(define (black-scholes-rho price years-left strike call-put rate vol divs)
(- (black-scholes price years-left strike call-put (+ rate 1/100) vol divs)
(black-scholes price years-left strike call-put rate vol divs)))