2025-05-13 09:11:14
แม้ว่า Python จะเป็นที่รู้จักในฐานะภาษา scripting ที่ใช้เชื่อมต่อส่วนต่างๆ ของระบบซอฟต์แวร์ แต่ด้วยเครื่องมืออย่าง NumPy และ SciPy มันมีความสามารถเพียงพอในการใช้กำหนดราคาทางเลือก (Option Pricing) อย่างเต็มรูปแบบ
เหตุผลที่ควรเลือกใช้ Python มีดังนี้:
บทความชุดนี้จะเน้นที่ “ความเรียบง่ายก่อนการเพิ่มประสิทธิภาพ” ตามแนวคิดของ Daniel Duffy:
“ทำให้มันทำงานได้ก่อน จากนั้นทำให้มันถูกต้อง แล้วจึงค่อยปรับแต่งให้เร็ว”
สูตร Black-Scholes สำหรับคำนวณราคาของ European Vanilla Call Option มีดังนี้:
C=SN(d1)−Ke−rTN(d2)
โดยที่:
d1=vTln(S/K)+(r+21v2)T,d2=d1−vT
จาก Put-Call Parity เราสามารถคำนวณราคาของ Put Option ได้โดย:
P=Ke−rTN(−d2)−SN(−d1)
สร้างไฟล์ statistics.py แล้วใส่โค้ดดังนี้:
python
CopyEdit
from math import exp, log, pi
def norm_pdf(x):
"""
Standard normal probability density function
"""
return (1.0/((2*pi)**0.5)) * exp(-0.5 * x * x)
def norm_cdf(x):
"""
Approximation to the cumulative distribution function for standard normal
"""
k = 1.0 / (1.0 + 0.2316419 * abs(x))
k_sum = k * (0.319381530 + k * (-0.356563782 +
k * (1.781477937 + k * (-1.821255978 + 1.330274429 * k))))
result = 1.0 - (1.0 / ((2 * pi)**0.5)) * exp(-0.5 * x * x) * k_sum
return result if x >= 0 else 1.0 - result
สร้างไฟล์ใหม่ชื่อ closed_form.py แล้วใส่โค้ดนี้:
python
CopyEdit
from math import exp, log, sqrt
from statistics import norm_pdf, norm_cdf
def d_j(j, S, K, r, v, T):
"""
Computes d1 (j=1) or d2 (j=2) used in Black-Scholes
"""
return (log(S/K) + (r + ((-1)**(j-1))*0.5*v*v)*T) / (v * sqrt(T))
def vanilla_call_price(S, K, r, v, T):
"""
Price of a European call option
"""
return S * norm_cdf(d_j(1, S, K, r, v, T)) - \
K * exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T))
def vanilla_put_price(S, K, r, v, T):
"""
Price of a European put option
"""
return -S * norm_cdf(-d_j(1, S, K, r, v, T)) + \
K * exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T))
ขั้นตอนต่อไปคือการ ตรวจสอบผลลัพธ์ ว่าสูตรเหล่านี้ให้ราคาที่ถูกต้องและเป็นไปตามเงื่อนไข เช่น Put-Call Parity
อ้างอิง : European Vanilla Call-Put Option Pricing with Python
จาก https://www.quantstart.com/articles/European-Vanilla-Call-Put-Option-Pricing-with-Python/
2025-01-10 10:12:01
2024-06-10 03:19:31
2024-05-31 03:06:49
2024-05-28 03:09:25
บทความที่น่าสนใจอื่นๆยังมีอีกมากลองเลืือกดูจากด้านล่างนี้ได้นะครับ
2023-09-29 10:22:30
2024-10-10 10:32:48
2023-11-06 11:34:51
2023-10-17 04:52:45
2024-10-28 01:27:23
2023-11-09 01:05:30
2023-09-12 01:36:07
2024-05-02 09:27:07
2023-10-31 02:07:58