Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Option pricing with discrete dividends using the Binomial Tree model

Rate me:
Please Sign up or sign in to vote.
4.07/5 (5 votes)
17 Jun 2007CPOL4 min read 42.9K   675   30  
Pricing European and American call and put options using the binomial tree model. Handles discrete dividends paid on underlying.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BinomialTreeUI
{
    public partial class Form1 : Form
    {
        private int _exceptionCount=0;
        public Form1()
        {
            InitializeComponent();
        }

         void button1_Click(object sender, EventArgs e)
        {
            try
            {

            #region  GetFormInformation
                _exceptionCount=0;
                 double _stockPrice = 0.0;
                 double _strike = 0.0;
                 double _timeToExpiration = 0.0;
                 double _volatility = 0.0;
                 double _riskFreeRate = 0.0;

                 EPutCall _putCall = EPutCall.Call;
                 EnumStyle _style = EnumStyle.American;
                
                 int _steps = 100;

                 double _dividendYield = 0;
                 bool _isFuture = false;
                 double _dividend1 = 0;
                 double _dividend1Date = 0;

                 double _dividend2 = 0;
                 double _dividend2Date = 0;

                 double _dividend3 = 0;
                 double _dividend3Date = 0;

                 double _dividend4 = 0;
                 double _dividend4Date = 0;

                //validate information
                if(IsNotNullAndIsPositiveDouble(txtStockPrice.Text))
                    _stockPrice = Convert.ToDouble(txtStockPrice.Text);

                if(IsNotNullAndIsPositiveDouble(txtStrike.Text))
                    _strike = Convert.ToDouble(txtStrike.Text);

                if(IsNotNullAndIsPositiveDouble(txtTimeToExpiration.Text))
                    _timeToExpiration = Convert.ToDouble(txtTimeToExpiration.Text);

                if(IsNotNullAndIsPositiveDouble(txtVol.Text))
                    _volatility = Convert.ToDouble(txtVol.Text);

                if(IsNotNullAndIsPositiveDouble(txtRate.Text))
                    _riskFreeRate = Convert.ToDouble(txtRate.Text);

                if(IsNotNullAndIsPositiveDouble(txtYield.Text))
                    _dividendYield = Convert.ToDouble(txtYield.Text);

                
                if (dividend1.Text!=String.Empty && double.TryParse(dividend1.Text, out _dividend1))
                {
                    if(date1.Text!=String.Empty)
                        _dividend1Date = Convert.ToDouble(date1.Text);
                }
                if (dividend2.Text!=String.Empty && double.TryParse(dividend2.Text, out _dividend2))
                {
                    if(date2.Text!=String.Empty)
                        _dividend2Date = Convert.ToDouble(date2.Text);
                }
                if (dividend3.Text!=String.Empty && double.TryParse(dividend3.Text, out _dividend3))
                {
                    if(date3.Text!=String.Empty)
                        _dividend3Date = Convert.ToDouble(date3.Text);
                }
                if (dividend4.Text!=String.Empty && double.TryParse(dividend4.Text, out _dividend4))
                {
                    if(date4.Text!=String.Empty)
                        _dividend4Date = Convert.ToDouble(date4.Text);
                }


                if (txtSteps.Text != string.Empty && Int32.TryParse(txtSteps.Text,out _steps) && _steps > 0)
                {
                }
                else
                {
                    _steps = 100;
                }

                if(rdoCall.Checked)
                    _putCall = EPutCall.Call;
                else if (rdoPut.Checked)
                    _putCall = EPutCall.Put;

                if(chkAmerican.Checked)
                    _style = EnumStyle.American;
                else
                    _style = EnumStyle.European;

                if(chkFuture.Checked)
                    _isFuture = true;


            #endregion

               lblResult.Text=String.Empty;
                BinomialTree tree = new BinomialTree(_stockPrice,_strike, _timeToExpiration,
                    _volatility,_riskFreeRate,_putCall,_style,_steps,_dividendYield,_isFuture,
                    _dividend1,_dividend1Date,_dividend2,_dividend2Date,
                    _dividend3,_dividend3Date,_dividend4,_dividend4Date);

                //decimal presentValue = Convert.ToDecimal(tree.OptionValue());
                //System.Diagnostics.Debug.WriteLine(string.Format("{0},{1}", _steps.ToString(), presentValue.ToString()));
                lblResult.Text = tree.ComputeOptionValues().ToString();              
            }
            catch (FormatException)
            { 
                lblResult.Text = "An input was not in the correct format. Please check your input values";
            }
        }
        #region UtilityFunctions
        bool IsNotNull(string value)
        {
            if (value != null && value != String.Empty)
            { return true; }
            else
            {
                _exceptionCount++;
                return false;
            }
        }
        bool IsNotNullAndIsPositiveDouble(string value)
        {
            try
            {
                if (value != null && value != String.Empty && Convert.ToDouble(value) > 0)
                { return true; }
                else
                {
                    _exceptionCount++;
                    return false; 
                }
            }
            catch(FormatException)
            {
                lblResult.Text = "An input was not in the correct format";
                return false;
            }
        }
        #endregion 
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Tanveer Ansari specializes in application of .NET and Java to building automated trading systems. He also builds statistical (time series and bayesian) models to predict asset prices.

He is available for technology consulting for the financial markets at info@tanveeransari.com

Comments and Discussions