{"id":17830,"date":"2023-05-30T14:00:00","date_gmt":"2023-05-30T06:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=17830"},"modified":"2023-09-07T10:06:33","modified_gmt":"2023-09-07T02:06:33","slug":"%e3%80%90quant%e3%80%91crr-model","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/","title":{"rendered":"\u3010Quant\u3011CRR Model"},"content":{"rendered":"\n<p id=\"7043\">Programming CRR model for calculating options theoretical price.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1VOmOH4kagRmM4x7hGUURig.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<a href=\"https:\/\/unsplash.com\/@simonfromengland?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Simon Wilkes<\/a>&nbsp;on&nbsp;<a href=\"https:\/\/unsplash.com\/images\/nature\/tree?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f7be156a4a5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f7be156a4a5\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Keyword_CRR_model_Options_Call_Put\" >Keyword: CRR model, Options, Call, Put<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Highlight\" >Highlight<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Programming_environment_and_Module_required\" >Programming environment and Module required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Database\" >Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Import_data\" >Import data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Data_processing\" >Data processing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#CRR_Model\" >CRR Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Convergence_property\" >Convergence property<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Practical_Example\" >Practical Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/%e3%80%90quant%e3%80%91crr-model\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1259\"><span class=\"ez-toc-section\" id=\"Keyword_CRR_model_Options_Call_Put\"><\/span>Keyword: CRR model, Options, Call, Put<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"59e5\"><span class=\"ez-toc-section\" id=\"Highlight\"><\/span>Highlight<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n\n\n\n<li>Using transaction data for options pricing.<\/li>\n\n\n\n<li>Advise: The primary focal point for this article is how to program CRR model via Python. Therefore, detailed introduction for CRR model is not included in this article. Previews for options and CRR model is recommended before reading.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"615d\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span>Preface<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"86c1\">In our previous article \u2014 \u3010Quant\u3011Black Scholes model and Greeks, we introduce how to program the Black Scholes model. However, Black Scholes has its disadvantages and can not calculate the theoretical price for American options. Therefore, three years after the appearance of Black Scholes, John Cox, Stephen Ross, and Mark Rubinstein invented a relatively easy and more general options pricing model \u2014 the CRR model.<\/p>\n\n\n\n<p id=\"279a\">The CRR model uses the binomial theorem to discretize the future, assuming that there are only two possibilities for each time point \u2014 an up or a down movement \u2014 and that the magnitude of the movements is fixed. In this way, the future price of the underlying asset can be plotted as a binary tree model as time progresses. Then, calculate the intrinsic value of the option under each node, and take the present value of each node to derive the option\u2019s theoretical value by working backward.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"d2ba\"><span class=\"ez-toc-section\" id=\"Programming_environment_and_Module_required\"><\/span>Programming environment and Module required<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"dcc5\">Windows 11 and Jupyter Notebook are used as editor<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Load required package\r\nimport math \r\nimport tejapi\r\nimport pandas as pd \r\nimport numpy as np \r\nimport matplotlib.pyplot as plt \r\nfrom scipy.stats import norm\r\nplt.style.use('ggplot')\r\n\r\n# log in TEJ API\r\napi_key = 'YOUR_KEY'\r\ntejapi.ApiConfig.api_key = api_key\r\ntejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0c36\"><span class=\"ez-toc-section\" id=\"Database\"><\/span>Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"60ef\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=42\" rel=\"noreferrer noopener\" target=\"_blank\">Stock trading database<\/a>: Unadjusted daily stock price, database code is (TWN\/APRCD).<br><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=100\" rel=\"noreferrer noopener\" target=\"_blank\">Derivatives database<\/a>: Options daily transaction information, database code is (TWN\/AOPTION).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"da9f\"><span class=\"ez-toc-section\" id=\"Import_data\"><\/span>Import data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"29f1\">Using the unadjusted close prices of the Taiwan Weighted Index (Y9999), from January 1, 2021, to April 19, 2023. And loading the Taiwan Weighted Index Call, Put Option (TXO202304C15500, TXO202304P15500), which are European options, with a start trading date of January 1 and an expiration date of April 19, and a strike price of 15500.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># set time zone\r\ngte, lte = '2021-03-16', '2023-04-20'\r\n\r\n# Get stock price\r\nstocks = tejapi.get('TWN\/APRCD',\r\n                   paginate = True,\r\n                   coid = 'Y9999',\r\n                   mdate = {'gte':gte, 'lte':lte},\r\n                   opts = {\r\n                       'columns':&#91; 'mdate','close_d']\r\n                   }\r\n                  )\r\n\r\n# Get options price\r\nputs = tejapi.get( # puts price\r\n    'TWN\/AOPTION',\r\n    paginate = True,\r\n    coid = 'TXO202304P15500',\r\n    mdate = {'gte':gte, 'lte':lte},\r\n    opts = {\r\n        'columns':&#91;'mdate', 'coid','settle', 'kk', 'theoremp', 'acls', 'ex_price', 'td1y', 'avolt', 'rtime']\r\n    }\r\n)\r\ncalls = tejapi.get( # calls price\r\n    'TWN\/AOPTION',\r\n    paginate = True,\r\n    coid = 'TXO202304C15500',\r\n    mdate = {'gte':gte, 'lte':lte},\r\n    opts = {\r\n        'columns':&#91;'mdate', 'coid','settle', 'kk', 'theoremp', 'acls', 'ex_price', 'td1y', 'avolt', 'rtime']\r\n    }\r\n)\r\n\r\n# set index\r\nstocks = stocks.set_index('mdate')\r\nputs = puts.set_index('mdate')\r\ncalls = calls.set_index('mdate')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"e3e0\"><span class=\"ez-toc-section\" id=\"Data_processing\"><\/span>Data processing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"f547\">Calculating daily return and moving volatility, set 252 days as the window.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Calculate stock return and moving volatility\r\nstocks&#91;'daily return'] = np.log(stocks&#91;'close_d']) - np.log(stocks&#91;'close_d'].shift(1))\r\nstocks&#91;'moving volatility'] = stocks&#91;'daily return'].rolling(252).std()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"953a\"><span class=\"ez-toc-section\" id=\"CRR_Model\"><\/span>CRR Model<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"d35b\">As previously mentioned, the CRR model is a more intuitive and flexible options pricing model than Black Scholes. Although it does not have a closed-form solution like Black-Scholes and may be more time-consuming to calculate due to its binary tree structure, the CRR model has advantages over Black-Scholes in pricing options with time-varying characteristics, such as American options and exotic options.<\/p>\n\n\n\n<p id=\"34c5\">The basic formula of the CRR model is to calculate the present value of each node in the binary tree. We can use the following formula to calculate the magnitude and probability of stock price movements in the next period, denoted as u, d, and p, in the diagram below. Then, we can calculate the stock prices going up and down in the following period and form a tree structure as time passes. The black lines and mathematical formulas in the diagram represent the changes in stock prices, while the red lines and mathematical formulas represent the call option\u2019s value. We can see that we first calculate the future changes in stock prices (e.g., uS0, dS0), then subtract the strike price from the stock price to obtain the intrinsic value of the call option (e.g., Ct,1, Ct,2), and finally, step by step, we can calculate the present value of the call option and obtain the theoretical price of the option today.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1noB5L_5Z2eZByp62XsOmVA.png\" alt=\"European call tree structure\"\/><figcaption class=\"wp-element-caption\">European call tree structure<\/figcaption><\/figure>\n\n\n\n<p id=\"3b5a\">For put options, we just need to change the method of calculating the intrinsic value by subtracting the stock price from the strike price (e.g., Pt,0, Pt,1). The rest of the calculation process is the same as for call options in the CRR model.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1SvUP07-8RQx7yum4-TqzkQ.png\" alt=\"European put tree structure\"\/><figcaption class=\"wp-element-caption\">European put tree structure<\/figcaption><\/figure>\n\n\n\n<p id=\"17bd\">Compared to Black Scholes, the CRR model can price American-style options. The so-called American options allow the option holder to exercise the option before the expiration date, while the regular options are called European options. For the holder, as long as the current intrinsic value is greater than the present value obtained by backward induction, there is an incentive to exercise the option. Therefore, the option price at each node will be the maximum value between the present value obtained by backward induction and the intrinsic value, as shown in the highlighted part of the diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1XgNaYts0HzwecfEOd3Qlcw.png\" alt=\"American call tree structure\"\/><figcaption class=\"wp-element-caption\">American call tree structure<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_10lLDHVhO5tFS6pJPTpg8Pw.png\" alt=\"American put tree structure\"\/><figcaption class=\"wp-element-caption\">American put tree structure<\/figcaption><\/figure>\n\n\n\n<p id=\"3155\">After understanding how to calculate options prices using the CRR model, we will move on to the programming part.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class crr_model:\r\n    \r\n    # init fuction\r\n    def __init__(self, s, x, r, t, sigma, N, sigma_daily = True):\r\n        \r\n        t \/= 252 \r\n        dt = t\/N \r\n        if sigma_daily:\r\n            sigma *= np.sqrt(252)\r\n        u = np.exp(sigma * math.sqrt(dt))\r\n        d = 1\/u\r\n        p = (np.exp(r*dt)-d) \/ (u-d)\r\n        \r\n        self.s = s\r\n        self.x = x\r\n        self.r = r\r\n        self.t = t\r\n        self.N = N \r\n        self.dt = dt\r\n        self.u = u\r\n        self.d = d\r\n        self.p = p\r\n        self.sigma = sigma\r\n    \r\n    # european call price\r\n    def eu_call_price(self):\r\n        \r\n        lattice = np.zeros((self.N+1, self.N+1))\r\n        for j in range(self.N+1):\r\n            lattice&#91;self.N, j] = max(0, self.s*(self.u**j)*(self.d**(self.N-j)) - self.x)\r\n        for i in range(self.N-1, -1, -1):\r\n            for j in range(i+1):\r\n                lattice&#91;i, j] = np.exp(-self.r*self.dt)*(self.p*lattice&#91;i+1,j+1] + (1-self.p) * lattice&#91;i+1, j])\r\n    \r\n        return lattice&#91;0,0], lattice\r\n    \r\n    # european put price\r\n    def eu_put_price(self):\r\n        \r\n        lattice = np.zeros((self.N+1, self.N+1))\r\n        for j in range(self.N+1):\r\n            lattice&#91;self.N, j] = max(0, self.x - self.s*(self.u**j)*(self.d**(self.N-j)))\r\n        for i in range(self.N-1, -1, -1):\r\n            for j in range(i+1):\r\n                lattice&#91;i, j] = np.exp(-self.r*self.dt)*(self.p*lattice&#91;i+1,j+1] + (1-self.p) * lattice&#91;i+1, j])\r\n    \r\n        return lattice&#91;0,0], lattice\r\n    \r\n    # american call price\r\n    def am_call_price(self):\r\n        \r\n        lattice = np.zeros((self.N+1, self.N+1))\r\n        for j in range(self.N+1):\r\n            lattice&#91;self.N, j] = max(0, self.s*(self.u**j)*(self.d**(self.N-j)) - self.x)\r\n        for i in range(self.N-1, -1, -1):\r\n            for j in range(i+1):\r\n                lattice&#91;i, j] = max(np.exp(-self.r*self.dt)*(self.p*lattice&#91;i+1,j+1] + (1-self.p) * lattice&#91;i+1, j]),\r\n                                   self.s*(self.u**j)*(self.d**(i-j)) - self.x )\r\n    \r\n        return lattice&#91;0,0], lattice        \r\n    \r\n    # american put price\r\n    def am_put_price(self):\r\n\r\n        lattice = np.zeros((self.N+1, self.N+1))\r\n        for j in range(self.N+1):\r\n            lattice&#91;self.N, j] = max(0, self.x - self.s*(self.u**j)*(self.d**(self.N-j)))\r\n        for i in range(self.N-1, -1, -1):\r\n            for j in range(i+1):\r\n                lattice&#91;i, j] = max(\r\n                    np.exp(-self.r*self.dt)*(self.p*lattice&#91;i+1,j+1] + (1-self.p) * lattice&#91;i+1, j]), \r\n                    self.x - self.s*(self.u**j)*(self.d**(i-j))\r\n                                   )\r\n    \r\n        return lattice&#91;0,0], lattice<\/code><\/pre>\n\n\n\n<p>To replicate the tree structure, we use a nested array of numpy to form an (N+1) x (N+1) matrix, where the top-left element is the option\u2019s theoretical price. The blue line in the lower left corner is the aforementioned tree diagram.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = 100 # set stock price at time 0\r\nx = 95 # set strike price\r\nr = 0 # set risk-free rate = 0%\r\nt = 252 # set time to maturity at 252 days \r\nsigma = 0.3 # set annualized volatility at 0.3\r\nN = 4 # set steps at 4\r\n\r\ncrr = crr_model(s, x, r, t, sigma, N, sigma_daily = False)\r\ncall, call_lat = crr.eu_call_price()\r\ncall_lat<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1ZO_726tOQNieVkkyF0XTZg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8f08\"><span class=\"ez-toc-section\" id=\"Convergence_property\"><\/span>Convergence property<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"48fc\">Keen-eyed readers may have noticed the existence of the variable N, which can be viewed as the number of times the expiration period is divided. For example, if the time to maturity is 252 days and N is set at 251, it means that the future stock price is calculated once a day, and the tree diagram will have 252 layers. That is, the number of layers (N+1) of our tree is determined by N. The question is, what is the optimal value for N to achieve better pricing results? In fact, there is no specific N value, but we can observe that as we increase the number of N, that is, divide time into smaller segments, the calculated option price will approach a constant value. Therefore, if the computer power permits, a higher N value would be better.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># plot the convergency pattern of crr model\r\n\r\ncalls_ = &#91;]\r\nfor n in range(1, 100):\r\n    \r\n    s = 100\r\n    x = 100\r\n    r = 0.011\r\n    t = 52\r\n    sigma = 0.3\r\n    \r\n    crr = crr_model(s, x, r, t, sigma, n, sigma_daily = False)\r\n    call, call_lat = crr.eu_call_price()\r\n    calls_.append(call)\r\n\r\nplt.plot(range(1, 100), calls_, color = 'red')\r\nplt.xlabel('Numbers of N')\r\nplt.ylabel('Theoretical call price')\r\nplt.title('Options price convergency')\r\nplt.savefig('Options price convergency')\r\nplt.show()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Spie5tV1otuFCRw1ecmUxw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">options price convergence<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1c09\"><span class=\"ez-toc-section\" id=\"Practical_Example\"><\/span>Practical Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"3029\">Finally, let\u2019s compare the theoretical prices obtained through the CRR model with the Black Scholes price calculated by TEJ and the actual market price.<br>We use a TAIEX put option with a strike price of 15500, from January 1, 2023, to April 19, 2023. The risk-free rate is set at the yield of the Taiwan 5-year bond (0.011), and Sigma uses the standard deviation of TAIEX returns over a 252-day window. N is set to 1000. The results obtained are shown below, and it can be seen that the CRR price is closer to the actual market price than the Black Scholes price calculated by TEJ.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s = stocks.loc&#91;'2023-01-31']&#91;'close_d'] # get the stock price at the first dat of options trading\r\nx = 15500 # strike price\r\nr = 0.011 # use 5-year Taiwan government bond ytm\r\nt = 51 # time to maturity\r\nsigma = stocks.loc&#91;'2023-01-31']&#91;'moving volatility'] # get the return volatility at the first day od options trading\r\nN = 100 # divided time 2 maturity into 10 parts\r\n\r\ncrr = crr_model(s, x, r, t, sigma, N, sigma_daily = True)\r\ncall, call_lat = crr.eu_call_price() # european call price\r\nput, put_lat = crr.eu_put_price() # european put price\r\ncall_a, call_lat_a = crr.am_call_price() # american call price\r\nput_a, put_lat_a = crr.am_put_price() # american put price\r\n\r\nprint('CRR theoretical price: ', put)\r\nprint('TEJ Black Scholes price: ', puts.loc&#91;'2023-01-31']&#91;'theoremp'])\r\nprint('Real price: ', puts.loc&#91;'2023-01-31']&#91;'settle'])<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1nvFqy-ZgnmQRDkH4IH4sg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f966\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"5e6d\">The CRR model, as a newcomer, offers more flexibility than the Black-Scholes model. In the wave of derivative products that followed, many unique options can be priced using the CRR model or its extension models. Its simple mathematical calculation method and theoretical basis make it a good choice for many novice option investors. In this article, we have completed the programming of the CRR model for pricing European and American options. We will provide more knowledge related to options or derivative products in the future. Please continue to follow us, and readers and investors are also welcome to purchase\u00a0<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/index\" target=\"_blank\" rel=\"noreferrer noopener\">TEJ E Shop<\/a>\u00a0to build their option pricing programs. Please note that the pricing formulas and options products mentioned above are for demonstration purposes only and do not represent any investment or target recommendations.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programming CRR model for calculating options theoretical price. Keyword: CRR model, Options, Call, Put Highlight Preface In our previous article \u2014 \u3010Quant\u3011Black Scholes model and Greeks, we introduce how to program the Black Scholes model. However, Black Scholes has its disadvantages and can not calculate the theoretical price for American options. Therefore, three years after [&hellip;]<\/p>\n","protected":false},"featured_media":17831,"template":"","tags":[2858],"insight-category":[690,50],"class_list":["post-17830","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-2858","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/17830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":0,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/17830\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/17831"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=17830"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=17830"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=17830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}