C++ Program to Implement Binomial Tree

This C++ Program demonstrates the implementation of Binomial Tree.

Here is source code of the C++ Program to demonstrate Binomial Tree. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Binomial Tree
  3.  */
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cmath>
  7. using namespace std;
  8. /*
  9.  * Node Declaration
  10.  */
  11. struct Node
  12. {
  13.     double price, time, optionvalue;
  14. };
  15. /*
  16.  * Class Declaration
  17.  */
  18. class BinomialTree
  19. {
  20.     private:
  21.         Node **tree;
  22.         int n;
  23.         double S, volatility, upfactor, tfin, tstep;
  24.         double getValue(int level, int node, double K, double R);
  25.         void initNode (int level, int node);
  26.     public:
  27.         BinomialTree(double S, double volatility, int n, double tstep);
  28.         double getValue(double K, double R);
  29.         void print();
  30. };
  31. /*
  32.  * Constructor
  33.  */
  34. BinomialTree::BinomialTree(double price , double vol, int _n, double _tstep)
  35. {
  36.     n = _n;
  37.     S = price;
  38.     volatility = vol;
  39.     tstep = _tstep;
  40.     tfin = n * tstep;
  41.     upfactor = exp (volatility * sqrt (tstep));
  42.     tree = new Node * [n];
  43.     for (int i = 0; i < n; i++)
  44.         tree[i] = new Node [i+1] ;
  45.     tree[0][0].price = S;
  46.     tree[0][0].time = 0.0;
  47.     double currtime = 0.0;
  48.     for (int i = 1; i < n; i++)
  49.     {
  50.         Node * currnode = tree[i];
  51.         currtime += tstep;
  52.         for (int j = 0; j <= i; j++, currnode++)
  53.         {
  54.             if (!j)
  55.             {
  56.                 currnode->price = tree[i-1][j].price / upfactor ;
  57.                 currnode->time = currtime;
  58.             }
  59.             else
  60.             {
  61.                 currnode->price = tree[i-1][j-1].price * upfactor ;
  62.                 currnode->time = currtime;
  63.             }
  64.         }
  65.     }
  66. }
  67. /*
  68.  * Get Value Function
  69.  */
  70. double BinomialTree::getValue(int l, int node, double K, double df)
  71. {
  72.     if (l == (n-1))
  73.     {
  74.         if (K < tree[l][node].price)
  75.             return tree[l][node].optionvalue = tree[l][node].price - K;
  76.         else
  77.             return tree[l][node].optionvalue = 0.0;
  78.     }
  79.    else
  80.    {
  81.       double g1 = getValue(l + 1, node + 1, K, df);
  82.       double g2 = getValue(l + 1, node, K, df);
  83.       return tree[l][node].optionvalue = 0.5 * df * (g1 + g2);
  84.    }
  85. }
  86.  
  87. /*
  88.  * Get Value Function
  89.  */
  90. double BinomialTree::getValue(double K, double R)
  91. {
  92.     double discountfactor = exp (-R * tstep);
  93.     return getValue(0, 0, K, discountfactor);
  94. }
  95. /*
  96.  * Display optimal values
  97.  */
  98. void BinomialTree::print()
  99. {
  100.     for (int i = 0; i < n; i++)
  101.     {
  102.         for( int j = 0; j <= i; j++ )
  103.         {
  104.             cout<< "[" << tree[i][j].price << "," << tree[i][j].time << ",";
  105.             cout<< tree[i][j].optionvalue << "]\t";
  106.         }
  107.         cout <<endl;
  108.    }
  109. }
  110. /*
  111.  * Main Contains Menu
  112.  */
  113. int main()
  114. {
  115.     double S, V, K, T, R, N;
  116.     cout<<"Enter Security Price: ";
  117.     cin>>S;
  118.     cout<<"Enter Volatility: ";
  119.     cin>>V;
  120.     cout<<"Enter Call Strike Price: ";
  121.     cin>>K;
  122.     cout<<"Enter Time To Expiry: ";
  123.     cin>>T;
  124.     cout<<"Enter Risk Free Rate: ";
  125.     cin>>R;
  126.     cout<<"Enter levels: ";
  127.     cin>>N;
  128.     BinomialTree bt(S, V, N, T / N);
  129.     double value = bt.getValue(K, R);
  130.     bt.print();
  131.     cout<< "OPTION VALUE = " << value <<endl;
  132.     return 0;
  133. }

$ g++ binomailtree.cpp
$ a.out
 
Enter Security Price: 10
Enter Volatility: 0.5
Enter Call Strike Price: 100
Enter Time To Expiry: 5
Enter Risk Free Rate: 0.6
Enter levels: 3
[10,0,0]
[5.24402,1.66667,0]     [19.0693,1.66667,0]
[2.74997,3.33333,0]     [10,3.33333,0]  [36.364,3.33333,0]
OPTION VALUE = 0
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.