{"id":26016666,"url":"https://github.com/boyac/pyoptionpricing","last_synced_at":"2025-09-08T19:41:37.364Z","repository":{"id":44829628,"uuid":"93248069","full_name":"boyac/pyOptionPricing","owner":"boyac","description":"Option pricing based on Black-Scholes processes, Monte-Carlo simulations with Geometric Brownian Motion, historical volatility, implied volatility, Greeks hedging","archived":false,"fork":false,"pushed_at":"2025-02-24T06:22:27.000Z","size":798,"stargazers_count":304,"open_issues_count":2,"forks_count":31,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-23T20:06:24.968Z","etag":null,"topics":["blackscholes","derivatives","investment-banking","option-pricing","volatility"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/boyac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-03T12:54:28.000Z","updated_at":"2025-05-17T00:40:14.000Z","dependencies_parsed_at":"2025-02-20T06:23:23.732Z","dependency_job_id":"65ac8156-22b0-4338-9b56-46c30b4d4375","html_url":"https://github.com/boyac/pyOptionPricing","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/boyac/pyOptionPricing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boyac%2FpyOptionPricing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boyac%2FpyOptionPricing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boyac%2FpyOptionPricing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boyac%2FpyOptionPricing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boyac","download_url":"https://codeload.github.com/boyac/pyOptionPricing/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boyac%2FpyOptionPricing/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274231433,"owners_count":25245585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-08T02:00:09.813Z","response_time":121,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blackscholes","derivatives","investment-banking","option-pricing","volatility"],"created_at":"2025-03-06T04:32:03.099Z","updated_at":"2025-09-08T19:41:37.329Z","avatar_url":"https://github.com/boyac.png","language":"Python","funding_links":["https://github.com/sponsors/boyac","https://ko-fi.com/boyac"],"categories":[],"sub_categories":[],"readme":"# pyOptionPricing\n\nA collection of Python scripts for option pricing and volatility calculations.\n\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/boyac?style=for-the-badge)](https://github.com/sponsors/boyac)\n[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/boyac)\n\n**If you find this project helpful, please consider supporting its development!** Your contribution helps to maintain and expand this valuable resource for the options trading community.\n\n## Description\n\nThis repository provides implementations of various option pricing models and volatility calculations in Python 2.7. It's a valuable resource for:\n\n*   Students learning about option pricing theory\n*   Quantitative analysts and traders\n*   Anyone interested in exploring financial modeling techniques\n\n## Key Features\n\n*   **Traditional Historical Volatility Calculation:** Simple and easy-to-understand implementation.\n*   **Garman-Klass Historical Volatility:** More sophisticated volatility estimation.\n*   **Black-Scholes Model:** Classic option pricing model.\n*   **Exotic Option Example (Shout Options):** Demonstration of Monte Carlo approximation.\n*   **Clear and Commented Code:** Easy to follow and adapt for your own needs.\n\n## Usage\n\nTo use these scripts, you'll need:\n\n*   Python 2.7\n*   pandas\n*   pandas_datareader\n*   scipy\n\nInstall the dependencies using pip:\n\n```bash\npip install pandas pandas_datareader scipy\n```\n---\n\n### Traditional Historical Volatility Calculation\n![alt tag](image/classical_vol.jpg)\n\n```python\n# -*- coding: utf-8 -*-\n# @Author: boyac\n# @Date:   2016-05-02 18:28:28\n# @Last Modified by:   boyac\n# @Last Modified time: 2016-05-02 19:09:29\n\nfrom pandas import np\nimport pandas_datareader.data as web\n\ndef historical_volatility(sym, days): # stock symbol, number of days\n    \"Return the annualized stddev of daily log returns of picked stock\"\n    try:\n        # past number of 'days' close price data, normally between (30, 60)\n        quotes = web.DataReader(sym, 'yahoo')['Close'][-days:] \n    except Exception, e:\n        print \"Error getting data for symbol '{}'.\\n\".format(sym), e\n        return None, None\n    logreturns = np.log(quotes / quotes.shift(1))\n    # return square root * trading days * logreturns variance\n    # NYSE = 252 trading days; Shanghai Stock Exchange = 242; Tokyo Stock Exchange = 246 days?\n    return np.sqrt(252*logreturns.var()) \n    \n    \nif __name__ == \"__main__\":\n    print historical_volatility('FB', 30) # facebook: 0.296710526109\n```\n\n\n### Garman-Klass Historical Volatility\n![alt tag](image/Garman-Klass_historical_vol.jpg)\n```python\n# -*- coding: utf-8 -*-\n# @Author: boyac\n# @Date:   2016-05-02 18:28:28\n# @Last Modified by:   boyac\n# @Last Modified time: 2016-05-02 19:09:29\n\nfrom pandas import np\nimport pandas_datareader.data as web\n\n\ndef gk_vol(sym, days):\n    \"\"\"\"\n    Return the annualized stddev of daily log returns of picked stock\n    Historical Open-High-Low-Close Volatility: Garman Klass\n    sigma**2 = ((h-l)**2)/2 - (2ln(2) - 1)(c-o)**2\n    ref: http://www.wilmottwiki.com/wiki/index.php?title=Volatility\n    \"\"\"\n\n    try:\n    \to = web.DataReader(sym, 'yahoo')['Open'][-days:] \n    \th = web.DataReader(sym, 'yahoo')['High'][-days:] \n    \tl = web.DataReader(sym, 'yahoo')['Low'][-days:] \n        c = web.DataReader(sym, 'yahoo')['Close'][-days:]\n    except Exception, e:\n        print \"Error getting data for symbol '{}'.\\n\".format(sym), e\n        return None, None\n    sigma = np.sqrt(252*sum((np.log(h/l))**2/2 - (2*np.log(2)-1)*(np.log(c/o)**2))/days)\n    return sigma\n    \n    \nif __name__ == \"__main__\":\n    print gk_vol('FB', 30) # facebook: 0.223351260219\n```\n\n\n### Black-Scholes Model\n![alt tag](image/blackscholes.jpg)\n```python\n# -*- coding: utf-8 -*-\n# @Author: boyac\n# @Date:   2016-05-02 18:28:28\n# @Last Modified by:   boyac\n# @Last Modified time: 2016-05-04 00:27:52\n\nfrom __future__ import division\nfrom scipy.stats import norm\nfrom math import *\n\n# Cumulative normal distribution\ndef CND(X):\n    return norm.cdf(X)\n\n# Black Sholes Function\ndef BlackScholes(CallPutFlag,S,K,t,r,s):\n    \"\"\"\n    S = Current stock price\n    t = Time until option exercise (years to maturity)\n    K = Option striking price\n    r = Risk-free interest rate\n    N = Cumulative standard normal distribution\n    e = Exponential term\n    s = St. Deviation (volatility)\n    Ln = NaturalLog\n    \"\"\"\n    d1 = (log(S/K) + (r + (s ** 2)/2) * t)/(s * sqrt(t))\n    d2 = d1 - s * sqrt(t)\n\n    if CallPutFlag=='c':\n        return S * CND(d1) - K * exp(-r * t) * CND(d2) # call option\n    else:\n        return K * exp(-r * t) * CND(-d2) - S * CND(-d1) # put option \n\n\nif __name__ == \"__main__\":\n    # Number taken from: http://wiki.mbalib.com/wiki/Black-Scholes期权定价模型\n    print BlackScholes('c', S=164.0, K=165.0, t=0.0959, r=0.0521, s=0.29) # 5.788529972549341\n```\n\n### Exotic Options Example: Shout Options by Monte Carlo Approximation\n![alt tag](image/MC2.png)\n![alt tag](image/Shout2.png)\n\n\n## Support\n\nThis project is made possible by the generous support of its users. If you find this project helpful, please consider:\n\n[![GitHub Sponsors](https://img.shields.io/github/sponsors/boyac?style=for-the-badge)](https://github.com/sponsors/boyac)\n[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/boyac)\n![Downloads](https://img.shields.io/github/downloads/boyac/pyOptionPricing/total)\n*   Becoming a GitHub Sponsor\n*   Buying me a coffee on Ko-fi\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboyac%2Fpyoptionpricing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboyac%2Fpyoptionpricing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboyac%2Fpyoptionpricing/lists"}